April 29, 2024

Solution for Advent of Code 2016 - Day 16: Dragon Checksum

Link to the puzzle text

Part 1

In this puzzle we first produce a string according to a simple rule and then reduce the string to a checksum. The production part takes the binary string, adds a '0' and then its reversed invertion. This is repeated until the string reaches a certain length (here 272). The checksum takes two adjacent characters in the string and xors them. This is repeated until the checksum has an even length.

The production part was a simple function using maketrans:

def transform(s):
a = s
b = s.translate(str.maketrans("01", "10"))
b = "".join(reversed(b))
return a + "0" + b

The checksum was similar short:

def checksum(s):
s_new = ""
for a, b in zip(s[::2], s[1::2]):
if a == b:
s_new += "1"
else:
s_new += "0"
return s_new

This checksum function was then called in a loop until its length was even. The answer was the result of this recursive checksum function.

Part 2

In part 2, we follow the same rules just until a longer target length for the production. The result for me was the two-complement of the previous answer.

Link to my solutions

No comments:

Post a Comment