Compare commits
3 Commits
bcd2bb6349
...
54eb8b20e9
Author | SHA1 | Date | |
---|---|---|---|
|
54eb8b20e9 | ||
|
a9afb23c79 | ||
|
afe82facb6 |
@@ -9,6 +9,7 @@
|
|||||||
<Compile Include="Arr2D.fs" />
|
<Compile Include="Arr2D.fs" />
|
||||||
<Compile Include="ResizeArray.fs" />
|
<Compile Include="ResizeArray.fs" />
|
||||||
<Compile Include="EfficientString.fs" />
|
<Compile Include="EfficientString.fs" />
|
||||||
|
<Compile Include="Arithmetic.fs" />
|
||||||
<Compile Include="Day1.fs" />
|
<Compile Include="Day1.fs" />
|
||||||
<Compile Include="Day2.fs" />
|
<Compile Include="Day2.fs" />
|
||||||
<Compile Include="Day3.fs" />
|
<Compile Include="Day3.fs" />
|
||||||
@@ -16,6 +17,7 @@
|
|||||||
<Compile Include="Day5.fs" />
|
<Compile Include="Day5.fs" />
|
||||||
<Compile Include="Day6.fs" />
|
<Compile Include="Day6.fs" />
|
||||||
<Compile Include="Day7.fs" />
|
<Compile Include="Day7.fs" />
|
||||||
|
<Compile Include="Day8.fs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@@ -0,0 +1,67 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Arithmetic =
|
||||||
|
|
||||||
|
/// Compute floor(sqrt(i)).
|
||||||
|
let inline sqrt (i : ^a) =
|
||||||
|
if i <= LanguagePrimitives.GenericOne then
|
||||||
|
i
|
||||||
|
else
|
||||||
|
let rec go start =
|
||||||
|
let next = start + LanguagePrimitives.GenericOne
|
||||||
|
let sqr = next * next
|
||||||
|
|
||||||
|
if sqr < LanguagePrimitives.GenericZero then
|
||||||
|
// Overflow attempted, so the sqrt is between start and next
|
||||||
|
start
|
||||||
|
elif i < sqr then
|
||||||
|
start
|
||||||
|
elif i = sqr then
|
||||||
|
next
|
||||||
|
else
|
||||||
|
go next
|
||||||
|
|
||||||
|
go LanguagePrimitives.GenericOne
|
||||||
|
|
||||||
|
/// Find Hcf, A, B s.t. A * a + B * b = Hcf, and Hcf is the highest common factor of a and b.
|
||||||
|
let inline euclideanAlgorithm
|
||||||
|
(a : ^a)
|
||||||
|
(b : ^a)
|
||||||
|
: {|
|
||||||
|
Hcf : ^a
|
||||||
|
A : ^a
|
||||||
|
B : ^a
|
||||||
|
|}
|
||||||
|
=
|
||||||
|
let rec go rMin1 r sMin1 s tMin1 t =
|
||||||
|
if r = LanguagePrimitives.GenericZero then
|
||||||
|
{|
|
||||||
|
Hcf = rMin1
|
||||||
|
A = sMin1
|
||||||
|
B = tMin1
|
||||||
|
|}
|
||||||
|
else
|
||||||
|
let newQ = rMin1 / r
|
||||||
|
go r (rMin1 - newQ * r) s (sMin1 - newQ * s) t (tMin1 - newQ * t)
|
||||||
|
|
||||||
|
let maxA = max a b
|
||||||
|
let minB = min a b
|
||||||
|
|
||||||
|
let result =
|
||||||
|
go
|
||||||
|
maxA
|
||||||
|
minB
|
||||||
|
LanguagePrimitives.GenericOne
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
LanguagePrimitives.GenericOne
|
||||||
|
|
||||||
|
if a = maxA then
|
||||||
|
result
|
||||||
|
else
|
||||||
|
{|
|
||||||
|
Hcf = result.Hcf
|
||||||
|
A = result.B
|
||||||
|
B = result.A
|
||||||
|
|}
|
108
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day8.fs
Normal file
108
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day8.fs
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open System
|
||||||
|
open System.Collections.Generic
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day8 =
|
||||||
|
|
||||||
|
type Instructions =
|
||||||
|
{
|
||||||
|
/// "true" is 'R'
|
||||||
|
Steps : bool array
|
||||||
|
|
||||||
|
Nodes : Dictionary<string, string * string>
|
||||||
|
}
|
||||||
|
|
||||||
|
let parse (s : string) =
|
||||||
|
use mutable lines = StringSplitEnumerator.make '\n' s
|
||||||
|
|
||||||
|
lines.MoveNext () |> ignore
|
||||||
|
|
||||||
|
let stepsLine = lines.Current.TrimEnd ()
|
||||||
|
let steps = Array.zeroCreate stepsLine.Length
|
||||||
|
|
||||||
|
for i = 0 to stepsLine.Length - 1 do
|
||||||
|
steps.[i] <- (stepsLine.[i] = 'R')
|
||||||
|
|
||||||
|
let dict = Dictionary ()
|
||||||
|
|
||||||
|
while lines.MoveNext () do
|
||||||
|
if not lines.Current.IsEmpty then
|
||||||
|
use mutable line = StringSplitEnumerator.make' ' ' lines.Current
|
||||||
|
line.MoveNext () |> ignore
|
||||||
|
let key = line.Current.ToString ()
|
||||||
|
line.MoveNext () |> ignore
|
||||||
|
line.MoveNext () |> ignore
|
||||||
|
let v1 = line.Current.Slice(1, line.Current.Length - 2).ToString ()
|
||||||
|
line.MoveNext () |> ignore
|
||||||
|
let v2 = line.Current.Slice(0, line.Current.Length - 1).ToString ()
|
||||||
|
dict.[key] <- (v1, v2)
|
||||||
|
|
||||||
|
{
|
||||||
|
Steps = steps
|
||||||
|
Nodes = dict
|
||||||
|
}
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
let data = parse s
|
||||||
|
let mutable i = 0
|
||||||
|
let mutable currentNode = "AAA"
|
||||||
|
let mutable answer = 0
|
||||||
|
|
||||||
|
while currentNode <> "ZZZ" do
|
||||||
|
let instruction = data.Nodes.[currentNode]
|
||||||
|
|
||||||
|
if data.Steps.[i] then
|
||||||
|
// "true" is R
|
||||||
|
currentNode <- snd instruction
|
||||||
|
else
|
||||||
|
currentNode <- fst instruction
|
||||||
|
|
||||||
|
i <- (i + 1) % data.Steps.Length
|
||||||
|
answer <- answer + 1
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let inline lcm (periods : ^T[]) =
|
||||||
|
let mutable lcm = periods.[0]
|
||||||
|
let mutable i = 1
|
||||||
|
|
||||||
|
while i < periods.Length do
|
||||||
|
let euclid = Arithmetic.euclideanAlgorithm lcm periods.[i]
|
||||||
|
lcm <- (lcm * periods.[i]) / euclid.Hcf
|
||||||
|
i <- i + 1
|
||||||
|
|
||||||
|
lcm
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
let data = parse s
|
||||||
|
|
||||||
|
let startingNodes =
|
||||||
|
data.Nodes.Keys
|
||||||
|
|> Seq.choose (fun k -> if k.[k.Length - 1] = 'A' then Some k else None)
|
||||||
|
|> Seq.toArray
|
||||||
|
|
||||||
|
let periods =
|
||||||
|
startingNodes
|
||||||
|
|> Array.map (fun startNode ->
|
||||||
|
let mutable i = 0
|
||||||
|
let mutable currentNode = startNode
|
||||||
|
let mutable answer = 0ul
|
||||||
|
|
||||||
|
while currentNode.[currentNode.Length - 1] <> 'Z' do
|
||||||
|
let instruction = data.Nodes.[currentNode]
|
||||||
|
|
||||||
|
if data.Steps.[i] then
|
||||||
|
// "true" is R
|
||||||
|
currentNode <- snd instruction
|
||||||
|
else
|
||||||
|
currentNode <- fst instruction
|
||||||
|
|
||||||
|
i <- (i + 1) % data.Steps.Length
|
||||||
|
answer <- answer + 1ul
|
||||||
|
|
||||||
|
uint64 answer
|
||||||
|
)
|
||||||
|
|
||||||
|
lcm periods
|
@@ -158,6 +158,26 @@ module Program =
|
|||||||
Console.WriteLine (part2.ToString ())
|
Console.WriteLine (part2.ToString ())
|
||||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 8====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input =
|
||||||
|
try
|
||||||
|
Path.Combine (dir.FullName, "day8part1.txt") |> File.ReadAllText
|
||||||
|
with :? FileNotFoundException ->
|
||||||
|
Path.Combine (dir.FullName, "day8.txt") |> File.ReadAllText
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day8.part1 input
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part1.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
sw.Restart ()
|
||||||
|
let input = Path.Combine (dir.FullName, "day8.txt") |> File.ReadAllText
|
||||||
|
let part2 = Day8.part2 input
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part2.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
endToEnd.Stop ()
|
endToEnd.Stop ()
|
||||||
|
|
||||||
Console.Error.WriteLine (
|
Console.Error.WriteLine (
|
||||||
|
@@ -16,6 +16,7 @@
|
|||||||
<Compile Include="TestDay5.fs" />
|
<Compile Include="TestDay5.fs" />
|
||||||
<Compile Include="TestDay6.fs" />
|
<Compile Include="TestDay6.fs" />
|
||||||
<Compile Include="TestDay7.fs" />
|
<Compile Include="TestDay7.fs" />
|
||||||
|
<Compile Include="TestDay8.fs" />
|
||||||
<EmbeddedResource Include="samples\day1.txt" />
|
<EmbeddedResource Include="samples\day1.txt" />
|
||||||
<EmbeddedResource Include="samples\day1part1.txt" />
|
<EmbeddedResource Include="samples\day1part1.txt" />
|
||||||
<EmbeddedResource Include="samples\day2.txt" />
|
<EmbeddedResource Include="samples\day2.txt" />
|
||||||
@@ -24,6 +25,8 @@
|
|||||||
<EmbeddedResource Include="samples\day5.txt" />
|
<EmbeddedResource Include="samples\day5.txt" />
|
||||||
<EmbeddedResource Include="samples\day6.txt" />
|
<EmbeddedResource Include="samples\day6.txt" />
|
||||||
<EmbeddedResource Include="samples\day7.txt" />
|
<EmbeddedResource Include="samples\day7.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day8part1.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day8.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
57
AdventOfCode2023.FSharp/Test/TestDay8.fs
Normal file
57
AdventOfCode2023.FSharp/Test/TestDay8.fs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay8 =
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day8part1.txt"
|
||||||
|
|> Day8.part1
|
||||||
|
|> shouldEqual 2
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample2 () =
|
||||||
|
"""LLR
|
||||||
|
|
||||||
|
AAA = (BBB, BBB)
|
||||||
|
BBB = (AAA, ZZZ)
|
||||||
|
ZZZ = (ZZZ, ZZZ)"""
|
||||||
|
|> Day8.part1
|
||||||
|
|> shouldEqual 6
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day8.txt"
|
||||||
|
|> Day8.part2
|
||||||
|
|> shouldEqual 6uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day8.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day8.part1 s |> shouldEqual 19199
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day8.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day8.part2 s |> shouldEqual 13663968099527uL
|
10
AdventOfCode2023.FSharp/Test/samples/day8.txt
Normal file
10
AdventOfCode2023.FSharp/Test/samples/day8.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
LR
|
||||||
|
|
||||||
|
11A = (11B, XXX)
|
||||||
|
11B = (XXX, 11Z)
|
||||||
|
11Z = (11B, XXX)
|
||||||
|
22A = (22B, XXX)
|
||||||
|
22B = (22C, 22C)
|
||||||
|
22C = (22Z, 22Z)
|
||||||
|
22Z = (22B, 22B)
|
||||||
|
XXX = (XXX, XXX)
|
9
AdventOfCode2023.FSharp/Test/samples/day8part1.txt
Normal file
9
AdventOfCode2023.FSharp/Test/samples/day8part1.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
RL
|
||||||
|
|
||||||
|
AAA = (BBB, CCC)
|
||||||
|
BBB = (DDD, EEE)
|
||||||
|
CCC = (ZZZ, GGG)
|
||||||
|
DDD = (DDD, DDD)
|
||||||
|
EEE = (EEE, EEE)
|
||||||
|
GGG = (GGG, GGG)
|
||||||
|
ZZZ = (ZZZ, ZZZ)
|
Reference in New Issue
Block a user