The same code solved Part A and B of this problem, because my word size is 64 bits and that was enough to track the number of lanternfish after 256 days. For my puzzle input the answer took about 41 bits. Good thing too, because I really didn't want to implement an arbitrary-precision math library.
\ No newline at end of file
The same code solved Part A and B of this problem, because my word size is 64 bits and that was enough to track the number of lanternfish after 256 days. For my puzzle input the answer took about 41 bits. Good thing too, because I really didn't want to implement an arbitrary-precision math library.
# Day 7
# Day 8
## Part A
First throw away everything before the pipe character (`|`, ascii 124), then iterate through the remaining words and count how many of them have length 2, 3, 4 or 7. If a pipe character is not found in the line, this must be the end of the input so we end the program.
## Part B
Each digit has seven segments a through g. I'll pack these into the low
seven bits of a byte, where 1 means the segment is used and 0 means it is not.
```
gfedcba
00000000
```
Some digits can be recognized immediately from how many segments they use.
```
segments value
(2) ------> #1
(3) ------> #7
(4) ------> #4
(7) ------> #8
```
The remaining six digits will be ambiguous for now. I will throw them into a pool, then define a "find" operation that searches the pool and removes/returns the digit matching a given bitmask. To come up with these search terms, I will use binary combinations of the digits already known. As digits are found, they are removed from the pool of ambiguous digits so subsequent searches get easier.
Here is my algorithm:
```
#7 #4 or -----> find --> #9
#8 #1 xor ----> find --> #6
#8 #4 #1 xor -> find --> #0
#9 #6 and ----> find --> #5
#8 #4 xor ----> find --> #2
and the last digit is #3
```
Here is a diagram showing why it works:

Finally, the data structures I am using are extremely simple: