-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08_part01.fs
44 lines (36 loc) · 1.47 KB
/
day08_part01.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module day08_part01
open System.Text.RegularExpressions
open AdventOfCode_2023.Modules.LocalHelper
let path = "day08/day08_input.txt"
type Node = {
Root: string
Left: string
Right: string
}
let rec countSteps (nodes: Node list) (currentNode: Node) (steps: int) (instructions: string array) (iIdx: int) =
let ins = instructions.[iIdx]
let toCheck = if ins = "L" then currentNode.Left else currentNode.Right
let nodes' = nodes |> List.filter(fun n -> n.Root = toCheck)
match nodes' with
| [] -> failwith "Unexpected"
| head :: _ ->
if head.Root = "ZZZ" then steps + 1
else
let steps = steps + 1
countSteps nodes head steps instructions ((iIdx + 1) % instructions.Length)
let execute =
let lines = GetLinesFromFile path
let instructions = lines.[0].ToCharArray() |> Array.map string
let nodes =
lines
|> Array.skip 2
|> Array.map (fun l -> Regex.Matches(l, @"\w+")
|> Seq.toArray)
|> Array.map (fun m -> {
Root = m.GetValue(0).ToString();
Left = m.GetValue(1).ToString();
Right = m.GetValue(2).ToString()
} ) |> Array.toList
let initNode = nodes |> List.filter(fun n -> n.Root = "AAA") |> List.head
let result = countSteps nodes initNode 0 instructions 0
result