Advent of Code solutions

2021 day 16

+104
+49
2021/16/p1.tri
··· 1 + import "trilogy:io" use readall 2 + import "trilogy:debug" use dbg 3 + import "trilogy:core" use length 4 + import "trilogy:parsec" use parse, hex_integer, repeat, apply, any, many_1, followed_by, eof 5 + import "trilogy:bits" as bits 6 + import "trilogy:string" use chomp 7 + import "trilogy:array" use reduce 8 + 9 + func from_binary [] = 0 10 + func from_binary [..rest, true] = (from_binary rest * 2) + 1 11 + func from_binary [..rest, false] = from_binary rest * 2 12 + 13 + proc literal!() { 14 + let is_continued = apply any 15 + apply <| repeat 4 any 16 + if is_continued { 17 + apply literal 18 + } 19 + return 0 20 + } 21 + 22 + proc operator!() { 23 + let length_type = apply any 24 + if length_type == false { 25 + let sublength = from_binary <| apply <| repeat 15 any 26 + let content_bits = bits::from_array <| apply <| repeat sublength any 27 + return reduce (+) <| parse (followed_by eof <| many_1 packet) content_bits 28 + } else { 29 + let subpacket_count = from_binary <| apply <| repeat 11 any 30 + return reduce (+) <| apply <| repeat subpacket_count packet 31 + } 32 + } 33 + 34 + proc packet!() { 35 + let version = from_binary <| apply <| repeat 3 any 36 + let type_id = from_binary <| apply <| repeat 3 any 37 + let val = match type_id 38 + case 4 then apply literal 39 + else then apply operator 40 + return version + val 41 + } 42 + 43 + proc main!() { 44 + let hex_string = chomp readall!() 45 + let binary_input = bits::from <| parse hex_integer hex_string 46 + let real_len = length hex_string * 4 47 + let real_bits = binary_input <<~ (length binary_input - real_len) 48 + dbg!(parse packet real_bits) 49 + }
+55
2021/16/p2.tri
··· 1 + import "trilogy:io" use readall 2 + import "trilogy:debug" use dbg 3 + import "trilogy:core" use length 4 + import "trilogy:parsec" use parse, hex_integer, repeat, apply, any, many_1, followed_by, eof 5 + import "trilogy:bits" as bits 6 + import "trilogy:string" use chomp 7 + import "trilogy:compare" use min, max 8 + import "trilogy:array" use reduce 9 + 10 + func from_binary [] = 0 11 + func from_binary [..rest, true] = (from_binary rest * 2) + 1 12 + func from_binary [..rest, false] = from_binary rest * 2 13 + 14 + proc literal!() { 15 + let is_continued = apply any 16 + let seg = apply <| repeat 4 any 17 + if !is_continued { return seg } 18 + let rest = apply literal 19 + return [..seg, ..rest] 20 + } 21 + 22 + proc operator!() { 23 + let length_type = apply any 24 + if length_type == false { 25 + let sublength = from_binary <| apply <| repeat 15 any 26 + let content_bits = bits::from_array <| apply <| repeat sublength any 27 + return parse (followed_by eof <| many_1 packet) content_bits 28 + } else { 29 + let subpacket_count = from_binary <| apply <| repeat 11 any 30 + return apply <| repeat subpacket_count packet 31 + } 32 + } 33 + 34 + proc packet!() { 35 + let version = from_binary <| apply <| repeat 3 any 36 + let type_id = from_binary <| apply <| repeat 3 any 37 + return match type_id 38 + case 0 then reduce (+) <| apply operator 39 + case 1 then reduce (*) <| apply operator 40 + case 2 then reduce min <| apply operator 41 + case 3 then reduce max <| apply operator 42 + case 4 then from_binary <| apply literal 43 + case 5 then match apply operator case [a, b] if a > b then 1 else then 0 44 + case 6 then match apply operator case [a, b] if a < b then 1 else then 0 45 + case 7 then match apply operator case [a, b] if a == b then 1 else then 0 46 + else then 'err 47 + } 48 + 49 + proc main!() { 50 + let hex_string = chomp readall!() 51 + let binary_input = bits::from <| parse hex_integer hex_string 52 + let real_len = length hex_string * 4 53 + let real_bits = binary_input <<~ (length binary_input - real_len) 54 + dbg!(parse packet real_bits) 55 + }