--- title: "Advent of Code 2021 - Day 16" date: 2021-12-18T01:44:09+01:00 day: 16 stars: 0 --- Today's challenge is [here](https://adventofcode.com/2021/day/16). My first attempt at solving this puzzle was to use a library called [construct](https://pypi.org/project/construct/) to parse the data, however it turned out to be too hard for me xD I managed to create a parser for the literals: ```python literal = Bitwise(Aligned(8, Struct( "version" / BitsInteger(3), "type" / Const(0b100, BitsInteger(3)), "sequence" / RepeatUntil( lambda x, _, __: x.flag == 0, Struct( "flag" / Flag, "data" / Nibble, ), ), "value" / Computed(lambda ctx: reduce(lambda acc, n: (acc << 4) + n.data, ctx.sequence, 0)), ))) assert(literal.parse(b"\xD2\xFE\x28").value == 2021) ``` This does work as excepted, but given that the entire problem is a tree of structures, I might just write a bunch of functions to parse the data instead. In construct the recursion has to be done by using `LazyBound` as far as I can tell, which doesn't really work with `Bitwise`.