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

41 lines
1.3 KiB
Python
Raw Normal View History

2021-11-01 16:40:46 +01:00
from solutions import BaseSolution
class Solution(BaseSolution):
2025-05-06 20:14:58 +02:00
input_file = "08.txt"
2021-11-01 16:40:46 +01:00
registry = {}
def __str__(self):
2025-05-06 20:14:58 +02:00
return "Day 8: I Heard You Like Registers"
2021-11-01 16:40:46 +01:00
def _should_modify(self, x, comp, y):
2025-05-06 20:14:58 +02:00
if comp in ("==", "!=", ">=", "<=", ">", "<"):
return eval("{:d} {} {:d}".format(x, comp, y))
2021-11-01 16:40:46 +01:00
return False
def _update_registry(self, registry, instruction):
r, action, n, _, k, comp, v = instruction.split()
current = registry.get(r, 0)
if self._should_modify(registry.get(k, 0), comp, int(v)):
2025-05-06 20:14:58 +02:00
registry[r] = current + int(n) if action == "inc" else current - int(n)
2021-11-01 16:40:46 +01:00
def solve(self, puzzle_input):
registry = {}
for instruction in puzzle_input.splitlines():
self._update_registry(registry, instruction)
return max(registry.values())
def solve_again(self, puzzle_input):
registry = {}
max_value = 0
for instruction in puzzle_input.splitlines():
self._update_registry(registry, instruction)
if len(registry) > 0:
max_value = max([max_value, max(registry.values())])
return max_value
2025-05-06 20:14:58 +02:00
if __name__ == "__main__":
2021-11-01 16:40:46 +01:00
solution = Solution()
solution.show_results()