53 lines
1.1 KiB
Python
53 lines
1.1 KiB
Python
from tools.aoc import AOCDay
|
|
from typing import Any
|
|
|
|
|
|
def get_decompressed_len(zipped: str, p2: bool = False) -> int:
|
|
length = 0
|
|
x = 0
|
|
while x < len(zipped):
|
|
if zipped[x] != "(":
|
|
length += 1
|
|
x += 1
|
|
continue
|
|
|
|
x += 1
|
|
marker = ""
|
|
while zipped[x] != ")":
|
|
marker += zipped[x]
|
|
x += 1
|
|
|
|
x += 1
|
|
c_count, repeats = map(int, marker.split("x"))
|
|
if not p2:
|
|
length += c_count * repeats
|
|
else:
|
|
repeator = zipped[x : x + c_count]
|
|
length += repeats * get_decompressed_len(repeator, p2=True)
|
|
|
|
x += c_count
|
|
|
|
return length
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
(183269, "input9"),
|
|
],
|
|
[
|
|
(11317278863, "input9"),
|
|
],
|
|
]
|
|
|
|
def part1(self) -> Any:
|
|
return get_decompressed_len(self.getInput())
|
|
|
|
def part2(self) -> Any:
|
|
return get_decompressed_len(self.getInput(), True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
day = Day(2016, 9)
|
|
day.run(verbose=True)
|