summaryrefslogtreecommitdiffstats
path: root/day3/__init__.py
blob: 0510cb34d390ed71985a54c7f8c895b0f7980ac2 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from collections import Counter
from copy import copy
from typing import List

from aoc import BaseAssignment


class Assignment(BaseAssignment):
    def read_input(self, example = False) -> List[str]:
        return list(super().read_input(example))

    @staticmethod
    def flip(bits: str) -> str:
        return bin(~int(bits, 2) + (1 << len(bits))).strip('0b')


class AssignmentOne(Assignment):
    example_result = 198
    @staticmethod
    def convert(input: List[str]) -> List[List[str]]:
        items = []
        for item in input:
            for index, char in enumerate([char for char in item]):
                try:
                    items[index].append(char)
                except IndexError:
                    items.append([char])

        return items

    @staticmethod
    def gamma_rate(input: List[str]) -> str:
        converted = AssignmentOne.convert(input)
        return ''.join([
            Counter(i).most_common(1)[0][0]
            for i
            in converted
        ])

    @staticmethod
    def epsilon_rate(input: List[str]) -> str:
        return Assignment.flip(AssignmentOne.gamma_rate(input))

    def run(self, input: List[str]) -> int:
        return int(self.gamma_rate(input), 2) * int(self.epsilon_rate(input), 2)


class AssignmentTwo(Assignment):
    example_result = 230

    @staticmethod
    def oxygen_generator_rating(input: List[str]):
        bit_length = len(input[0])

        modified_input = copy(input)
        bits = ''
        for i in range(bit_length):
            count = Counter([_[i] for _ in modified_input]).most_common(2)

            most_used = count[0]
            most_used_count = most_used[1]
            least_used = count[-1]
            least_used_count = least_used[1]

            bit = '1' if most_used_count == least_used_count else most_used[0]

            bits += bit
            modified_input = list(filter(lambda _: _[i] == bit, modified_input))

        return bits

    @staticmethod
    def co2_scrubbing_rating(input: List[str]):
        bit_length = len(input[0])

        modified_input = copy(input)
        for i in range(bit_length):
            if len(modified_input) == 1:
                return modified_input[0]

            count = Counter([_[i] for _ in modified_input]).most_common(2)

            most_used = count[0]
            most_used_count = most_used[1]
            least_used = count[-1]
            least_used_count = least_used[1]

            bit = '0' if most_used_count == least_used_count else least_used[0]

            modified_input = list(filter(lambda _: _[i] == bit, modified_input))


    def run(self, input: List[str]) -> int:
        return int(AssignmentTwo.oxygen_generator_rating(input), 2) \
               * int(AssignmentTwo.co2_scrubbing_rating(input), 2)