website/content/wiki/aoc/2021/16.md
2024-04-13 15:26:52 +02:00

1.1 KiB

title date day stars
AoC 2021 - Day 16 2021-12-18T01:44:09+01:00 16 0

Today's challenge is here.

My first attempt at solving this puzzle was to use a library called 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:

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.