From 9d38cb75c2e23a173adc7e390777c50361c31436 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Mon, 14 Dec 2020 11:35:46 +0100 Subject: Added day 8 --- day8/__init__.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 day8/__init__.py (limited to 'day8/__init__.py') diff --git a/day8/__init__.py b/day8/__init__.py new file mode 100644 index 0000000..c6d086a --- /dev/null +++ b/day8/__init__.py @@ -0,0 +1,75 @@ +from copy import copy +from typing import Generator, Iterator, Any, List, Tuple + +from aoc import BaseAssignment + + +class Assignment(BaseAssignment): + @staticmethod + def acc(line, accumulated_value, value): + return line + 1, accumulated_value + value + + @staticmethod + def jmp(line, accumulated_value, value): + return line + value, accumulated_value + + @staticmethod + def nop(line, accumulated_value, value): + return line + 1, accumulated_value + + def read_input(self, example=False) -> List: + return list(super().read_input(example)) + + def parse_item(self, item: str) -> Tuple: + instruction, value = item.split(' ') + + return getattr(self, instruction), int(value) + + def run(self, input: List) -> int: + executed_lines = set() + accumulated_value = 0 + line = 0 + + while True: + if line in executed_lines: + raise Exception( + f'Loop detected at line: {line}. ' + f'Accumulated value: {accumulated_value}' + ) + + try: + instruction, value = input[line] + except IndexError: + break + + executed_lines.add(line) + line, accumulated_value = instruction( + line, accumulated_value, value + ) + + return accumulated_value + + +class AssignmentOne(Assignment): + pass + + +class AssignmentTwo(Assignment): + def patch_instructions(self, input: List) -> Generator: + for index, (instruction, value) in enumerate(input): + if instruction is self.acc: + continue + + patched_input = copy(input) + patched_input[index] = ( + self.nop if instruction is self.jmp else self.jmp, + value + ) + yield patched_input + + def run(self, input: List) -> int: + for patched_input in self.patch_instructions(input): + try: + return super().run(patched_input) + except Exception: + continue -- cgit v1.2.3