diff options
| author | 2023-12-03 13:46:27 +0100 | |
|---|---|---|
| committer | 2023-12-03 13:46:27 +0100 | |
| commit | d1bbbd066202b7e8a6ef75ef961de20ef4331a40 (patch) | |
| tree | 84f7459a6fd70966d1d22ea11a1c3251fcb20551 /day1/__init__.py | |
| parent | 6794fc500be97c45c98fe39ed31d91cbc40dcd19 (diff) | |
| download | 2023-d1bbbd066202b7e8a6ef75ef961de20ef4331a40.tar.gz 2023-d1bbbd066202b7e8a6ef75ef961de20ef4331a40.tar.bz2 2023-d1bbbd066202b7e8a6ef75ef961de20ef4331a40.zip | |
WIP
Diffstat (limited to 'day1/__init__.py')
| -rw-r--r-- | day1/__init__.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/day1/__init__.py b/day1/__init__.py new file mode 100644 index 0000000..ee66fd3 --- /dev/null +++ b/day1/__init__.py | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from abc import ABC | ||
| 3 | from enum import Enum | ||
| 4 | from typing import Iterator | ||
| 5 | |||
| 6 | from aoc import BaseAssignment, I, T | ||
| 7 | |||
| 8 | |||
| 9 | class Assignment(BaseAssignment, ABC): | ||
| 10 | def run(self, input: Iterator[list[int]]) -> int: | ||
| 11 | input = list(input) | ||
| 12 | print(input) | ||
| 13 | return sum([ | ||
| 14 | int(f'{item[0]}{item[-1]}') | ||
| 15 | for item in input | ||
| 16 | ]) | ||
| 17 | |||
| 18 | |||
| 19 | class AssignmentOne(Assignment): | ||
| 20 | example_result = 142 | ||
| 21 | |||
| 22 | def parse_item(self, item: str) -> list[int]: | ||
| 23 | return [int(i) for i in item if i.isdigit()] | ||
| 24 | |||
| 25 | |||
| 26 | class AssignmentTwo(Assignment): | ||
| 27 | example_result = 281 | ||
| 28 | |||
| 29 | class Numbers(Enum): | ||
| 30 | one = 1 | ||
| 31 | two = 2 | ||
| 32 | three = 3 | ||
| 33 | four = 4 | ||
| 34 | five = 5 | ||
| 35 | six = 6 | ||
| 36 | seven = 7 | ||
| 37 | eight = 8 | ||
| 38 | nine = 9 | ||
| 39 | |||
| 40 | @staticmethod | ||
| 41 | def _parse_item(item: str): | ||
| 42 | numbers = { | ||
| 43 | index: number | ||
| 44 | for index, number in [ | ||
| 45 | (item.find(number.name), number.value) | ||
| 46 | for number in AssignmentTwo.Numbers | ||
| 47 | ] | ||
| 48 | if index >= 0 | ||
| 49 | } | ||
| 50 | |||
| 51 | for index, i in enumerate(item): | ||
| 52 | if i.isdigit(): | ||
| 53 | numbers[index] = int(i) | ||
| 54 | |||
| 55 | return [ | ||
| 56 | value | ||
| 57 | for key, value | ||
| 58 | in sorted( | ||
| 59 | numbers.items(), | ||
| 60 | key=lambda item: item[0] | ||
| 61 | ) | ||
| 62 | ] | ||
| 63 | |||
| 64 | def parse_item(self, item: str) -> list[int]: | ||
| 65 | return self._parse_item(item) | ||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | |||
