Advent of Code solutions
1import "trilogy:debug" use dbg
2import "trilogy:io" use readlines
3import "trilogy:number" use abs
4import "trilogy:parsec" use parse, char_of, integer, apply
5import "trilogy:iterator" as it
6
7proc rotation!() {
8 let dir = apply <| char_of "LR"
9 let amt = apply integer
10 return if dir == 'R' then amt else -amt
11}
12
13func rotate amt pos = {
14 let full = abs amt // 100
15 let rotated = pos + amt % 100
16 let crossed = rotated < 0 || rotated > 99
17 yield 'rotations(full + if crossed then 1 else 0)
18 (rotated + 100) % 100
19}
20
21proc main!() {
22 let mut answer = 0
23 with readlines
24 |> it::map (parse rotation)
25 |> it::fold rotate 50 {
26 when 'rotations(n) then {
27 answer += n
28 become unit
29 }
30 }
31 dbg!(answer)
32}