···11+import "trilogy:io" use readall
22+import "trilogy:debug" use dbg
33+import "trilogy:core" use length
44+import "trilogy:parsec" use parse, hex_integer, repeat, apply, any, many_1, followed_by, eof
55+import "trilogy:bits" as bits
66+import "trilogy:string" use chomp
77+import "trilogy:array" use reduce
88+99+func from_binary [] = 0
1010+func from_binary [..rest, true] = (from_binary rest * 2) + 1
1111+func from_binary [..rest, false] = from_binary rest * 2
1212+1313+proc literal!() {
1414+ let is_continued = apply any
1515+ apply <| repeat 4 any
1616+ if is_continued {
1717+ apply literal
1818+ }
1919+ return 0
2020+}
2121+2222+proc operator!() {
2323+ let length_type = apply any
2424+ if length_type == false {
2525+ let sublength = from_binary <| apply <| repeat 15 any
2626+ let content_bits = bits::from_array <| apply <| repeat sublength any
2727+ return reduce (+) <| parse (followed_by eof <| many_1 packet) content_bits
2828+ } else {
2929+ let subpacket_count = from_binary <| apply <| repeat 11 any
3030+ return reduce (+) <| apply <| repeat subpacket_count packet
3131+ }
3232+}
3333+3434+proc packet!() {
3535+ let version = from_binary <| apply <| repeat 3 any
3636+ let type_id = from_binary <| apply <| repeat 3 any
3737+ let val = match type_id
3838+ case 4 then apply literal
3939+ else then apply operator
4040+ return version + val
4141+}
4242+4343+proc main!() {
4444+ let hex_string = chomp readall!()
4545+ let binary_input = bits::from <| parse hex_integer hex_string
4646+ let real_len = length hex_string * 4
4747+ let real_bits = binary_input <<~ (length binary_input - real_len)
4848+ dbg!(parse packet real_bits)
4949+}
+55
2021/16/p2.tri
···11+import "trilogy:io" use readall
22+import "trilogy:debug" use dbg
33+import "trilogy:core" use length
44+import "trilogy:parsec" use parse, hex_integer, repeat, apply, any, many_1, followed_by, eof
55+import "trilogy:bits" as bits
66+import "trilogy:string" use chomp
77+import "trilogy:compare" use min, max
88+import "trilogy:array" use reduce
99+1010+func from_binary [] = 0
1111+func from_binary [..rest, true] = (from_binary rest * 2) + 1
1212+func from_binary [..rest, false] = from_binary rest * 2
1313+1414+proc literal!() {
1515+ let is_continued = apply any
1616+ let seg = apply <| repeat 4 any
1717+ if !is_continued { return seg }
1818+ let rest = apply literal
1919+ return [..seg, ..rest]
2020+}
2121+2222+proc operator!() {
2323+ let length_type = apply any
2424+ if length_type == false {
2525+ let sublength = from_binary <| apply <| repeat 15 any
2626+ let content_bits = bits::from_array <| apply <| repeat sublength any
2727+ return parse (followed_by eof <| many_1 packet) content_bits
2828+ } else {
2929+ let subpacket_count = from_binary <| apply <| repeat 11 any
3030+ return apply <| repeat subpacket_count packet
3131+ }
3232+}
3333+3434+proc packet!() {
3535+ let version = from_binary <| apply <| repeat 3 any
3636+ let type_id = from_binary <| apply <| repeat 3 any
3737+ return match type_id
3838+ case 0 then reduce (+) <| apply operator
3939+ case 1 then reduce (*) <| apply operator
4040+ case 2 then reduce min <| apply operator
4141+ case 3 then reduce max <| apply operator
4242+ case 4 then from_binary <| apply literal
4343+ case 5 then match apply operator case [a, b] if a > b then 1 else then 0
4444+ case 6 then match apply operator case [a, b] if a < b then 1 else then 0
4545+ case 7 then match apply operator case [a, b] if a == b then 1 else then 0
4646+ else then 'err
4747+}
4848+4949+proc main!() {
5050+ let hex_string = chomp readall!()
5151+ let binary_input = bits::from <| parse hex_integer hex_string
5252+ let real_len = length hex_string * 4
5353+ let real_bits = binary_input <<~ (length binary_input - real_len)
5454+ dbg!(parse packet real_bits)
5555+}