summaryrefslogtreecommitdiffstats
path: root/day1/__init__.py
blob: ee66fd358ff3cbe3630550740a66c49b8444cbd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
from abc import ABC
from enum import Enum
from typing import Iterator

from aoc import BaseAssignment, I, T


class Assignment(BaseAssignment, ABC):
    def run(self, input: Iterator[list[int]]) -> int:
        input = list(input)
        print(input)
        return sum([
            int(f'{item[0]}{item[-1]}')
            for item in input
        ])


class AssignmentOne(Assignment):
    example_result = 142

    def parse_item(self, item: str) -> list[int]:
        return [int(i) for i in item if i.isdigit()]


class AssignmentTwo(Assignment):
    example_result = 281

    class Numbers(Enum):
        one = 1
        two = 2
        three = 3
        four = 4
        five = 5
        six = 6
        seven = 7
        eight = 8
        nine = 9

    @staticmethod
    def _parse_item(item: str):
        numbers = {
            index: number
            for index, number in [
                (item.find(number.name), number.value)
                for number in AssignmentTwo.Numbers
            ]
            if index >= 0
        }

        for index, i in enumerate(item):
            if i.isdigit():
                numbers[index] = int(i)

        return [
            value
            for key, value
            in sorted(
                numbers.items(),
                key=lambda item: item[0]
            )
        ]

    def parse_item(self, item: str) -> list[int]:
        return self._parse_item(item)