This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
generated from mazharenko/aoc-agent-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.fs
70 lines (55 loc) · 2.14 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module impl.day08
open Farkle
open Farkle.Builder
open Farkle.Builder.Regex
type Direction = | Left | Right
let direction = "Direction" ||= [
!& "R" =% Right
!& "L" =% Left
]
let instructions = many1 direction
let node = regexString "[0-9A-Z]+" |> terminal "Node" (T(fun _ chars -> chars.ToString()))
let mapEntry = "MapEntry" ||= [
!@ node .>> "=" .>> "(" .>>. node .>> "," .>>. node .>> ")"
=> fun target left right -> target, (left, right)
]
let parse input =
let blocks = Pattern2.read id input
let instructions =
blocks[0]
|> RuntimeFarkle.parseUnsafe (RuntimeFarkle.build instructions)
let mapEntryParser = RuntimeFarkle.build mapEntry
let parseMapEntry s =
s
|> RuntimeFarkle.parseUnsafe mapEntryParser
let map = Pattern1.read parseMapEntry blocks[1] |> Map.ofArray
instructions |> Array.ofList, map
let solve1 (instructions : Direction[]) (map : Map<string, string*string>) =
let infiniteInstructions = Seq.initInfinite (fun i -> instructions[i % (Array.length instructions)])
let infiniteNavigations =
infiniteInstructions
|> Seq.scan (
fun node instruction ->
let entry = map[node]
match instruction with
| Left -> fst entry
| Right -> snd entry
) "AAA"
infiniteNavigations
|> Seq.findIndex ((=)"ZZZ")
let solve2 (instructions : Direction[]) (map : Map<string, string*string>) =
let infiniteInstructions = Seq.initInfinite (fun i -> instructions[i % (Array.length instructions)])
let allA = map |> Map.keys |> Seq.filter (fun s -> s.EndsWith "A") |> Array.ofSeq
let infiniteNavigations start =
infiniteInstructions
|> Seq.scan (
fun node instruction ->
let entry = map[node]
match instruction with
| Left -> fst entry
| Right -> snd entry
) start
allA |> Array.map (fun a ->
infiniteNavigations a
|> Seq.findIndex (fun node -> node.EndsWith "Z") |> int64
) |> Array.reduce lcm