-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc2023day8ex1.dwl
44 lines (44 loc) · 938 Bytes
/
aoc2023day8ex1.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
%dw 2.5
fun repeat(init: String): Array<String> = do {
@TailRec()
fun rec(curr: String | Null): Array<String> =
if (curr == null)
rec(init)
else
[curr[0] ~ rec(curr[1 to -1])]
---
rec(init)
}
var parts = payload
replace '\r' with ''
splitBy '\n\n'
var directions = repeat(parts[0])
var graph = parts[1]
splitBy '\n'
map ($ splitBy ' = ')
map {
($[0]): $[1][1 to -2]
splitBy ', '
then { L: $[0], R: $[1]}
}
reduce ($ ++ $$)
@TailRec()
fun wander(
directions: Array<String>,
graph,
curr: String,
steps: Number
) =
if (curr == 'ZZZ')
steps
else
directions match {
case [x ~ xs] ->
wander(
xs,
graph,
graph[curr][x],
steps + 1)
}
---
wander(directions, graph, 'AAA', 0)