day16: remove some unnecessary code
This commit is contained in:
parent
978e47db5b
commit
159ce3ca9f
25
day16.py
25
day16.py
@ -4,17 +4,13 @@ from tools.aoc import AOCDay
|
||||
from typing import Any, List
|
||||
|
||||
|
||||
def get_packet(bit_rep: str) -> (int, int, str, int): # version, type, packet, packet_length
|
||||
msg_version = int(bit_rep[:3], 2)
|
||||
def get_packet(bit_rep: str) -> (str, int): # packet, packet_length
|
||||
msg_type = int(bit_rep[3:6], 2)
|
||||
pkg_msg = ""
|
||||
index = 6
|
||||
|
||||
if msg_type == 4:
|
||||
while bit_rep[index] == "1":
|
||||
pkg_msg += bit_rep[index+1:index+5]
|
||||
index += 5
|
||||
pkg_msg += bit_rep[index:index + 5]
|
||||
index += 5
|
||||
else:
|
||||
op_type = bit_rep[index]
|
||||
@ -26,10 +22,10 @@ def get_packet(bit_rep: str) -> (int, int, str, int): # version, type, packet,
|
||||
sub_pkg_count = int(bit_rep[index:index+11], 2)
|
||||
index += 11
|
||||
for x in range(sub_pkg_count):
|
||||
_, _, _, sub_pkg_len = get_packet(bit_rep[index:])
|
||||
_, sub_pkg_len = get_packet(bit_rep[index:])
|
||||
index += sub_pkg_len
|
||||
|
||||
return msg_version, msg_type, bit_rep[:index], index
|
||||
return bit_rep[:index], index
|
||||
|
||||
|
||||
def get_subpackages(packet: str) -> List[str]:
|
||||
@ -43,14 +39,14 @@ def get_subpackages(packet: str) -> List[str]:
|
||||
sub_pkg_len = int(packet[7:22], 2)
|
||||
sub_packages = packet[22:22+sub_pkg_len]
|
||||
while "1" in sub_packages:
|
||||
_, _, sub_packet, sub_pkg_len = get_packet(sub_packages)
|
||||
sub_packet, sub_pkg_len = get_packet(sub_packages)
|
||||
sub_packages = sub_packages[sub_pkg_len:]
|
||||
plist.append(sub_packet)
|
||||
else:
|
||||
sub_pkg_count = int(packet[7:18], 2)
|
||||
index = 18
|
||||
for x in range(sub_pkg_count):
|
||||
_, _, sub_packet, sub_pkg_len = get_packet(packet[index:])
|
||||
sub_packet, sub_pkg_len = get_packet(packet[index:])
|
||||
index += sub_pkg_len
|
||||
plist.append(sub_packet)
|
||||
|
||||
@ -71,7 +67,8 @@ def get_versions(packet: str) -> int:
|
||||
|
||||
def get_value(packet: str) -> int:
|
||||
while "1" in packet:
|
||||
pkg_version, pkg_type, this_packet, pkg_len = get_packet(packet)
|
||||
this_packet, pkg_len = get_packet(packet)
|
||||
pkg_type = int(this_packet[3:6], 2)
|
||||
packet = packet[pkg_len:]
|
||||
if pkg_type == 0:
|
||||
return sum(get_value(p) for p in get_subpackages(this_packet))
|
||||
@ -109,13 +106,7 @@ class Day(AOCDay):
|
||||
for c in self.getInput():
|
||||
bit_rep += "{0:04b}".format(int(c, 16))
|
||||
|
||||
version_sum = 0
|
||||
while "1" in bit_rep:
|
||||
pkg_version, pkg_type, packet, pkg_len = get_packet(bit_rep)
|
||||
bit_rep = bit_rep[pkg_len:]
|
||||
version_sum += get_versions(packet)
|
||||
|
||||
return version_sum
|
||||
return get_versions(bit_rep)
|
||||
|
||||
def part2(self) -> Any:
|
||||
bit_rep = ""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user