/ 2025 / 3 / p1.tri
p1.tri
 1  import "trilogy:debug" use dbg
 2  import "trilogy:io" use readlines
 3  import "trilogy:string" use chars, chomp
 4  import "trilogy:number" use from_digit
 5  import "trilogy:iterator" as it
 6  import "trilogy:array" as arr
 7  
 8  func run_joltage (a : b) [only] = if only > b then a:only else a:b
 9  func run_joltage (a : b) [head, ..rest] =
10    if head > a then run_joltage (head:0) rest
11    else if head > b then run_joltage (a:head) rest
12    else run_joltage (a:b) rest
13  
14  func joltage digits = run_joltage (0:0) digits
15  
16  proc main!() {
17    let total = readlines
18      |> it::map (
19        chomp
20        >> chars
21        >> arr::map from_digit
22        >> joltage
23        >> fn a:b. a * 10 + b
24      )
25      |> it::sum
26    dbg!(total)
27  }