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
138111001111010
139110000001100
140101011111110
141100001111100
142011011100110
143101000001101
144001011110011
145111100100011
146101111000111
147101000000001
148001001011010
149010111111010
150101010111001
151011110101001
152101100111101
153011100000010
154000110001111
155111100110101
156100011100010
157000110010100
158011000000011
159010011001011
160000001101111
161101011101101
162100100100101
163011001110110
164010100000000
165100100010001
166001100000100
167100000111010
168101001011001
169010010111001
170110000101010
171001101010101
172110110011100
173010000000011
174111100011000
175000101111110
176100010110101
177101111010000
178000010100010
179101101001010
180011111000000
181011000100100
182110010100110
183010001110011
184010101101101
185001011100000
186001000110110
187001000110111
188110100111110
189110010110100
190101010100111
191000111011001
192111000101101
193110000010110
194010011101010
195110100111011
196100110010100
197100111000011
198011110111111
199010010000111
200111100111110
201001010111101
202000011001110
203000110010101
204111100110011
205101111111001
206101001111011
207111010110000
208000001010101
209100010011001
210111110110010
211110100101100
212100001011110
213100001000011
214101111000011
215101111110100
216111010000010
217001010010110
218010001000110
219100010000101
220111101111010
221000101111111
222010110110100
223110111111110
224011010101011
225100000110011
226000000101001
227010010101111
228010001001010
229000101010100
230101110111010
231101001000000
232001010101111
233011110100110
234110101011001
235100111101101
236110001001001
237110100001010
238100100101001
239110111010011
240011010001011
241011001001101
242110100010001
243010100001100
244011011000001
245110100011100
246001110111111
247111001110010
248001110010000
249011100001110
250001111110001
251001101010111
252111110011100
253111100111010
254010100010100
255000011001010
256011110001010
257111101010011
258100111001010
259010110110011
260000101000000
261000110011010
262100100001111
263000100111111
264010100010101
265001110100011
266100111000100
267100001101110
268011010011110
269001111010101
270111000001100
271100111110110
272101110001100
273101011000111
274001010110010
275100011011100
276110000010000
277101001101011
278111011011000
279000101101001
280011001111011
281101011001111
282001111111111
283110000101011
284111111100011
285101010101111
286001001010001
287011111010001
288001011101110
289011010011011
290011000001011
291100000100000
292001011110110
293011000100011
294001010011010
295000010111111
296001110001111
297111110001100
298010110100011
299110101100010
300100111110000
301010000101011
302011100011111
303001100101010
304101110110100
305100101000111
306111111111010
307000001000001
308110111011001
309010011000000
310101100000101
311110000100100
312110100100101
313101011010010
314101001000101
315110001001111
316010010011010
317000110110111
318011010110001
319001101001001
320010110101000
321101000011000
322110010011010
323100101011111
324001101110011
325110001110001
326011110000110
327111011100101
328001100111001
329001011011011
330001011110001
331010001111101
332011000101001
333100100000110
334011000101111
335001010110100
336000100101010
337111011010110
338100111000000
339001000111010
340101101001111
341000110111010
342001111100001
343011110011001
344100011000101
345001010010011
346110011100110
347010010001011
348110101101110
349111100100101
350101100100010
351111010111101
352000000000011
353011011001110
354101110000001
355000111111000
356100000110001
357101101100111
358001001011001
359101100010010
360001000011100
361111011111100
362010000100011
363111010111100
364011001101111
365100011001111
366011001111010
367110110100101
368010110100110
369011110101101
370011101110001
371111000100101
372000010000000
373100110111001
374000101000001
375000001101100
376011010000100
377111000101001
378111100111111
379100110000110
380100010001010
381011110001000
382100001100100
383101101101010
384101111010001
385010001111100
386001010111001
387001101110111
388010100101100
389100011011101
390010100111000
391000110000000
392110101101010
393010100000101
394101110111100
395001010110000
396000000010101
397000100011000
398011010110100
399010000000010
400111101100011
401000111101001
402011101011100
403101000101101
404110110101011
405011010110011
406011001101010
407100000000011
408101001111111
409111010110100
410010000011100
411011011100000
412110011011000
413111011101111
414100010110110
415010000100111
416110101110101
417100010111001
418011011101000
419011010000001
420001001111110
421100010010001
422010101000110
423111111110010
424101011011110
425010011110110
426000010010100
427100011101111
428001001110110
429000100110100
430100101110110
431011010111001
432010010101110
433110010100011
434011010101010
435000101111000
436111011110011
437100111101110
438101111101001
439010111100010
440111011001011
441111110011110
442110110010110
443010110010111
444011111001100
445110111110010
446110011111000
447001110111101
448111100111000
449000100000010
450110101111110
451010111101101
452111001001010
453000010011100
454110101100100
455111000011110
456010110000000
457011100010011
458011110010100
459101000100111
460111011110101
461000100010000
462000110110011
463100011001011
464111010100000
465010100110101
466001000000101
467010001101100
468010110000101
469100100101111
470011101111110
471000011110000
472000011010011
473101111111110
474101001101010
475000010100011
476010100101010
477001111001110
478011000001001
479010001110100
480010111011110
481100000111111
482101100010101
483111101000110
484110111101111
485011010010010
486011100011100
487001111010010
488111001101000
489110110111010
490101110000111
491011100101100
492011100001010
493110101011101
494011100010010
495010101010010
496000001111101
497110100110111
498011111100111
499111101011110
500110110000110
501100110001011
502111110000001
503010001010011
504101001110110
505011101011101
506010011011110
507000100000000
508100110010001
509000011100110
510101111100001
511101100101010
512001010101110
513001111001101
514110000000111
515110010100001
516011100000111
517100111100111
518101001011111
519110000100011
520111001000111
521010101111010
522110111100100
523001010101100
524001011110100
525001010010111
526110001001000
527110010011001
528110111011100
529001110000011
530010001000010
531100000000101
532110001101001
533000101010111
534101010110101
535100000010000
536000000111100
537011011100100
538110010001110
539110110100000
540101100001100
541110010110111
542010001100010
543101110100010
544001101101101
545010100110000
546010010100000
547011101110100
548100011011001
549010110111101
550010110011011
551011111011111
552001000000000
553001101110101
554010000010101
555000101001000
556011011100101
557001010000101
558110010001101
559000001000011
560001101001110
561000101100010
562010110100001
563010111110011
564011000100111
565100001101011
566100100100011
567111100011001
568010010101000
569101010000010
570010011000001
571100000000010
572010011101011
573101110100111
574111101101011
575011010001111
576001000011000
577011111110111
578101111011100
579011011011111
580110111100010
581010010110001
582111100010111
583111000001111
584101100010011
585010101001101
586110101100001
587010111010110
588110000001111
589000111000000
590110010010001
591001110110101
592101000010010
593010110001100
594001101101010
595110110110110
596000000000111
597011010010000
598011001010111
599001010101011
600001001100011
601001111010110
602000000100111
603000000010100
604001001110011
605010111000010
606010000001100
607101111010111
608101001110100
609100011010111
610000111110100
611011101111010
612000111000010
613011100111100
614011011110100
615101000110011
616010101110111
617111101101000
618010010011011
619000101011111
620010000000110
621011110100101
622111000000100
623010110110101
624010110111001
625101110010000
626010111001111
627001001000110
628010100010000
629101100100111
630101100001000
631110110001001
632011001001010
633011001001110
634110011101001
635010100101111
636111101001000
637100000101100
638111000011000
639011000011100
640111111010001
641111100100100
642011000111110
643010011010000
644100110101100
645110000100001
646001101010011
647011000101110
648000110010000
649011010101100
650001110001010
651010101000001
652101010010101
653100000010111
654000100110101
655110111010110
656101101101011
657100101100101
658101010011110
659111001000100
660011101100111
661010000110101
662100111010110
663010011111110
664111011100100
665100010101101
666010110100101
667100011100000
668110010011101
669011110000011
670111111110011
671100001101100
672000000011000
673000100101101
674000100110000
675000110000010
676011110010011
677111001111111
678000011110101
679111001101100
680111010111010
681000101000101
682111000010110
683111010011000
684101110100110
685111100000100
686101000110001
687000001010110
688101000100000
689110111001010
690001001101000
691010011111010
692100010100100
693110010101001
694111101100101
695010001000101
696011001111101
697110000001110
698100101101100
699110110111100
700010111011101
701111011011100
702011111011100
703100100011001
704111010011011
705110011101011
706100001101000
707010110100010
708101100100110
709010110111010
710111111011001
711101011110011
712011001101001
713110011100111
714010100111110
715001001010011
716010101110000
717010111010101
718100110011100
719110010011011
720001011010111
721000001010100
722001111011100
723000010100100
724010101011011
725010100001011
726000001011110
727011111010111
728010000101100
729111111101111
730100101010110
731100011010101
732100101100100
733010011111101
734010011001111
735011010000101
736110010000111
737101110110110
738000010001001
739111000010010
740111001001111
741111000111010
742010111011100
743011010001101
744111011011111
745001010110001
746011111111101
747001001001011
748100100011000
749001000101010
750101001111010
751011101000010
752111010000111
753001000100000
754100000100010
755111111011000
756110011000110
757111111100010
758100110011101
759101111101000
760001001001100
761100001100001
762100111110011
763001000100010
764011100011000
765000001010010
766100010111110
767101111010010
768100100000011
769000101000010
770101001011100
771000101001101
772000101110010
773111111111011
774110100000100
775000001000010
776101011010101
777000011110011
778000011001000
779010111111001
780111110100111
781100110100111
782001100001111
783000100011110
784001101000101
785011110000000
786000100111100
787011110010111
788011011010011
789010101011110
790100111001100
791011111101010
792011011001000
793100100110100
794011101010101
795100101111001
796100011101001
797101010000001
798101110111001
799110100101111
800010001001100
801100110110000
802100000111001
803010111011011
804111110001000
805101000111101
806000011010110
807100010010010
808010010011111
809011110111110
810000100111000
811111100001101
812000010110101
813010010001110
814101111000100
815100101111100
816010100011111
817110101010010
818000010111110
819100001000100
820001010001010
821110000111100
822010000101000
823111000110110
824001001101010
825001101011100
826001000001100
827110010011100
828011111000011
829111100101101
830110111010101
831000010010110
832111101110111
833000011010010
834111110000000
835001111000101
836100010111100
837001011000011
838010110000010
839001101111101
840101011000011
841100001010010
842100001010100
843111100011101
844010110001101
845011101101101
846101010101110
847110111110100
848001111001010
849100010111101
850011010001001
851111111001000
852100010100001
853111010010110
854001101000011
855000011111011
856110101000100
857110101111101
858010010000011
859110101001111
860001110111110
861101111100011
862011011011100
863001100100100
864010011111000
865101010001111
866111101010101
867001110110001
868101100010100
869100010001000
870101110000110
871001101100001
872111001000110
873101110011110
874100010000110
875101010111011
876001010110101
877001101101110
878100000101111
879011001000001
880010100001110
881100111101000
882011000110000
883110000101111
884010110111111
885001110101000
886111010100010
887100000110100
888100100000010
889101010110011
890010101000100
891100100001011
892000110011011
893110001000010
894111111000111
895010001010010
896001000011011
897110010000101
898111110110111
899001000111001
900101111100010
901101001110010
902001101101111
903101001101101
904001000010011
905111000011111
906111111110101
907101001000001
908010101101111
909110100100100
910001011101011
911001010111100
912000111100011
913010001010100
914110011000011
915100111100000
916001001111001
917111111100001
918001001100110
919001000101001
920111101011011
921111000000010
922100000100111
923101011100100
924001010111110
925100110110100
926110111100111
927000110101000
928011001000111
929010111000001
930101111101101
931000001101101
932000110100111
933110001010100
934111110101111
935111110110101
936111000010001
937101111011010
938101111110110
939000000101100
940010011010010
941000101010101
942100110101000
943000111000011
944001000010110
945000111001010
946000001000111
947000101000110
948000100010100
949001011011101
950001001111101
951000000000101
952000100100101
953101010001101
954001010000111
955110110001110
956000010011010
957011001110101
958111111101011
959001100001100
960100111001000
961011010011000
962000001101001
963010001111001
964001001110100
965111110111000
966000010010001
967111111010011
968100000011011
969010110010100
970010000111000
971100001110110
972111101111001
973110000011010
974100110101111
975010001100111
976100001011100
977110000010011
978010100001111
979110010010110
980110010111000
981001011000110
982101011001101
983010000000100
984010010000000
985010100100101
986110010100101
987010101110110
988101101001101
989110000110011
990110011110110
991111101110110
992001011111001
993110101101000
994101111111100
995110011111101
996100001111110
997011101111000
998101010000111
999101110011111
1000101000010101 \ No newline at end of file