Advent of Code solutions

2025 day 9 (oom p2 though)

+147 -7
+2 -1
2025/3/p2.tri
··· 1 1 import "trilogy:debug" use dbg 2 2 import "trilogy:io" use readlines 3 3 import "trilogy:string" use chars, chomp 4 - import "trilogy:number" use from_digit, max 4 + import "trilogy:number" use from_digit 5 + import "trilogy:compare" use max 5 6 import "trilogy:iterator" as it 6 7 import "trilogy:array" as arr use take, skip 7 8
+2 -2
2025/5/p2.tri
··· 3 3 import "trilogy:array" use sort_by, fold 4 4 import "trilogy:parsec" use parse, apply, integer, char, per_line 5 5 import "trilogy:iterator" as it 6 - import "trilogy:number" use max 6 + import "trilogy:compare" use max, asc 7 7 8 8 proc input!() { 9 9 let range = do() { ··· 18 18 proc main!() { 19 19 let fresh:_ = readall!() 20 20 |> parse input 21 - |> sort_by (fn a b. a.'left < b.'left) 21 + |> sort_by (asc <| fn a. a.'left) 22 22 |> fold (fn (fresh:index) (low:high). fresh + ((max index (high + 1)) - (max index low)):(max index (high + 1))) (0:0) 23 23 dbg!(fresh) 24 24 }
+2 -1
2025/7/p1.tri
··· 1 1 import "trilogy:debug" use dbg 2 2 import "trilogy:io" use readlines 3 3 import "trilogy:string" use split, chomp 4 - import "trilogy:number" use max, im 4 + import "trilogy:compare" use max 5 + import "trilogy:number" use im 5 6 import "trilogy:array" use map, reduce, length 6 7 import "trilogy:set" use push, contains, collect 7 8 import "trilogy:iterator" as it
+2 -1
2025/7/p2.tri
··· 1 1 import "trilogy:debug" use dbg 2 2 import "trilogy:io" use readlines 3 3 import "trilogy:string" use split, chomp 4 - import "trilogy:number" use max, im 4 + import "trilogy:number" use im 5 + import "trilogy:compare" use max 5 6 import "trilogy:array" use map, reduce, length 6 7 import "trilogy:set" use push, contains, collect 7 8 import "trilogy:iterator" as it
+2 -2
2025/8/p1.tri
··· 4 4 import "trilogy:array" use push, collect, sort_by, take, reduce 5 5 import "trilogy:core" use length 6 6 import "trilogy:iterator" as it 7 + import "trilogy:compare" use asc 7 8 import "trilogy:set" use union 8 9 9 10 func dist [x1, y1, z1]:[x2, y2, z2] = (x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2 10 - func dist_asc a b = dist a < dist b 11 11 12 12 proc main!() { 13 13 let points = readlines ··· 25 25 i += 1 26 26 } 27 27 dbg!(length edges) 28 - edges sort_by= dist_asc 28 + edges sort_by= (asc dist) 29 29 dbg!("sorted") 30 30 edges take= 1000 31 31 dbg!("taken")
+27
2025/9/p1.tri
··· 1 + import "trilogy:io" use readlines 2 + import "trilogy:debug" use dbg 3 + import "trilogy:parsec" use parse, sep_by, char, integer 4 + import "trilogy:array" use collect, length 5 + import "trilogy:number" use abs 6 + import "trilogy:compare" use max 7 + import "trilogy:iterator" use map 8 + 9 + func area [x1, y1] [x2, y2] = (abs (x1 - x2) + 1) * (abs (y1 - y2) + 1) 10 + 11 + proc main!() { 12 + let points = readlines 13 + |> map (parse (sep_by (char ',') integer)) 14 + |> collect 15 + 16 + let mut best = 0 17 + let mut i = 0 18 + while i < length points { 19 + let mut j = i + 1 20 + while j < length points { 21 + best max= (area (points.i) (points.j)) 22 + j += 1 23 + } 24 + i += 1 25 + } 26 + dbg!(best) 27 + }
+110
2025/9/p2.tri
··· 1 + import "trilogy:io" use readlines 2 + import "trilogy:debug" use dbg 3 + import "trilogy:parsec" use parse, sep_by, char, integer 4 + import "trilogy:array" use collect, length, map, reduce, first, tail, filter, sort, sort_by, any, push, zip 5 + import "trilogy:number" use abs 6 + import "trilogy:compare" use max, asc 7 + import "trilogy:iterator" as it 8 + 9 + func area [x1, y1] [x2, y2] = (abs (x1 - x2) + 1) * (abs (y1 - y2) + 1) 10 + 11 + func is_horizontal [_, y1]:[_, y2] = y1 == y2 12 + func is_vertical [x1, _]:[x2, _] = x1 == x2 13 + 14 + func collide p1:p2 [bx1, by1]:[bx2, by2] = 15 + let [px1, px2] = sort [x p1, x p2], 16 + let [py1, py2] = sort [y p1, y p2], 17 + px1 < bx2 18 + && px2 > bx1 19 + && py1 < by2 20 + && py2 > by1 21 + 22 + func x [a, _] = a 23 + func y [_, a] = a 24 + 25 + func left a:_ = a 26 + 27 + func contains_by key (a:b) (p1:p2) = 28 + let [w, z] = sort [key p1, key p2], 29 + w <= a && a <= z && w <= b && b <= z 30 + 31 + proc main!() { 32 + let points = readlines 33 + |> it::map (parse (sep_by (char ',') integer)) 34 + |> collect 35 + dbg!('points(length points)) 36 + 37 + let edges = zip points [..tail points, first points] 38 + dbg!('edges(length edges)) 39 + 40 + # It has been observed that the shape is convex and without holes or interior crossings 41 + # 42 + # So we can make a series of rectangles that represent the illegal area by sending beams 43 + # from each edge 44 + let verticals = sort_by (asc <| (x << left)) <| filter is_vertical edges 45 + dbg!('verticals(length verticals)) 46 + 47 + let horizontals = sort_by (asc <| (y << left)) <| filter is_horizontal edges 48 + dbg!('horizontals(length horizontals)) 49 + 50 + let xs = map (x << left) verticals 51 + dbg!('xs(length xs)) 52 + 53 + let ys = map (y << left) horizontals 54 + dbg!('ys(length ys)) 55 + 56 + let [min_x, .._, max_x] = xs 57 + let [min_y, .._, max_y] = ys 58 + 59 + let illegal = [] 60 + 61 + dbg!(min_x:max_x) 62 + dbg!(min_y:max_y) 63 + 64 + let x_ranges = zip xs (tail xs) 65 + let mut i = 0 66 + while i < length x_ranges { 67 + let x1:x2 and range = x_ranges.i 68 + i += 1 69 + if x1 == x2 { continue unit } 70 + let [y1, .._, y2] = horizontals 71 + |> filter (contains_by x range) 72 + |> map (y << left) 73 + |> sort 74 + push!(illegal, [x1, min_y]:[x2, y1]) 75 + push!(illegal, [x1, y2]:[x2, max_y]) 76 + } 77 + dbg!('illegal(length illegal)) 78 + 79 + let y_ranges = zip ys (tail ys) 80 + let mut i = 0 81 + while i < length y_ranges { 82 + let y1:y2 and range = y_ranges.i 83 + i += 1 84 + if y1 == y2 { continue unit } 85 + let [x1, .._, x2] = verticals 86 + |> filter (contains_by y range) 87 + |> map (x << left) 88 + |> sort 89 + push!(illegal, [min_x, y1]:[x1, y2]) 90 + push!(illegal, [x2, y1]:[max_x, y2]) 91 + } 92 + dbg!('illegal(length illegal)) 93 + 94 + # Then we just run standard collision of the area boxes with those 95 + let mut best = 0 96 + let mut i = 0 97 + while i < length points { 98 + let mut j = i + 1 99 + while j < length points { 100 + let a = points.i 101 + let b = points.j 102 + if !(any (collide (a:b)) illegal) { 103 + best max= (area a b) 104 + } 105 + j += 1 106 + } 107 + i += 1 108 + } 109 + dbg!(best) 110 + }