Advent of Code solutions

syntax updates

+67 -67
+1 -1
2023/14/p2.tri
··· 90 90 91 91 func key grid = fold (fn a b. $"${a}${str::cast b}") "" <| do() $(ch for line in grid and (ch in line or ch = '\n')) 92 92 93 - const target = 1000000000 93 + slot target = 1000000000 94 94 95 95 proc main!() { 96 96 let mut total = 0
+7 -7
2023/17/p1.tri
··· 5 5 module tuple at "trilogy:tuple" use fst, snd 6 6 module btreemod at "trilogy:btree" use btree 7 7 8 - const N = -0i1 9 - const S = 0i1 10 - const E = 1i0 11 - const W = -1i0 8 + slot N = -1i 9 + slot S = 1i 10 + slot E = 1 11 + slot W = -1 12 12 13 - func turn dir = [-0i1 * dir, -(-0i1 * dir)] 13 + func turn dir = [-1i * dir, -(-1i * dir)] 14 14 func key ^N = 'v 15 15 func key ^S = 'v 16 16 func key ^E = 'h ··· 24 24 25 25 proc main!() { 26 26 let grid = [[cast $"${n}"fmt for n in chars <| chomp line] for line in readlines!()] 27 - let best = {| 0i0 => {| 'h => 0, 'v => 0 |} |} 27 + let best = {| 0 => {| 'h => 0, 'v => 0 |} |} 28 28 29 29 let goal = (length grid * S + length (grid.0) * E) + N + W 30 30 ··· 33 33 34 34 let ltree = btree (asc score) 35 35 36 - let mut queue = ltree::from [0i0:E, 0i0:S] 36 + let mut queue = ltree::from [0:E, 0:S] 37 37 while queue != 'empty { 38 38 let (pos:face):q2 = ltree::pop_min queue 39 39 queue = q2
+7 -7
2023/17/p2.tri
··· 6 6 module btreemod at "trilogy:btree" use btree 7 7 module iter at "trilogy:iter" use range 8 8 9 - const N = -0i1 10 - const S = 0i1 11 - const E = 1i0 12 - const W = -1i0 9 + slot N = -1i 10 + slot S = 1i 11 + slot E = 1 12 + slot W = -1 13 13 14 - func turn dir = [-0i1 * dir, -(-0i1 * dir)] 14 + func turn dir = [-1i * dir, -(-1i * dir)] 15 15 func key ^N = 'v 16 16 func key ^S = 'v 17 17 func key ^E = 'h ··· 25 25 26 26 proc main!() { 27 27 let grid = [[cast $"${n}"fmt for n in chars <| chomp line] for line in readlines!()] 28 - let best = {| 0i0 => {| 'h => 0, 'v => 0 |} |} 28 + let best = {| 0 => {| 'h => 0, 'v => 0 |} |} 29 29 30 30 let goal = (length grid * S + length (grid.0) * E) + N + W 31 31 ··· 34 34 35 35 let ltree = btree (asc score) 36 36 37 - let mut queue = ltree::from [0i0:E, 0i0:S] 37 + let mut queue = ltree::from [0:E, 0:S] 38 38 while queue != 'empty { 39 39 let (pos:face):q2 = ltree::pop_min queue 40 40 queue = q2
+6 -6
2023/18/p1.tri
··· 6 6 module array at "trilogy:array" use fold, map, flatmap, length, filter 7 7 module iter at "trilogy:iter" use repeat, collect, range, rangex, of 8 8 9 - const line_regex = regex::new "([LRUD]) (\\d+) \\(#([a-f0-9]{6})\\)" 9 + slot line_regex = regex::new "([LRUD]) (\\d+) \\(#([a-f0-9]{6})\\)" 10 10 11 11 func scan f i [] = [i] 12 12 func scan f i [x, ..xs] = let a = f i x, [a, ..scan f a xs] 13 13 14 - const L = -1 15 - const R = 1 16 - const U = -0i1 17 - const D = 0i1 14 + slot L = -1 15 + slot R = 1 16 + slot U = -1i 17 + slot D = 1i 18 18 19 19 func delta "L" = L 20 20 func delta "R" = R ··· 35 35 36 36 proc main!() { 37 37 let instructions = [d:cast n:c for line in readlines!() and {| 1 => d, 2 => n, 3 => c, .._ |} = line_regex::matches line] 38 - let coords = scan move 0i0 instructions 38 + let coords = scan move 0 instructions 39 39 let min_x = fold min 0 <| map re coords 40 40 let max_x = fold max 0 <| map re coords 41 41 let min_y = fold min 0 <| map im coords
+6 -6
2023/18/p2.tri
··· 6 6 module array at "trilogy:array" use fold, map, flatmap, length, filter, sort, position 7 7 module iter at "trilogy:iter" use repeat, collect, range, rangex, of 8 8 9 - const line_regex = regex::new "([LRUD]) (\\d+) \\(#([a-f0-9]{6})\\)" 9 + slot line_regex = regex::new "([LRUD]) (\\d+) \\(#([a-f0-9]{6})\\)" 10 10 11 11 func scan f i [] = [i] 12 12 func scan f i [x, ..xs] = let a = f i x, [a, ..scan f a xs] 13 13 14 - const L = -1 15 - const R = 1 16 - const U = -0i1 17 - const D = 0i1 14 + slot L = -1 15 + slot R = 1 16 + slot U = -1i 17 + slot D = 1i 18 18 19 19 func delta "L" = L 20 20 func delta "R" = R ··· 44 44 45 45 proc main!() { 46 46 let instructions = [parse c for line in readlines!() and {| 3 => c, .._ |} = line_regex::matches line] 47 - let coords = scan move 0i0 instructions 47 + let coords = scan move 0 instructions 48 48 let xs = sort (<=) [x for x in [| re coord for coord in coords |]] 49 49 let ys = sort (<=) [y for y in [| im coord for coord in coords |]] 50 50
+2 -2
2023/19/p1.tri
··· 4 4 module str at "trilogy:str" use chomp, split 5 5 module regex at "trilogy:regex" 6 6 7 - const workflow_regex = regex::new "(?<name>\\w+)\\{(?<steps>.*)\\}" 8 - const step_regex = regex::new "(?<comp>[xmas])(?<op>[<>])(?<val>\\d+)" 7 + slot workflow_regex = regex::new "(?<name>\\w+)\\{(?<steps>.*)\\}" 8 + slot step_regex = regex::new "(?<comp>[xmas])(?<op>[<>])(?<val>\\d+)" 9 9 10 10 func always x _ = x 11 11
+2 -2
2023/19/p2.tri
··· 5 5 module range at "trilogy:range" use overlap, length, new 6 6 module regex at "trilogy:regex" 7 7 8 - const workflow_regex = regex::new "(?<name>\\w+)\\{(?<steps>.*)\\}" 9 - const step_regex = regex::new "(?<comp>[xmas])(?<op>[<>])(?<val>\\d+)" 8 + slot workflow_regex = regex::new "(?<name>\\w+)\\{(?<steps>.*)\\}" 9 + slot step_regex = regex::new "(?<comp>[xmas])(?<op>[<>])(?<val>\\d+)" 10 10 11 11 func comp_total comp = length comp 12 12 func part_total {| "x" => x, "m" => m, "a" => a, "s" => s |} = comp_total x * comp_total m * comp_total a * comp_total s
+1 -1
2023/2/p2.tri
··· 4 4 module array at "trilogy:array" use map, flatmap, fold 5 5 module regex at "trilogy:regex" 6 6 7 - const init = {| 7 + slot init = {| 8 8 "red" => 0, 9 9 "green" => 0, 10 10 "blue" => 0,
+2 -2
2023/20/p1.tri
··· 23 23 } 24 24 25 25 module flipflop destinations { 26 - const state = {| 'on => false |} 26 + slot state = {| 'on => false |} 27 27 28 28 proc add_input!(_) {} 29 29 ··· 55 55 } 56 56 57 57 module conjunction destinations { 58 - const state = {||} 58 + slot state = {||} 59 59 60 60 proc add_input!(input) { 61 61 state.input = 'low
+3 -3
2023/20/p2.tri
··· 25 25 } 26 26 27 27 module flipflop destinations { 28 - const state = {| 'on => false |} 28 + slot state = {| 'on => false |} 29 29 30 30 proc add_input!(_) {} 31 31 ··· 57 57 } 58 58 59 59 module conjunction destinations { 60 - const state = {||} 60 + slot state = {||} 61 61 62 62 proc add_input!(input) { 63 63 state.input = 'low ··· 93 93 export add_input, init, signal 94 94 } 95 95 96 - const important = ["mp", "qt", "qb", "ng"] 96 + slot important = ["mp", "qt", "qb", "ng"] 97 97 98 98 func parse_node srcdest = let [name, dests] = split " -> " srcdest, name : split ", " dests 99 99
+4 -4
2023/21/p1.tri
··· 5 5 module record at "trilogy:record" use values 6 6 module iter at "trilogy:iter" use count, filter, rangex 7 7 8 - const N = -0i1 9 - const S = 0i1 10 - const E = 1 11 - const W = -1 8 + slot N = -1i 9 + slot S = 1i 10 + slot E = 1 11 + slot W = -1 12 12 13 13 func get pos grid = with grid.(im pos).(re pos) else cancel '#' 14 14 func adj pos = $(d + pos for d in [N, S, E, W])
+4 -4
2023/21/p2.tri
··· 5 5 module record at "trilogy:record" use values 6 6 module iter at "trilogy:iter" use count, filter, rangex 7 7 8 - const N = -0i1 9 - const S = 0i1 10 - const E = 1 11 - const W = -1 8 + slot N = -1i 9 + slot S = 1i 10 + slot E = 1 11 + slot W = -1 12 12 13 13 func get pos grid = with grid.(im pos).(re pos) else cancel '#' 14 14 func adj pos = $(d + pos for d in [N, S, E, W])
+1 -1
2023/22/p1.tri
··· 7 7 module regex at "trilogy:regex" 8 8 module iter at "trilogy:iter" use range 9 9 10 - const line_regex = regex::new "(\\d+),(\\d+),(\\d+)~(\\d+),(\\d+),(\\d+)" 10 + slot line_regex = regex::new "(\\d+),(\\d+),(\\d+)~(\\d+),(\\d+),(\\d+)" 11 11 func parse str = 12 12 let matches = line_regex::matches str, 13 13 normalize (cast (matches.1):cast (matches.2):cast (matches.3)) (cast (matches.4):cast (matches.5):cast (matches.6))
+1 -1
2023/22/p2.tri
··· 7 7 module regex at "trilogy:regex" 8 8 module iter at "trilogy:iter" use range 9 9 10 - const line_regex = regex::new "(\\d+),(\\d+),(\\d+)~(\\d+),(\\d+),(\\d+)" 10 + slot line_regex = regex::new "(\\d+),(\\d+),(\\d+)~(\\d+),(\\d+),(\\d+)" 11 11 func parse str = 12 12 let matches = line_regex::matches str, 13 13 normalize (cast (matches.1):cast (matches.2):cast (matches.3)) (cast (matches.4):cast (matches.5):cast (matches.6))
+4 -4
2023/23/p1.tri
··· 5 5 module set at "trilogy:set" use contains, length 6 6 module tuple at "trilogy:tuple" use fst 7 7 8 - const N = -0i1 9 - const S = 0i1 10 - const E = 1 11 - const W = -1 8 + slot N = -1i 9 + slot S = 1i 10 + slot E = 1 11 + slot W = -1 12 12 13 13 func walkable ^E '>' = true 14 14 func walkable ^W '<' = true
+4 -4
2023/23/p2.tri
··· 6 6 module record at "trilogy:record" 7 7 module tuple at "trilogy:tuple" use fst 8 8 9 - const N = -0i1 10 - const S = 0i1 11 - const E = 1 12 - const W = -1 9 + slot N = -1i 10 + slot S = 1i 11 + slot E = 1 12 + slot W = -1 13 13 14 14 func walkable '#' = false 15 15 func walkable _ = true
+2 -2
2023/3/p1.tri
··· 7 7 module regex at "trilogy:regex" 8 8 module tuple at "trilogy:tuple" use fst 9 9 10 - const symbol_regex = regex::new "[^\\d.]" 11 - const number_regex = regex::new "\\d+" 10 + slot symbol_regex = regex::new "[^\\d.]" 11 + slot number_regex = regex::new "\\d+" 12 12 13 13 func symbols str = symbol_regex::all_positions str |> map (get 0 >> fst) 14 14 func numbers str = number_regex::all_captures str |> map (get 0)
+2 -2
2023/3/p2.tri
··· 7 7 module regex at "trilogy:regex" 8 8 module tuple at "trilogy:tuple" use fst 9 9 10 - const gear_regex = regex::new "[*]" 11 - const number_regex = regex::new "\\d+" 10 + slot gear_regex = regex::new "[*]" 11 + slot number_regex = regex::new "\\d+" 12 12 13 13 func gears str = gear_regex::all_positions str |> map (get 0 >> fst) 14 14 func numbers str = number_regex::all_captures str |> map (get 0)
+1 -1
2023/4/p1.tri
··· 6 6 module set at "trilogy:set" use intersection, length 7 7 module array at "trilogy:array" 8 8 9 - const card_regex = regex::new "Card\\s+\\d+:([^|]+)\\|([^|]+)" 9 + slot card_regex = regex::new "Card\\s+\\d+:([^|]+)\\|([^|]+)" 10 10 11 11 func parse_nums numstr = [| num::cast x for x in numstr |> split " " and is x != "" |] 12 12 func parse_card card = parse_nums card.1 : parse_nums card.2
+1 -1
2023/4/p2.tri
··· 6 6 module set at "trilogy:set" use intersection, length 7 7 module array at "trilogy:array" use fold 8 8 9 - const card_regex = regex::new "Card\\s+\\d+:([^|]+)\\|([^|]+)" 9 + slot card_regex = regex::new "Card\\s+\\d+:([^|]+)\\|([^|]+)" 10 10 11 11 func parse_nums numstr = [| num::cast x for x in numstr |> split " " and is x != "" |] 12 12 func parse_card card = parse_nums card.1 : parse_nums card.2
+1 -1
2023/5/p1.tri
··· 6 6 module record at "trilogy:record" 7 7 module iter at "trilogy:iter" use collect, of 8 8 9 - const heading_regex = regex::new "(\\w+)-to-(\\w+) map:" 9 + slot heading_regex = regex::new "(\\w+)-to-(\\w+) map:" 10 10 func parse_section [heading, ..ranges] = 11 11 let {| 1 => from, 2 => to, .._ |} = heading_regex::matches heading, 12 12 let parsed_ranges = map (str::split " " >> map cast) ranges,
+1 -1
2023/5/p2.tri
··· 13 13 |> map cast 14 14 |> (fn [dest, src, len]. dest - src : src : src + len) 15 15 16 - const heading_regex = regex::new "(\\w+)-to-(\\w+) map:" 16 + slot heading_regex = regex::new "(\\w+)-to-(\\w+) map:" 17 17 func parse_section [heading, ..ranges] = 18 18 let {| 1 => from, 2 => to, .._ |} = heading_regex::matches heading, 19 19 let parsed_ranges = map parse_range ranges,
+2 -2
2025/7/p1.tri
··· 29 29 readlines 30 30 |> it::map (it::enumerate << it::from << chomp) 31 31 |> it::enumerate 32 - |> it::flat_map (fn y:inner. it::map (fn x:v. x + y * 0i1 : v) inner) 32 + |> it::flat_map (fn y:inner. it::map (fn x:v. x + y * 1i : v) inner) 33 33 ) 34 34 let max_y = reduce max <| [im pos for pos in splitters] 35 35 let mut splits = 0 ··· 39 39 splits += length [beam for beam in beams and is contains beam splitters] 40 40 beams = beams 41 41 |> it::from 42 - |> it::flat_map (fn beam. if contains beam splitters then it::from [beam + 1i1, beam - 1 + 0i1] else it::of (beam + 0i1)) 42 + |> it::flat_map (fn beam. if contains beam splitters then it::from [beam + 1 + 1i, beam - 1 + 1i] else it::of (beam + 1i)) 43 43 |> collect 44 44 i += 1 45 45 }
+2 -2
2025/7/p2.tri
··· 29 29 readlines 30 30 |> it::map (it::enumerate << it::from << chomp) 31 31 |> it::enumerate 32 - |> it::flat_map (fn y:inner. it::map (fn x:v. x + y * 0i1 : v) inner) 32 + |> it::flat_map (fn y:inner. it::map (fn x:v. x + y * 1i : v) inner) 33 33 ) 34 34 let max_y = reduce max <| [im pos for pos in splitters] 35 35 let mut splits = 0 ··· 38 38 while i <= max_y { 39 39 beams = beams 40 40 |> it::from 41 - |> it::flat_map (fn beam:val. if contains beam splitters then it::from [beam + 1i1: val, beam - 1 + 0i1: val] else it::of (beam + 0i1:val)) 41 + |> it::flat_map (fn beam:val. if contains beam splitters then it::from [beam + 1 + 1i: val, beam - 1 + 1i: val] else it::of (beam + 1i:val)) 42 42 |> it::fold (fn rec pos:val. {| ..rec, pos => val + with rec.pos when 'mia cancel 0 else yield |}) {||} 43 43 i += 1 44 44 }