diff options
| author | 2020-12-15 13:14:58 +0100 | |
|---|---|---|
| committer | 2020-12-15 13:14:58 +0100 | |
| commit | 37aa8eec0498d7e8491084711132f16db9129a39 (patch) | |
| tree | bfcf5bd4a5fe3d6a364db2ea2b65d60112d879a8 | |
| parent | 9d38cb75c2e23a173adc7e390777c50361c31436 (diff) | |
| download | 2021-37aa8eec0498d7e8491084711132f16db9129a39.tar.gz 2021-37aa8eec0498d7e8491084711132f16db9129a39.tar.bz2 2021-37aa8eec0498d7e8491084711132f16db9129a39.zip | |
Added day 9
| -rw-r--r-- | aoc/__main__.py | 17 | ||||
| -rw-r--r-- | day9/__init__.py | 63 | ||||
| -rw-r--r-- | day9/example.txt | 20 | ||||
| -rw-r--r-- | day9/input.txt | 1000 |
4 files changed, 1098 insertions, 2 deletions
diff --git a/aoc/__main__.py b/aoc/__main__.py index 8c6a21e..efb215d 100644 --- a/aoc/__main__.py +++ b/aoc/__main__.py | |||
| @@ -2,6 +2,8 @@ import argparse | |||
| 2 | import os | 2 | import os |
| 3 | import sys | 3 | import sys |
| 4 | import importlib | 4 | import importlib |
| 5 | from typing import Tuple, List | ||
| 6 | |||
| 5 | 7 | ||
| 6 | def day(assignment_part: str) -> str: | 8 | def day(assignment_part: str) -> str: |
| 7 | return { | 9 | return { |
| @@ -9,6 +11,9 @@ def day(assignment_part: str) -> str: | |||
| 9 | '2': 'Two', | 11 | '2': 'Two', |
| 10 | }[assignment_part] | 12 | }[assignment_part] |
| 11 | 13 | ||
| 14 | def kwargs(kwarg: str) -> List: | ||
| 15 | return kwarg.split('=') | ||
| 16 | |||
| 12 | parser = argparse.ArgumentParser(description='Advent of Code') | 17 | parser = argparse.ArgumentParser(description='Advent of Code') |
| 13 | 18 | ||
| 14 | parser.add_argument('day', type=str, help='Assignment day') | 19 | parser.add_argument('day', type=str, help='Assignment day') |
| @@ -17,6 +22,7 @@ parser.add_argument( | |||
| 17 | help='Assingment part. Defaults to one.' | 22 | help='Assingment part. Defaults to one.' |
| 18 | ) | 23 | ) |
| 19 | parser.add_argument('--example', default=False, action='store_true') | 24 | parser.add_argument('--example', default=False, action='store_true') |
| 25 | parser.add_argument('kwargs', type=kwargs, nargs='*') | ||
| 20 | 26 | ||
| 21 | 27 | ||
| 22 | if __name__ == '__main__': | 28 | if __name__ == '__main__': |
| @@ -24,6 +30,13 @@ if __name__ == '__main__': | |||
| 24 | assignment_day = importlib.import_module(args.day) | 30 | assignment_day = importlib.import_module(args.day) |
| 25 | 31 | ||
| 26 | Assignment = getattr(assignment_day, f'Assignment{args.part}') | 32 | Assignment = getattr(assignment_day, f'Assignment{args.part}') |
| 27 | assignment = Assignment(path=os.path.dirname(assignment_day.__file__)) | 33 | assignment = Assignment( |
| 34 | path=os.path.dirname(assignment_day.__file__), | ||
| 35 | **dict(args.kwargs) | ||
| 36 | ) | ||
| 28 | 37 | ||
| 29 | print(assignment.run(input=assignment.read_input(example=args.example))) | 38 | print( |
| 39 | assignment.run( | ||
| 40 | input=assignment.read_input(example=args.example), | ||
| 41 | ) | ||
| 42 | ) | ||
diff --git a/day9/__init__.py b/day9/__init__.py new file mode 100644 index 0000000..0cf3770 --- /dev/null +++ b/day9/__init__.py | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | from typing import Any, Iterator, List | ||
| 2 | |||
| 3 | from aoc import BaseAssignment | ||
| 4 | |||
| 5 | |||
| 6 | class Assignment(BaseAssignment): | ||
| 7 | def __init__(self, path, preamble_length=25): | ||
| 8 | super().__init__(path) | ||
| 9 | self.preamble_length = int(preamble_length) | ||
| 10 | |||
| 11 | def read_input(self, example=False) -> List: | ||
| 12 | return list(super(Assignment, self).read_input(example)) | ||
| 13 | |||
| 14 | def parse_item(self, item: str) -> Any: | ||
| 15 | return int(item) | ||
| 16 | |||
| 17 | |||
| 18 | class AssignmentOne(Assignment): | ||
| 19 | def find_invalid(self, input: List, preamble_length: int = 25) -> int: | ||
| 20 | preamble = sorted(input[:preamble_length]) | ||
| 21 | to_check = input[preamble_length] | ||
| 22 | |||
| 23 | front_position = 0 | ||
| 24 | end_position = preamble_length - 1 | ||
| 25 | |||
| 26 | while True: | ||
| 27 | if front_position is end_position: | ||
| 28 | return to_check | ||
| 29 | |||
| 30 | sum = preamble[front_position] + preamble[end_position] | ||
| 31 | |||
| 32 | if sum > to_check: | ||
| 33 | end_position -= 1 | ||
| 34 | elif sum < to_check: | ||
| 35 | front_position += 1 | ||
| 36 | else: | ||
| 37 | break | ||
| 38 | |||
| 39 | return self.find_invalid(input[1:], preamble_length=preamble_length) | ||
| 40 | |||
| 41 | def run(self, input: List) -> int: | ||
| 42 | return self.find_invalid( | ||
| 43 | input, preamble_length=int(self.preamble_length)) | ||
| 44 | |||
| 45 | |||
| 46 | class AssignmentTwo(AssignmentOne): | ||
| 47 | def find_weakness(self, input: List, invalid_nr: int) -> int: | ||
| 48 | for index, current_num in enumerate(input): | ||
| 49 | if index < 2: | ||
| 50 | continue | ||
| 51 | |||
| 52 | items_to_check = input[:index] | ||
| 53 | sum_list = sum(items_to_check) | ||
| 54 | |||
| 55 | if sum_list > invalid_nr: | ||
| 56 | break | ||
| 57 | elif sum_list == invalid_nr: | ||
| 58 | return min(items_to_check) + max(items_to_check) | ||
| 59 | |||
| 60 | return self.find_weakness(input[1:], invalid_nr) | ||
| 61 | |||
| 62 | def run(self, input: List) -> int: | ||
| 63 | return self.find_weakness(input, super().run(input)) | ||
diff --git a/day9/example.txt b/day9/example.txt new file mode 100644 index 0000000..cda4246 --- /dev/null +++ b/day9/example.txt | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | 35 | ||
| 2 | 20 | ||
| 3 | 15 | ||
| 4 | 25 | ||
| 5 | 47 | ||
| 6 | 40 | ||
| 7 | 62 | ||
| 8 | 55 | ||
| 9 | 65 | ||
| 10 | 95 | ||
| 11 | 102 | ||
| 12 | 117 | ||
| 13 | 150 | ||
| 14 | 182 | ||
| 15 | 127 | ||
| 16 | 219 | ||
| 17 | 299 | ||
| 18 | 277 | ||
| 19 | 309 | ||
| 20 | 576 \ No newline at end of file | ||
diff --git a/day9/input.txt b/day9/input.txt new file mode 100644 index 0000000..e9932d5 --- /dev/null +++ b/day9/input.txt | |||
| @@ -0,0 +1,1000 @@ | |||
| 1 | 18 | ||
| 2 | 19 | ||
| 3 | 46 | ||
| 4 | 14 | ||
| 5 | 29 | ||
| 6 | 45 | ||
| 7 | 40 | ||
| 8 | 47 | ||
| 9 | 25 | ||
| 10 | 43 | ||
| 11 | 36 | ||
| 12 | 22 | ||
| 13 | 21 | ||
| 14 | 4 | ||
| 15 | 32 | ||
| 16 | 33 | ||
| 17 | 37 | ||
| 18 | 38 | ||
| 19 | 26 | ||
| 20 | 2 | ||
| 21 | 42 | ||
| 22 | 15 | ||
| 23 | 5 | ||
| 24 | 13 | ||
| 25 | 31 | ||
| 26 | 9 | ||
| 27 | 6 | ||
| 28 | 30 | ||
| 29 | 7 | ||
| 30 | 14 | ||
| 31 | 10 | ||
| 32 | 8 | ||
| 33 | 69 | ||
| 34 | 11 | ||
| 35 | 12 | ||
| 36 | 25 | ||
| 37 | 16 | ||
| 38 | 17 | ||
| 39 | 22 | ||
| 40 | 19 | ||
| 41 | 18 | ||
| 42 | 20 | ||
| 43 | 51 | ||
| 44 | 21 | ||
| 45 | 24 | ||
| 46 | 23 | ||
| 47 | 26 | ||
| 48 | 15 | ||
| 49 | 60 | ||
| 50 | 13 | ||
| 51 | 29 | ||
| 52 | 27 | ||
| 53 | 44 | ||
| 54 | 42 | ||
| 55 | 28 | ||
| 56 | 38 | ||
| 57 | 30 | ||
| 58 | 36 | ||
| 59 | 31 | ||
| 60 | 32 | ||
| 61 | 33 | ||
| 62 | 34 | ||
| 63 | 35 | ||
| 64 | 37 | ||
| 65 | 55 | ||
| 66 | 39 | ||
| 67 | 40 | ||
| 68 | 70 | ||
| 69 | 41 | ||
| 70 | 43 | ||
| 71 | 78 | ||
| 72 | 58 | ||
| 73 | 73 | ||
| 74 | 101 | ||
| 75 | 60 | ||
| 76 | 56 | ||
| 77 | 57 | ||
| 78 | 75 | ||
| 79 | 59 | ||
| 80 | 67 | ||
| 81 | 71 | ||
| 82 | 61 | ||
| 83 | 68 | ||
| 84 | 110 | ||
| 85 | 69 | ||
| 86 | 80 | ||
| 87 | 72 | ||
| 88 | 112 | ||
| 89 | 76 | ||
| 90 | 79 | ||
| 91 | 144 | ||
| 92 | 129 | ||
| 93 | 114 | ||
| 94 | 84 | ||
| 95 | 99 | ||
| 96 | 113 | ||
| 97 | 132 | ||
| 98 | 115 | ||
| 99 | 148 | ||
| 100 | 116 | ||
| 101 | 118 | ||
| 102 | 120 | ||
| 103 | 128 | ||
| 104 | 126 | ||
| 105 | 187 | ||
| 106 | 130 | ||
| 107 | 190 | ||
| 108 | 152 | ||
| 109 | 229 | ||
| 110 | 141 | ||
| 111 | 151 | ||
| 112 | 155 | ||
| 113 | 299 | ||
