···11+import "trilogy:debug" use dbg
22+import "trilogy:io" use readlines
33+import "trilogy:string" use chars, chomp
44+import "trilogy:number" use from_digit
55+import "trilogy:iterator" as it
66+import "trilogy:array" as arr
77+88+func run_joltage (a : b) [only] = if only > b then a:only else a:b
99+func run_joltage (a : b) [head, ..rest] =
1010+ if head > a then run_joltage (head:0) rest
1111+ else if head > b then run_joltage (a:head) rest
1212+ else run_joltage (a:b) rest
1313+1414+func joltage digits = run_joltage (0:0) digits
1515+1616+proc main!() {
1717+ let total = readlines
1818+ |> it::map (
1919+ chomp
2020+ >> chars
2121+ >> arr::map from_digit
2222+ >> joltage
2323+ >> fn a:b. a * 10 + b
2424+ )
2525+ |> it::sum
2626+ dbg!(total)
2727+}
+32
2025/3/p2.tri
···11+import "trilogy:debug" use dbg
22+import "trilogy:io" use readlines
33+import "trilogy:string" use chars, chomp
44+import "trilogy:number" use from_digit, max
55+import "trilogy:iterator" as it
66+import "trilogy:array" as arr use take, skip
77+88+func update_state state offset digit =
99+ let len = arr::length state,
1010+ if offset >= len then state
1111+ else if state.offset < digit then [..take offset state, digit, ..arr::collect <| it::repeat (len - offset - 1) 0]
1212+ else update_state state (offset + 1) digit
1313+1414+func run_joltage state [] = state
1515+func run_joltage state [head, ..rest] =
1616+ let limit = max ((arr::length state) - (arr::length rest) - 1) 0,
1717+ run_joltage (update_state state limit head) rest
1818+1919+func joltage digits = run_joltage [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] digits
2020+2121+proc main!() {
2222+ let total = readlines
2323+ |> it::map (
2424+ chomp
2525+ >> chars
2626+ >> arr::map from_digit
2727+ >> joltage
2828+ >> arr::fold (fn a b. a * 10 + b) 0
2929+ )
3030+ |> it::sum
3131+ dbg!(total)
3232+}