Update day 16 solution

This commit is contained in:
2021-12-16 16:41:06 +01:00
parent cbd7e79917
commit c365cc5b64

View File

@@ -73,38 +73,23 @@ def calc_result(packets):
return res return res
def part_1(input): def solve(input):
result = 0 result_p1 = 0
line = input[0].rstrip().strip('0') line = input[0].rstrip().strip('0')
msg = bin(int(line, 16)) msg = bin(int(line, 16))[2:]
msg = msg[2:] while len(msg) < len(line) * 4:
while len(msg) % 4:
msg = '0' + msg msg = '0' + msg
packets = [] packets = []
next = 0 next = 0
while -1 != next: while -1 != next:
next, result = get_next_packet(msg, next, packets, result) next, result_p1 = get_next_packet(msg, next, packets, result_p1)
print("Part 1 result:", result) result_p2 = calc_result(packets)
print("Part 1 result:", result_p1)
print("Part 2 result:", result_p2)
def part_2(input):
result = 0
line = input[0].rstrip().strip('0')
msg = bin(int(line, 16))
msg = msg[2:]
while len(msg) % 4:
msg = '0' + msg
packets = []
next = 0
while -1 != next:
next, _ = get_next_packet(msg, next, packets)
result = calc_result(packets)
print("Part 2 result:", result)
input = list() input = list()
p = Path(__file__).with_name('input.txt') p = Path(__file__).with_name('input.txt')
with open(p) as f: with open(p) as f:
input = f.readlines() input = f.readlines()
part_1(input) solve(input)
part_2(input)