advent-of-code/2017-python/solutions/day_09.py

34 lines
805 B
Python
Raw Normal View History

2021-11-01 16:40:46 +01:00
import re
from solutions import BaseSolution
class Solution(BaseSolution):
2025-05-06 20:14:58 +02:00
input_file = "09.txt"
2021-11-01 16:40:46 +01:00
def __str__(self):
2025-05-06 20:14:58 +02:00
return "Day 9: Stream Processing"
2021-11-01 16:40:46 +01:00
def solve(self, puzzle_input):
level = 0
groups = []
2025-05-06 20:14:58 +02:00
stream = re.sub(r"!.", "", puzzle_input)
stream = re.sub(r"<[^>]*>", "", stream)
2021-11-01 16:40:46 +01:00
for c in stream:
2025-05-06 20:14:58 +02:00
if c == "{":
2021-11-01 16:40:46 +01:00
level += 1
2025-05-06 20:14:58 +02:00
if c == "}":
2021-11-01 16:40:46 +01:00
groups.append(level)
level -= 1
return sum(groups)
def solve_again(self, puzzle_input):
2025-05-06 20:14:58 +02:00
stream = re.sub(r"!.", "", puzzle_input)
garbage = re.findall(r"<([^>]*)>", stream)
2021-11-01 16:40:46 +01:00
return sum([len(g) for g in garbage])
2025-05-06 20:14:58 +02:00
if __name__ == "__main__":
2021-11-01 16:40:46 +01:00
solution = Solution()
solution.show_results()