# ETOOBUSY ðŸš€ minimal blogging for the impatient

# AoC 2021/16 - Bitstream decoding

**TL;DR**

On with Advent of Code puzzle 16 from 2021: good old bit stream decoding

This dayâ€™s puzzle required some care in the decoding algorithm but I did not find it hard.

I had the occasion of using `multi`

methods to cope with a specific
dispatch, instead of using hashes or `if`

s:

```
class BitsNode {
has $.version is built is required;
has $.type is built is required;
has $.value is built is required;
has $.bits is built is required;
method version-sum () {
my $retval = $.version;
if $.type != 4 {
$retval += .version-sum for $.value.List;
}
return $retval;
}
method evaluate () { self.evaluate-by(self.type) }
multi method evaluate-by (4) { self.value }
multi method evaluate-by (0) { [+] self.valueÂ».evaluate }
multi method evaluate-by (1) { [*] self.valueÂ».evaluate }
multi method evaluate-by (2) { self.valueÂ».evaluate.min }
multi method evaluate-by (3) { self.valueÂ».evaluate.max }
multi method evaluate-by (5) { ([>] self.valueÂ».evaluate) ?? 1 !! 0 }
multi method evaluate-by (6) { ([<] self.valueÂ».evaluate) ?? 1 !! 0 }
multi method evaluate-by (7) { ([==] self.valueÂ».evaluate) ?? 1 !! 0 }
}
```

Itâ€™s the implementation of the different operations depending on their code in the hierarchy inside the bit stream. Should I have used inheritance instead? It seemed overkill.

Overall it reminded me when I did ASN1 parsing a long, long time ago. Iâ€™m also a bit disappointed that I could not use a grammar hereâ€¦

Anyway, thatâ€™s all for today and stay safe!

*Comments? Octodon, Twitter, GitHub, Reddit, or drop me a line!*