Day 8 part 1

This commit is contained in:
Smaug123
2023-12-08 08:26:11 +00:00
parent afe82facb6
commit a9afb23c79
4 changed files with 63 additions and 8 deletions

View File

@@ -1,30 +1,67 @@
namespace AdventOfCode2023
open System
open System.Collections.Generic
open System.Globalization
[<RequireQualifiedAccess>]
module Day8 =
let parse (s : string) : ResizeArray<int> =
type Instructions =
{
/// "true" is 'R'
Steps : bool array
Nodes : Dictionary<string, string * string>
}
let parse (s : string) =
use mutable lines = StringSplitEnumerator.make '\n' s
let result = ResizeArray ()
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
result.Add (3)
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)
result
{
Steps = steps
Nodes = dict
}
let part1 (s : string) =
let arr = parse s
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 part2 (s : string) =
let arr = parse s
let data = parse s
let mutable answer = 0
answer

View File

@@ -25,7 +25,7 @@
<EmbeddedResource Include="samples\day5.txt" />
<EmbeddedResource Include="samples\day6.txt" />
<EmbeddedResource Include="samples\day7.txt" />
<Content Include="samples\day8.txt" />
<EmbeddedResource Include="samples\day8.txt" />
</ItemGroup>
<ItemGroup>

View File

@@ -13,7 +13,16 @@ module TestDay8 =
[<Test>]
let part1Sample () =
sample |> Day8.part1 |> shouldEqual 0
sample |> Day8.part1 |> shouldEqual 2
[<Test>]
let part1Sample2 () =
"""LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)"""
|> Day8.part1 |> shouldEqual 6
[<Test>]
let part2Sample () =

View 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)