summaryrefslogtreecommitdiffstats
path: root/2021
diff options
context:
space:
mode:
authorYigit Sever2021-12-10 19:54:01 +0300
committerYigit Sever2021-12-10 19:54:01 +0300
commit3c97ec63d921c7d2f132a834f9b739cc8d039c99 (patch)
tree492bbff2312042c68e1a2d2d95ed0bf599dfe545 /2021
parentae3853b6e8ab02023ccd74baac6dc177b1ee879a (diff)
downloadaoc-3c97ec63d921c7d2f132a834f9b739cc8d039c99.tar.gz
aoc-3c97ec63d921c7d2f132a834f9b739cc8d039c99.tar.bz2
aoc-3c97ec63d921c7d2f132a834f9b739cc8d039c99.zip
2021, day3: done
Diffstat (limited to '2021')
-rw-r--r--2021/day3/Cargo.toml8
-rw-r--r--2021/day3/input1000
-rw-r--r--2021/day3/input.example12
-rw-r--r--2021/day3/src/main.rs99
4 files changed, 1119 insertions, 0 deletions
diff --git a/2021/day3/Cargo.toml b/2021/day3/Cargo.toml
new file mode 100644
index 0000000..898e70d
--- /dev/null
+++ b/2021/day3/Cargo.toml
@@ -0,0 +1,8 @@
1[package]
2name = "day3"
3version = "0.1.0"
4edition = "2021"
5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8[dependencies]
diff --git a/2021/day3/input b/2021/day3/input
new file mode 100644
index 0000000..929c455
--- /dev/null
+++ b/2021/day3/input
@@ -0,0 +1,1000 @@
1010101110000
2010011000110
3010101000011
4111100100001
5011100110101
6110001010101
7001111110101
8101100011100
9010111111011
10101010111101
11101000001110
12001000100001
13000100011110
14100011000100
15101100100001
16101010111010
17111000001011
18101101001011
19000010010110
20110111100111
21111101011110
22100100010010
23110001011110
24010011101000
25111110000110
26110001011111
27001001010110
28110111100100
29100111000101
30110011110000
31000110000010
32010001100001
33111110111011
34100010101011
35000000010101
36100010101110
37100110100111
38100111101111
39110011110001
40001000111100
41001101010010
42111101011101
43011000110001
44111111011010
45011000000000
46100000111001
47011110101100
48011101010111
49011001111110
50011100101101
51101100010011
52001100110000
53110101101100
54110110001100
55000101101110
56001010110010
57111110110110
58111010010011
59100010111111
60001001001000
61110111001011
62010110000010
63100000000100
64100001010010
65110010011101
66000001011011
67000100010100
68101011000001
69011101110111
70001100010100
71101000010110
72111111001100
73110011001011
74110000000000
75010100110101
76001111100011
77100011010010
78100111100011
79110101001010
80011111011000
81001010100110
82010101010100
83110111011111
84001001010000
85001011010101
86101111011011
87101001101000
88000011101110
89000101110010
90100011001010
91011001010010
92100100110011
93010010011100
94001100100110
95100111110111
96000111111000
97100000001101
98001111010101
99000001001111
100000101100000
101000101001111
102101010001001
103111011110001
104000111100011
105110011000010
106110011111101
107101001010110
108101111111000
109000110100010
110101111010001
111010011110001
112111110010010
113101111011101
114101110111101
115010001011111
116101110011010
117001110001100
118000110011001
119000101000101
120101010111011
121110010101100
122000100101101
123010111011111
124111001010110
125000010100100
126100000111100
127101101110001
128100001111010
129000111010010
130111100000110
131010010000000
132010001001011
133111101010000
134010011111110
135111011101011
136110110100110
137100001000011
138010010000110
139010000000001
140000001000101
141011010100000
142001011011000
143110101010001
144011000111110
145010011101101
146010011000011
147011010011101
148001101110100
149010001100101
150111011100011
151010111101000
152111100000010
153010000110000
154000100111001
155010010110100
156000101110110
157000011001110
158111010111100
159100101010001
160111011000101
161011111101100
162000110010001
163100011101111
164100101001110
165101101110010
166000000011010
167000001101000
168010110011000
169110100001001
170111111101100
171011010110001
172011100111101
173001101010110
174100111000111
175000000001111
176001110101010
177101000011001
178000111010111
179100110110001
180100011000011
181110111101011
182000110111001
183000010010011
184010111011001
185000111001000
186100011011010
187011101000100
188101010010100
189010111110000
190011011100111
191000001110001
192000100111000
193111100001011
194100101010000
195000110110100
196110011110111
197111000010111
198101110011000
199110011010000
200001111000100
201111010001001
202110001111000
203011101011111
204000001001100
205110100111011
206110001011011
207001010000100
208110011101110
209101100100100
210100101100001
211001010001010
212101010001000
213000110101001
214001100010000
215110001000101
216000100111110
217000110100111
218100010001010
219100100010110
220010001110011
221101111010010
222100000001001
223000110110000
224100010011011
225111101000001
226101111111011
227111111100110
228101001101010
229000011111011
230110000000011
231000001111010
232001001000100
233101110001101
234100111011111
235110101101110
236100011011011
237101000010001
238101001111000
239111101110010
240000101000011
241100100010100
242010101111101
243100110001011
244010011010111
245110111100101
246110010010101
247100010101101
248100110000110
249001101000001
250111001001000
251010111010011
252000110001111
253010101011010
254000000001000
255001001100100
256101111111001
257001110101000
258000110100100
259001111101111
260010100011000
261010000110110
262011101101110
263010100100100
264111110010101
265101100000110
266011010110000
267001001011101
268000000000101
269100101111001
270110101110110
271100111101010
272010010101101
273010100001111
274111111111110
275011111000000
276000001101111
277011001011001
278001101101000
279011010110110
280100101000110
281110001101110
282110100101011
283010001110100
284011110110101
285010001010001
286110110101000
287010000100011
288010101011001
289101111111010
290000011110010
291001000011101
292110001100010
293110110000011
294010000111011
295111010010000
296011001001000
297000111011010
298011111111100
299111001111100
300001101000100
301101110001011
302101110000111
303011010111111
304110011100111
305001101110010
306011111101111
307001110000110
308010111100001
309110100101001
310101010110010
311111011010100
312101001110010
313101101000100
314100111010001
315000011010110
316100110000100
317101000101010
318000000001010
319110100111010
320110010011010
321100110010100
322011101110010
323000111111010
324001000101001
325110100110010
326100100111011
327011001010111
328111000101010
329011001011011
330011010000000
331001101101110
332101010111000
333101010011000
334011000010101
335111001011010
336100111111111
337001101001100
338111011011010
339110110100011
340111010010100
341000110011101
342011100010011
343011111100111
344010110100000
345001100001000
346000110000110
347000101011011
348101011101011
349111101111110
350111111110000
351101101001100
352011000110110
353100111001000
354011011111100
355001111011110
356111011100100
357000101010010
358100011011000
359100000100110
360100000110011
361011000010110
362001010110100
363100111100010
364011110101011
365110111111010
366011100110110
367100100000000
368011010101101
369001101000010
370010110010111
371100010011100
372111101010101
373011011010111
374000000000110
375101001100000
376010100110011
377010101111111
378101110111001
379101101111000
380101110101000
381011011111110
382010001101010
383001000101010
384111100111111
385001011011110
386101010101001
387011111001010
388001001001111
389101000101101
390001111011000
391000100001101
392101101011000
393001100000100
394001000111101
395000001110100
396111101011111
397111010000010
398101111011111
399001100110101
400101010110001
401001110111010
402010010111110
403101101010011
404100001100110
405110010010001
406111100111001
407000100010001
408110010101010
409111010110101
410000001010110
411000111110110
412111010101100
413011110001100
414010110001001
415010111100011
416001010111000
417100111100111
418110110011001
419010100000000
420001011001111
421110010110000
422100011001111
423100111111011
424110001001001
425000100101111
426011010011011
427001001111101
428111110001100
429101111110010
430101100101110
431100110111001
432111010110011
433110010011110
434001111001101
435010011100111
436000111000011
437101101000000
438000001100101
439011101111001
440110100100110
441000110000001
442110111011101
443011100000110
444001011110100
445010001011011
446110000011110
447110001001011
448110100000001
449011010010100
450010010000101
451001010110111
452110001001111
453101100000001
454010000000000
455101110010001
456001100101110
457010000011010
458010100101011
459011010101100
460111101011011
461110111010000
462111111101111
463111011111011
464110001110111
465011100100011
466100001110101
467100111011001
468100110010000
469100110101110
470110101100001
471001010001101
472011101101000
473110001001110
474110010111011
475111100110100
476101011100011
477010100100101
478010001101111
479111000110101
480001000101011
481101101000101
482011000100111
483001111000011
484011000000011
485000011001111
486001010011111
487101001100010
488101010100001
489001001110010
490101010000111
491010111011010
492110101110111
493000110101111
494000110010011
495010110110010
496100111001011
497001100111111
498111110000001
499110110111000
500101010000100
501011100101010
502001000010101
503010010011001
504011111000110
505010011010011
506001011101101
507001001110100
508110101011100
509011000110100
510010101101000
511100000100000
512011011111010
513011010011111
514111011011101
515001111101110
516001000110101
517010001011100
518110101100010
519001100100100
520001100101011
521100110111000
522001100101101
523101000001011
524001100010101
525001100011010
526101011010101
527110101011111
528101111001011
529000010001111
530011110001111
531110000000100
532000110100001
533010010101111
534110000000110
535010011000000
536011001000101
537001101011010
538000011001101
539110100101010
540111001010111
541010011010101
542111010100101
543111110110101
544111110011100
545110010000110
546000110100011
547001000111000
548000101101100
549101110001010
550010010010110
551100001110111
552111010100010
553110110010111
554101000011000
555100100111010
556001000011011
557111101101011
558101001010111
559100110000011
560100001010001
561011011000001
562010110010101
563110001010000
564001110000000
565111001001110
566011011101100
567000100001111
568111000110011
569111101101101
570100110000000
571100011110110
572001000000010
573110011011000
574111000100110
575100110110000
576010011101010
577111011001010
578001101111000
579001100000111
580001110011110
581100001000100
582000010000111
583011010101001
584111110001001
585110110000010
586011011101101
587110001111010
588011001101110
589010011000010
590100101110100
591101000100001
592000111011101
593101100001011
594100000011010
595111001001001
596100000000001
597000101111001
598100101101101
599111001101111
600001011011011
601000010101000
602001010111010
603000100100011
604001110101101
605100000111000
606110110001110
607000111101101
608001010001111
609011000111011
610100000100100
611100011010110
612000010000000
613001000001101
614010001001110
615000111111011
616100110000001
617110010111000
618111100111100
619001000001100
620000010110100
621011010001110
622110001110101
623001100000101
624100010110100
625111000000010
626110100100101
627010010111011
628010110011001
629001001101100
630001010101010
631100001011100
632010001101001
633000000010010
634000101111010
635011000101011
636111111010101
637011110101111
638011110011001
639001101101101
640111110101110
641101111110100
642010101001111
643010010011101
644000110101100
645011100000011
646100011101001
647101101100111
648110011101100
649000110111010
650000011001000
651111101010011
652011011101110
653111101001100
654100100101111
655111010110111
656000001100110
657110100110101
658110100001011
659111010000001
660001011110011
661001010110101
662100001100000
663101000101100
664011011100001
665000011011011
666100101110001
667101100111100
668010000111110
669010100101101
670001111011111
671110101010000
672000110010100
673001001011011
674110111111011
675111100010110
676110000101011
677111001000100
678000100000111
679001011001110
680011110100010
681011011110101
682001100011001
683111010011111
684011011110110
685111101000000
686101111110000
687011100111000
688010010011011
689101000110010
690001101101111
691010001000011
692001010010110
693101000010010
694110111001111
695011111010101
696111111011001
697100011110011
698110001100100
699100000010100
700001111011010
701111001111000
702110101001011
703101000110001
704111001111111
705110000010111
706000000100100
707101011101110
708111001110111
709001110011010
710110011101000
711001011100100
712011001011111
713010100010101
714011111111010
715010010110000
716100011101110
717000011000110
718001001010111
719010111110100
720010110110101
721010000101000
722101010101010
723000100100010
724000110011000
725101010110101
726100000100001
727101100010010
728010110000110
729011011010011
730000001010010
731011101111110
732100010000100
733000100101110
734010110010001
735101100011011
736010111010000
737101100011111
738010011001010
739100010100000
740001001101001
741110010011001
742111000110100
743111111010000
744111100101110
745101010001010
746111111100100
747011110001110
748101100001111
749001010001100
750110100100011
751011100010101
752110001011000
753000010100001
754011000011110
755000000101101
756000011101101
757111010011000
758000101110100
759111100011010
760101100010110
761100001101011
762110100110100
763101100111010
764010000011110
765101001100011
766000101110001
767001101111111
768111101000101
769100101111101
770001101011110
771011111101011
772100000010101
773011010000001
774011000011101
775110101011110
776111010101000
777110111111100
778101111110011
779101101001001
780000111101001
781010110000011
782110011000000
783000001111101
784001011101000
785001001111011
786110110000111
787101100010101
788101100100111
789000010110001
790110000111011
791110111001000
792010010111111
793100010110000
794010000111100
795101001000111
796010100001100
797001111001010
798010100101010
799100001000010
800010011101111
801100011000001
802110000001101
803011001011110
804100111111001
805001100111101
806010110100110
807001000011001
808101000111110
809000100001011
810111110100111
811011100111001
812100011111001
813100001100001
814110110001011
815110011101111
816011010111011
817110101010110
818010111101001
819111110000011
820111100000111
821011111100100
822101001000001
823110111010100
824000011000000
825000100111101
826001100010011
827100000010011
828010010001001
829010111000100
830110110010100
831011100101110
832110110100010
833100111010111
834001111010011
835000110110110
836000010000110
837110100010001
838010100111101
839011001000110
840101011010100
841001110001101
842101111110101
843110011001010
844110100011100
845001101011000
846100101101110
847000001000111
848111001101011
849101011000010
850001001011111
851111000111010
852111011100110
853001001011100
854000010011000
855000101001001
856100110001111
857010100100010
858001101000101
859010111111100
860100010000001
861110101101111
862100010101010
863110110010001
864011101000111
865011110001010
866100110011110
867010011100100
868110101110100
869000000110010
870001111101100
871010011010010
872010000100000
873110001000100
874110110111111
875011100001111
876110110001101
877111110000010
878010001110010
879001101110101
880111111101101
881010101101101
882001011011100
883001011000001
884001011011101
885010001100010
886011110110011
887000101101011
888000110111000
889101101110000
890101011011001
891001001001110
892000001000000
893101011010010
894101110010111
895110011100001
896001011101001
897000000100001
898010000100110
899101111010000
900011000011100
901100011100110
902110111110011
903110010000100
904101110000000
905110010101101
906011101111000
907001111000110
908101001011100
909011111001111
910111011101010
911001110101110
912101001000011
913111101101111
914111001110101
915000110100110
916111111011011
917101011011011
918111100101101
919111111010100
920101010010000
921101000110011
922011110100000
923010100101001
924010011100011
925101000111001
926101100000111
927101010100010
928101110011111
929111011011110
930110011011001
931101001101101
932110010001011
933010100101000
934001010011010
935001100000000
936110000010000
937000110111011
938011101111111
939011001001001
940011011001001
941000100101001
942110010010010
943000110001001
944001000100011
945101011101111
946100111110010
947110010101011
948000000110110
949010111001101
950100100001111
951100010001011
952011101100100
953000011111000
954010100111011
955010101101100
956100110100110
957110100101110
958010111111001
959010010011110
960111110000100
961000110001101
962100100100001
963111011010111
964101001010101
965111011010010
966101001000000
967100111101100
968101010110000
969000111001101
970000100100110
971100100101000
972111000011001
973000110110101
974111010000011
975101100110001
976000001011000
977001110110011
978101000100110
979100110100101
980010001000111
981100011010001
982010100011101
983110100110110
984100010100110
985101100010001
986010011000100
987100100010101
988110000101101
989010101001100
990100011100100
991001111010100
992100111001001
993110000100000
994100100010111
995010101000010
996101101001010
997110101111101
998010100000110
999011101100110
1000011011010010
diff --git a/2021/day3/input.example b/2021/day3/input.example
new file mode 100644
index 0000000..a6366a8
--- /dev/null
+++ b/2021/day3/input.example
@@ -0,0 +1,12 @@
100100
211110
310110
410111
510101
601111
700111
811100
910000
1011001
1100010
1201010
diff --git a/2021/day3/src/main.rs b/2021/day3/src/main.rs
new file mode 100644
index 0000000..2440a39
--- /dev/null
+++ b/2021/day3/src/main.rs
@@ -0,0 +1,99 @@
1use std::env;
2use std::fs::File;
3use std::io::{BufRead, BufReader};
4
5fn main() {
6 let args: Vec<String> = env::args().collect();
7
8 if args.len() != 2 {
9 eprintln!("Usage: {} filename", args[0]);
10 std::process::exit(1);
11 }
12
13 let filename = &args[1];
14
15 let file = File::open(filename).unwrap_or_else(|_| panic!("No such file: {}", filename));
16 let reader = BufReader::new(file);
17
18 let mut numbers: Vec<u32> = Vec::new();
19
20 for line in reader.lines() {
21 let line = line.unwrap();
22 numbers.push(isize::from_str_radix(&line, 2).unwrap().try_into().unwrap());
23 }
24
25 let mut co2_numbers: Vec<u32> = numbers.clone();
26 let mut oxygen: u32 = 0;
27 let mut co2: u32 = 0;
28
29 for bit in (0..12).rev() {
30 let keep;
31 let mut one_bits = 0;
32 let mut zero_bits = 0;
33 for num in &numbers {
34 one_bits += (num >> bit) & 1;
35 zero_bits += ((num >> bit) & 1) ^ 1;
36 }
37
38 println!("for bit: {}", bit);
39 println!("one_bits: {}", one_bits);
40 println!("zero_bits: {}", zero_bits);
41
42 if zero_bits > one_bits {
43 keep = 0;
44 } else if zero_bits < one_bits {
45 keep = 1;
46 } else {
47 keep = 1;
48 }
49
50 println!("keeping {}", keep);
51
52 numbers.retain(|num| ((num >> bit) & 1) == keep);
53
54 if numbers.len() == 1 {
55 oxygen = numbers.pop().unwrap();
56 break;
57 }
58
59 println!("{:?}", numbers);
60 }
61
62 println!("====co2 now====");
63
64 for bit in (0..12).rev() {
65 let keep;
66 let mut one_bits = 0;
67 let mut zero_bits = 0;
68 for num in &co2_numbers {
69 one_bits += (num >> bit) & 1;
70 zero_bits += ((num >> bit) & 1) ^ 1;
71 }
72
73 println!("for bit: {}", bit);
74 println!("one_bits: {}", one_bits);
75 println!("zero_bits: {}", zero_bits);
76
77 if zero_bits > one_bits {
78 keep = 1;
79 } else if zero_bits < one_bits {
80 keep = 0;
81 } else {
82 keep = 0;
83 }
84
85 println!("keeping {}", keep);
86
87 co2_numbers.retain(|num| (num >> bit) & 1 == keep);
88
89 if co2_numbers.len() == 1 {
90 co2 = co2_numbers.pop().unwrap();
91 break;
92 }
93
94 println!("{:?}", co2_numbers);
95 }
96
97 println!("oxygen: {}\nco2: {}", oxygen, co2);
98 println!("answer: {}", oxygen * co2);
99}