Created
May 25, 2022 19:55
-
-
Save Mu-adventofcode/cc8bedf9ca80f16d0c7e19edcd462aef to your computer and use it in GitHub Desktop.
AoC 2021 16 I iterator version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def versions(data): | |
while data: | |
yield int(data[:3], 2) | |
ptype = int(data[3:6], 2) | |
data = data[6:] | |
if ptype == 4: | |
while data[0] == "1": | |
data = data[5:] | |
data = data[5:] | |
else: | |
ltype = data[0] | |
if ltype == "0": | |
data = data[16:] | |
else: | |
data = data[12:] | |
hexstr = open("input_16.txt").read().strip() | |
packet = "".join(f"{int(ch, 16):04b}" for ch in hexstr) | |
result = sum(v for v in versions(packet)) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment