-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2023day8ex2.dwl
50 lines (50 loc) · 1.45 KB
/
aoc2023day8ex2.dwl
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
%dw 2.5
import every from dw::core::Arrays
var parts = payload
replace '\r' with ''
splitBy '\n\n'
var dir = parts[0]
var len = sizeOf(dir)
var graph: Object = parts[1]
splitBy '\n'
map ($ splitBy ' = ')
map {
($[0]): $[1][1 to -2]
splitBy ', '
then { L: $[0], R: $[1]}
}
reduce (v, acc={}) -> v ++ acc
var idToName = namesOf(graph)
var nameToId = idToName map { ($): $$ } reduce (v, acc={}) -> v ++ acc
var isWinning = idToName map ($ endsWith 'Z')
var numGraph = idToName flatMap [nameToId[graph[$].L] as Number, nameToId[graph[$].R] as Number]
fun wander(init: String) = do {
@TailRec()
fun rec(step: Number, curr: Number) =
if (isWinning[curr])
step
else
dir[step mod len] match {
case 'L' -> rec(step + 1, numGraph[curr * 2])
case 'R' -> rec(step + 1, numGraph[curr * 2 + 1])
}
---
rec(0, nameToId[init] as Number)
}
@TailRec()
fun gcd(a: Number, b: Number): Number =
if (b == 0)
a
else
gcd(b, a mod b)
fun lcm(a: Number, b: Number): Number =
(a*b)/gcd(a, b)
var initial = namesOf(graph) filter ($ endsWith 'A')
---
// NOTE: This is not a real solution to the problem
//
// This solution relies on an unspecified property of the input graph.
// The "ENDING WITH Z" property of a path always repeats with the same frequency.
initial
map wander($)
reduce (v, acc=1) -> lcm(v, acc)