-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day08.fs
40 lines (33 loc) · 1.25 KB
/
Day08.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
module Day08
open System
open System.Text.RegularExpressions
let parseLines (lines: string list) =
let parseLine ln =
Regex("\w+").Matches(ln) |> Seq.map (fun m -> m.Value) |> List.ofSeq
|> (fun [name; left; right] -> (name, (left, right)))
let top::_::rest = lines
let directions = top.ToCharArray() |> List.ofArray
let nodes = rest |> List.map parseLine |> Map
(directions, nodes)
let navigate start (allDirections, nodes) =
let rec nav current dirs count =
match current, dirs with
| name, _ when (name: string)[2] = 'Z' -> count
| _, [] -> nav current allDirections count
| _, dir::rest ->
match dir, Map.find current nodes with
| 'L', (next, _)
| 'R', (_, next) -> nav next rest (count + 1)
nav start allDirections 0
let rec lcm x y =
let rec hcf a b = if b = 0UL then a else hcf b (a % b)
x * (y / (hcf x y))
let part1 getLines = getLines "input" |> parseLines |> navigate "AAA"
let part2 getLines =
let (_, nodes) as input = getLines "input" |> parseLines
nodes
|> Map.toList
|> List.map fst
|> List.filter (fun name -> name[2] = 'A')
|> List.map (fun start -> navigate start input |> Convert.ToUInt64)
|> List.reduce lcm