diff options
| author | 2020-12-14 11:35:46 +0100 | |
|---|---|---|
| committer | 2020-12-14 11:35:46 +0100 | |
| commit | 9d38cb75c2e23a173adc7e390777c50361c31436 (patch) | |
| tree | 361e34d71d1bac3cc57c5d5a3ec2ab3d4d42ae3c /day8/__init__.py | |
| parent | 2dae8ff6da421943e26af83866765a024c8f6650 (diff) | |
| download | 2021-9d38cb75c2e23a173adc7e390777c50361c31436.tar.gz 2021-9d38cb75c2e23a173adc7e390777c50361c31436.tar.bz2 2021-9d38cb75c2e23a173adc7e390777c50361c31436.zip | |
Added day 8
Diffstat (limited to 'day8/__init__.py')
| -rw-r--r-- | day8/__init__.py | 75 |
1 files changed, 75 insertions, 0 deletions
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 @@ | |||
| 1 | from copy import copy | ||
| 2 | from typing import Generator, Iterator, Any, List, Tuple | ||
| 3 | |||
| 4 | from aoc import BaseAssignment | ||
| 5 | |||
| 6 | |||
| 7 | class Assignment(BaseAssignment): | ||
| 8 | @staticmethod | ||
| 9 | def acc(line, accumulated_value, value): | ||
| 10 | return line + 1, accumulated_value + value | ||
| 11 | |||
| 12 | @staticmethod | ||
| 13 | def jmp(line, accumulated_value, value): | ||
| 14 | return line + value, accumulated_value | ||
| 15 | |||
| 16 | @staticmethod | ||
| 17 | def nop(line, accumulated_value, value): | ||
| 18 | return line + 1, accumulated_value | ||
| 19 | |||
| 20 | def read_input(self, example=False) -> List: | ||
| 21 | return list(super().read_input(example)) | ||
| 22 | |||
| 23 | def parse_item(self, item: str) -> Tuple: | ||
| 24 | instruction, value = item.split(' ') | ||
| 25 | |||
| 26 | return getattr(self, instruction), int(value) | ||
| 27 | |||
| 28 | def run(self, input: List) -> int: | ||
| 29 | executed_lines = set() | ||
| 30 | accumulated_value = 0 | ||
| 31 | line = 0 | ||
| 32 | |||
| 33 | while True: | ||
| 34 | if line in executed_lines: | ||
| 35 | raise Exception( | ||
| 36 | f'Loop detected at line: {line}. ' | ||
| 37 | f'Accumulated value: {accumulated_value}' | ||
| 38 | ) | ||
| 39 | |||
| 40 | try: | ||
| 41 | instruction, value = input[line] | ||
| 42 | except IndexError: | ||
| 43 | break | ||
| 44 | |||
| 45 | executed_lines.add(line) | ||
| 46 | line, accumulated_value = instruction( | ||
| 47 | line, accumulated_value, value | ||
| 48 | ) | ||
| 49 | |||
| 50 | return accumulated_value | ||
| 51 | |||
| 52 | |||
| 53 | class AssignmentOne(Assignment): | ||
| 54 | pass | ||
| 55 | |||
| 56 | |||
| 57 | class AssignmentTwo(Assignment): | ||
| 58 | def patch_instructions(self, input: List) -> Generator: | ||
| 59 | for index, (instruction, value) in enumerate(input): | ||
| 60 | if instruction is self.acc: | ||
| 61 | continue | ||
| 62 | |||
| 63 | patched_input = copy(input) | ||
| 64 | patched_input[index] = ( | ||
| 65 | self.nop if instruction is self.jmp else self.jmp, | ||
| 66 | value | ||
| 67 | ) | ||
| 68 | yield patched_input | ||
| 69 | |||
| 70 | def run(self, input: List) -> int: | ||
| 71 | for patched_input in self.patch_instructions(input): | ||
| 72 | try: | ||
| 73 | return super().run(patched_input) | ||
| 74 | except Exception: | ||
| 75 | continue | ||
