summaryrefslogtreecommitdiffstats
path: root/day3
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@vanderlee.io>2021-12-03 16:53:55 +0100
committerGravatar Tom van der Lee <tom@vanderlee.io>2021-12-03 16:53:55 +0100
commit3df1c0af382c6c04370b879c7168b46d833c761f (patch)
treeaf64d825d07a9863a6f317144c2da71e4d06c6c5 /day3
parentb062e0f2abc189f352e019bac6854f469e9dfc8c (diff)
download2021-3df1c0af382c6c04370b879c7168b46d833c761f.tar.gz
2021-3df1c0af382c6c04370b879c7168b46d833c761f.tar.bz2
2021-3df1c0af382c6c04370b879c7168b46d833c761f.zip
Day3 p2
Diffstat (limited to 'day3')
-rw-r--r--day3/__init__.py96
-rw-r--r--day3/example.txt12
-rw-r--r--day3/input.txt1000
3 files changed, 1108 insertions, 0 deletions
diff --git a/day3/__init__.py b/day3/__init__.py
new file mode 100644
index 0000000..29f4ab3
--- /dev/null
+++ b/day3/__init__.py
@@ -0,0 +1,96 @@
1from collections import Counter
2from copy import copy
3from dataclasses import dataclass
4from typing import Iterator, List
5
6from aoc import BaseAssignment
7
8class Assignment(BaseAssignment):
9 def read_input(self, example = False) -> List[str]:
10 return list(super().read_input(example))
11
12 @staticmethod
13 def flip(bits: str) -> str:
14 return bin(~int(bits, 2) + (1 << len(bits))).strip('0b')
15
16
17class AssignmentOne(Assignment):
18 example_result = 198
19 @staticmethod
20 def convert(input: List[str]) -> List[List[str]]:
21 items = []
22 for item in input:
23 for index, char in enumerate([char for char in item]):
24 try:
25 items[index].append(char)
26 except IndexError:
27 items.append([char])
28
29 return items
30
31 @staticmethod
32 def gamma_rate(input: List[str]) -> str:
33 converted = AssignmentOne.convert(input)
34 return ''.join([
35 Counter(i).most_common(1)[0][0]
36 for i
37 in converted
38 ])
39
40 @staticmethod
41 def epsilon_rate(input: List[str]) -> str:
42 return Assignment.flip(AssignmentOne.gamma_rate(input))
43
44 def run(self, input: List[str]) -> int:
45 return int(self.gamma_rate(input), 2) * int(self.epsilon_rate(input), 2)
46
47
48class AssignmentTwo(Assignment):
49 example_result = 230
50
51 @staticmethod
52 def oxygen_generator_rating(input: List[str]):
53 bit_length = len(input[0])
54
55 modified_input = copy(input)
56 bits = ''
57 for i in range(bit_length):
58 count = Counter([_[i] for _ in modified_input]).most_common(2)
59
60 most_used = count[0]
61 most_used_count = most_used[1]
62 least_used = count[-1]
63 least_used_count = least_used[1]
64
65 bit = '1' if most_used_count == least_used_count else most_used[0]
66
67 bits += bit
68 modified_input = list(filter(lambda _: _[i] == bit, modified_input))
69
70 return bits
71
72 @staticmethod
73 def co2_scrubbing_rating(input: List[str]):
74 bit_length = len(input[0])
75
76 modified_input = copy(input)
77 for i in range(bit_length):
78 if len(modified_input) == 1:
79 return modified_input[0]
80
81 count = Counter([_[i] for _ in modified_input]).most_common(2)
82
83 most_used = count[0]
84 most_used_count = most_used[1]
85 least_used = count[-1]
86 least_used_count = least_used[1]
87
88 bit = '0' if most_used_count == least_used_count else least_used[0]
89
90 modified_input = list(filter(lambda _: _[i] == bit, modified_input))
91
92
93 def run(self, input: List[str]) -> int:
94 return int(AssignmentTwo.oxygen_generator_rating(input), 2) \
95 * int(AssignmentTwo.co2_scrubbing_rating(input), 2)
96
diff --git a/day3/example.txt b/day3/example.txt
new file mode 100644
index 0000000..665fd57
--- /dev/null
+++ b/day3/example.txt
@@ -0,0 +1,12 @@
100100
211110
310110
410111
510101
601111
700111
811100
910000
1011001
1100010
1201010 \ No newline at end of file
diff --git a/day3/input.txt b/day3/input.txt
new file mode 100644
index 0000000..16977ec
--- /dev/null
+++ b/day3/input.txt
@@ -0,0 +1,1000 @@
1000110000001
2011011001101
3001101100111
4001101011001
5110111011101
6110011101010
7111101010001
8010100111101
9011000011000
10001110110011
11001100010110
12110111101100
13110001111100
14001011111100
15000000011010
16110101100111
17011000011111
18011000000111
19011111000110
20100101110111
21010101001110
22111101000011
23010010010110
24100100011111
25101011001110
26001111110000
27110000011111
28110000011000
29011001111100
30010010001101
31000111001110
32110111001110
33110001010101
34100111011001
35000101110000
36110001011100
37111101010010
38101011000001
39001101001111
40110111101010
41101111111000
42110101000110
43011111001001
44001110100001
45010100110111
46110100000110
47101010110010
48100100101110
49101111011110
50000110110101
51011011110101
52111001011110
53110110100111
54000100010001
55001101010110
56100011000110
57001110010010
58010111110111
59011010011101
60110000011100
61010100001001
62000110100000
63101001010000
64000001110000
65101110010011
66010011100111
67010011011000
68110111011111
69000111010010
70101010111010
71111001100100
72101110100011
73111101111110
74010111111000
75010010001111
76110010000011
77001110000010
78100101111110
79000100101001
80101101010000
81111111010000
82101010011000
83011100100001
84011101101000
85001010010100
86010010100011
87110011111111
88110100011001
89111010011110
90011110001101
91011010011100
92100100000001
93111111001010
94110100110011
95110100011111
96100010001110
97101000111100
98100001110010
99110101010011
100101010011101
101011010011010
102101110101101
103001100011010
104101001010101
105101010000100
106000110101010
107100000111100
108000111111100
109000001001011
110010010111100
111011000111001
112111010101100
113010011100010
114010100110001
115001100111011
116101100001111
117111010101111
118010001000011
119000001100010
120000100001011
121100110011011
122101100001110
123000010110100
124011000101011
125010011011100
126110101010000
127101101101100
128101001110000
129010111010011
130110101110001
131011000001000
132011101010010
133111011111110
134010010000110
135110000111000
136101000000110
137011100110000
</