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.
No comments:
Post a Comment