2016: d09: ex2: add solution

This commit is contained in:
Bruno BELANYI 2025-05-19 02:25:31 +01:00
parent d8242fb07f
commit d0c5d60c3e

39
2016/d09/ex2/ex2.py Executable file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env python
import sys
from collections.abc import Iterator
def solve(input: str) -> int:
def decompress(input: str) -> int:
def helper() -> Iterator[int]:
i = 0
is_marker = False
while i < len(input):
if is_marker:
j = input.find(")", i)
else:
j = input.find("(", i)
if j == -1:
j = len(input)
if is_marker:
length, repeat = map(int, input[i + 1 : j].split("x"))
yield decompress(input[j + 1 : j + length + 1]) * repeat
i = j + length + 1
else:
yield j - i
i = j
is_marker = not is_marker
return sum(helper())
return decompress(input.strip())
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()