summaryrefslogtreecommitdiffstats
path: root/2021
diff options
context:
space:
mode:
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
-rw-r--r--2021/day4/Cargo.toml8
-rw-r--r--2021/day4/input601
-rw-r--r--2021/day4/input.example19
-rw-r--r--2021/day4/src/main.rs99
8 files changed, 1846 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}
diff --git a/2021/day4/Cargo.toml b/2021/day4/Cargo.toml
new file mode 100644
index 0000000..8842130
--- /dev/null
+++ b/2021/day4/Cargo.toml
@@ -0,0 +1,8 @@
1[package]
2name = "day4"
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/day4/input b/2021/day4/input
new file mode 100644
index 0000000..33aa45f
--- /dev/null
+++ b/2021/day4/input
@@ -0,0 +1,601 @@
184,28,29,75,58,71,26,6,73,74,41,39,87,37,16,79,55,60,62,80,64,95,46,15,5,47,2,35,32,78,89,90,96,33,4,69,42,30,54,85,65,83,44,63,20,17,66,81,67,77,36,68,82,93,10,25,9,34,24,72,91,88,11,38,3,45,14,56,22,61,97,27,12,48,18,1,31,98,86,19,99,92,8,43,52,23,21,0,7,50,57,70,49,13,51,40,76,94,53,59
2
331 93 46 11 30
4 2 45 40 69 33
582 21 37 99 86
657 16 34 94 85
760 49 28 14 65
8
996 2 20 41 24
1029 15 27 83 48
11 7 93 99 82 26
12 3 91 66 35 85
1362 78 67 4 22
14
1510 87 50 84 40
1678 5 17 59 44
1738 88 15 46 32
18 8 72 74 90 23
1964 93 49 39 20
20
2125 41 32 30 39
22 6 66 38 95 5
2331 13 56 67 34
2469 18 64 44 96
2575 14 88 97 40
26
2739 62 50 10 68
2818 7 95 72 82
2983 23 19 70 71
3011 64 30 8 3
31 6 81 27 34 99
32
3340 52 66 20 49
3493 74 16 35 29
3597 88 6 98 81
3662 55 99 47 12
3783 76 57 75 22
38
3952 76 43 86 99
4058 26 61 36 42
4111 69 65 3 49
4233 7 71 8 25
4350 82 32 16 64
44
4545 38 88 96 8
4622 17 5 60 66
4787 12 61 59 2
48 0 37 18 15 98
49 7 62 23 56 92
50
5120 7 12 26 69
5281 63 89 57 19
5318 44 61 64 53
5447 27 8 30 0
5560 99 28 6 96
56
5770 50 63 56 26
5855 97 65 5 96
5972 68 29 91 61
6034 0 14 28 4
6145 53 78 80 47
62
6319 12 92 80 97
6422 37 38 9 82
6586 72 99 59 89
6673 18 21 87 46
6733 77 71 54 94
68
6966 99 10 44 48
7038 45 4 63 62
7150 27 88 56 89
7281 91 72 90 32
7376 29 17 6 7
74
7571 39 10 77 50
7697 95 90 98 4
7766 32 73 35 16
78 1 99 7 96 23
7974 31 94 37 5
80
8120 72 78 5 69
8288 8 55 80 30
8362 58 4 53 47
8445 13 7 14 52
8586 12 66 74 76
86
8795 53 59 68 37
8863 40 20 46 43
89 5 27 67 84 39
9099 51 12 76 50
9178 23 89 22 57
92
93 0 96 2 88 11
9498 52 70 28 22
9560 50 18 49 41
9697 6 68 21 47
9790 57 58 99 48
98
99 7 3 77 61 34
10057 69 64 68 58
101 8 49 33 22 15
10263 10 4 95 59
10328 20 71 44 75
104
10590 68 15 99 38
10616 80 56 70 3
10788 52 18 4 39
10840 85 89 84 44
10965 73 71 79 2
110
11166 43 12 10 70
11279 57 54 41 6
11346 73 40 3 52
11436 21 38 8 62
115 7 26 42 32 0
116
11757 51 69 34 43
11814 9 96 88 74
11990 22 83 30 71
12080 64 91 48 97
12154 38 89 66 16
122
12351 4 49 2 59
12453 40 33 54 45
12569 41 92 26 60
12693 72 94 90 35
12776 8 75 38 52
128
129 5 75 19 28 39
13014 80 27 64 78
13124 69 23 96 42
132 0 81 10 20 13
13336 57 17 76 52
134
13538 25 7 88 33
13624 41 96 85 64
13752 89 63 93 18
13894 20 0 98 28
139 9 30 27 79 83
140
14174 97 15 55 67
14292 28 24 11 38
143 8 70 65 63 45
14483 22 30 76 96
14521 85 29 59 36
146
147 1 79 29 0 75
14868 58 61 84 51
14985 74 46 69 3
15094 16 41 43 15
15157 21 48 22 42
152
15361 5 1 25 28
15450 40 74 14 94
15532 0 68 15 54
15660 45 51 31 97
15756 52 8 9 63
158
15920 36 72 74 83
160 4 88 35 62 24
16168 39 58 30 34
16261 41 42 93 66
16348 12 57 6 11
164
16589 52 47 66 64
16667 28 30 46 22
16744 79 23 86 80
16881 25 10 53 94
16956 87 74 60 75
170
17138 62 76 43 50
17283 53 64 4 44
17352 47 91 49 7
174 0 85 96 78 86
17532 8 81 87 90
176
17776 67 31 74 87
17815 70 9 83 78
17960 46 86 73 72
18071 53 82 89 28
18139 97 68 96 11
182
18388 7 32 40 94
18439 68 13 6 9
18520 55 72 49 25
18656 86 78 12 18
18727 34 19 48 44
188
18988 19 29 87 28
19053 47 40 74 77
191 3 93 14 38 33
19278 39 26 32 22
19375 0 2 95 54
194
19552 35 43 77 79
19653 56 93 92 12
19715 23 16 10 66
19863 96 58 60 94
199 6 55 76 21 89
200
20155 11 26 37 18
20277 80 0 30 58
203 2 97 13 27 98
20468 40 91 56 20
20578 42 45 87 24
206
20762 71 79 12 66
208 9 73 30 70 88
20918 72 27 5 75
21049 3 47 33 6
21155 63 98 15 21
212
21320 52 46 33 27
21480 2 48 29 62
215 9 32 5 41 53
21674 39 87 12 73
21796 79 61 63 22
218
21930 48 26 21 83
22056 94 39 47 99
22193 66 25 76 57
22260 96 62 13 84
22371 42 28 31 20
224
22530 77 49 36 34
22653 75 11 38 61
22741 72 4 58 92
22891 10 21 51 31
22989 43 14 23 45
230
23130 76 12 1 21
23231 0 82 2 8
23326 28 61 32 99
23465 46 16 39 74
23560 63 5 50 42
236
23731 90 72 81 8
23817 94 45 20 38
23930 28 68 0 25
24084 10 37 61 83
24187 70 32 64 99
242
24321 28 99 30 2
24486 64 53 54 84
24582 13 79 96 55
24676 3 12 92 98
24727 89 10 37 95
248
24974 38 98 18 55
25062 82 93 85 46
25143 3 26 84 86
25297 1 19 12 67
25394 72 68 88 0
254
25541 97 88 0 40
25620 5 51 42 87
25738 9 11 50 6
25835 25 54 12 81
25975 58 57 94 26
260
261 2 30 22 48 88
26260 86 69 31 57
26397 58 95 47 71
26485 63 81 80 73
26525 92 62 11 83
266
267 3 93 20 74 86
26821 31 73 35 91
26941 28 25 4 92
27019 5 38 52 59
27137 16 48 85 68
272
27326 32 66 93 68
27474 90 70 41 57
27599 94 51 3 46
276 1 24 17 54 39
27753 60 50 96 49
278
27950 27 72 42 84
28082 62 39 33 31
28199 4 20 3 70
28211 28 57 55 12
28361 29 14 60 90
284
28560 23 66 99 3
286 6 24 42 51 45
28741 39 53 25 72
28832 57 5 74 4
28978 44 67 7 50
290
291 8 39 4 42 50
29227 25 77 94 90
29352 45 59 60 84
294 0 78 36 86 22
29556 1 71 82 29
296
29778 11 49 67 93
29899 76 12 37 1
29917 35 19 92 91
30022 54 31 72 36
30161 96 63 26 33
302
303 1 69 71 21 72
30487 92 32 29 80
30540 39 52 42 75
30626 24 11 44 23
30710 99 62 36 98
308
30960 47 49 24 66
310 2 19 38 57 94
31154 22 17 33 42
31265 80 48 86 20
31353 27 59 30 11
314
315 3 81 64 69 82
31693 80 71 51 46
31776 7 38 57 91
31837 88 8 65 54
31936 25 1 95 77
320
32175 56 19 21 34
32215 7 24 41 86
32368 29 85 80 88
32418 58 81 44 62
325 0 52 89 47 59
326
32749 98 57 90 46
32876 88 84 60 6
32938 78 77 83 99
33079 48 5 63 74
33166 30 37 4 47
332
33356 37 93 43 11
33481 87 85 52 94
33564 91 36 97 27
33618 31 26 22 78
33750 54 32 69 96
338
33973 20 51 50 30
34048 83 22 76 85
34126 3 63 67 92
34297 0 87 88 1
34318 33 86 77 52
344
345 1 18 50 7 22
34611 96 47 94 67
34758 36 43 65 6
34891 53 23 34 44
34981 30 83 86 55
350
351 1 36 10 84 90
35255 35 74 23 26
35329 53 76 40 85
35489 77 16 17 93
35511 19 65 5 49
356
35799 48 88 10 79
35884 75 66 74 58
359 3 26 92 91 86
36087 98 52 45 21
36156 51 0 85 69
362
36353 45 75 5 82
36444 90 11 2 85
36569 19 46 89 47
366 4 22 16 84 52
36781 1 39 9 38
368
36918 57 15 30 35
370 9 32 52 44 25
37169 43 61 3 51
372 2 8 80 85 33
37372 13 45 10 87
374
37552 24 26 74 49
376 0 37 61 91 54
37770 96 60 80 39
37811 75 85 46 18
37916 90 20 66 67
380
38113 74 92 11 7
38259 46 41 12 55
38327 77 89 70 96
38481 5 29 32 51
38584 75 33 63 99
386
38787 58 84 6 68
38865 9 75 48 90
38941 94 89 64 21
39095 81 72 32 30
39183 17 63 52 97
392
39389 0 61 92 16
39444 65 25 66 20
39529 27 37 94 68
39610 63 12 48 77
397 6 32 82 4 40
398
399 1 20 69 80 45
400 8 7 15 27 19
40140 87 2 98 32
40235 79 84 71 14
40324 43 23 29 6
404
40559 62 32 74 58
40678 61 85 4 75
40740 30 24 31 46
40889 39 23 63 37
40945 29 44 21 19
410
41188 44 39 81 43
41226 18 10 73 3
41362 63 74 53 60
414 0 65 84 54 21
41514 25 76 13 2
416
41761 57 77 0 92
41812 27 68 89 72
41986 21 44 43 40
42041 74 26 73 66
421 9 59 32 3 8
422
42320 93 99 67 28
42453 52 19 72 23
42531 65 25 88 10
42661 37 43 9 76
42760 33 87 3 96
428
42983 15 46 70 80
43090 69 1 6 64
43143 33 17 99 53
43298 25 30 8 31
43366 94 13 12 75
434
43518 52 83 3 19
43641 51 60 13 54
43733 5 69 99 8
43827 44 31 34 4
43917 25 1 68 79
440
44192 23 93 26 75
44250 28 30 42 25
44377 83 56 95 34
44457 13 27 65 81
445 7 71 40 94 9
446
44716 28 3 39 82
448 7 99 44 69 8
44949 51 93 34 6
45060 84 0 92 2
45176 88 38 68 24
452
45323 99 47 36 77
45420 90 4 78 93
45571 88 52 28 92
45697 31 25 95 39
457 6 21 68 45 53
458
459 0 51 69 75 56
46010 47 12 52 6
46165 87 77 85 42
46298 23 25 97 30
46384 86 67 35 22
464
46524 36 70 99 98
46694 1 59 67 35
46781 25 6 61 68
46872 53 9 40 92
46997 73 91 41 10
470
471 1 78 3 66 15
47239 29 24 76 98
47310 25 90 30 55
47441 96 83 72 51
47546 87 42 26 44
476
47720 60 59 4 21
47850 71 45 31 1
47949 75 37 54 28
48084 46 98 24 2
48196 16 51 79 35
482
48359 46 65 39 95
48492 5 10 63 36
48516 90 56 89 70
48683 45 24 64 74
48755 27 54 2 51
488
48916 23 63 62 57
49017 26 58 83 60
49139 68 22 98 48
49265 50 76 3 14
49324 75 25 7 86
494
49535 26 74 81 56
49662 84 34 30 33
497 5 73 21 15 93
49878 77 69 49 43
49944 11 54 51 87
500
50156 78 74 24 39
50270 85 6 84 9
50397 65 16 82 5
504 2 12 20 79 42
50514 37 15 69 26
506
50742 31 88 46 71
50865 97 11 34 48
50985 96 27 0 7
51041 9 5 72 53
51137 93 98 57 64
512
513 1 10 55 57 99
51461 92 82 86 56
51519 16 43 84 87
51642 6 79 46 17
51751 44 64 73 35
518
51917 0 84 15 34
52057 92 41 82 40
521 8 38 16 35 4
522 6 33 79 97 96
52352 11 21 10 73
524
52589 2 81 22 34
52677 87 84 11 80
52785 67 96 49 40
52819 3 76 98 16
52929 1 20 35 82
530
531 4 57 84 91 6
532 8 16 69 1 0
53373 2 41 20 5
53498 40 96 82 93
53512 51 23 68 83
536
53764 52 50 1 94
53897 58 24 90 5
539 9 75 3 61 37
54084 91 34 29 8
54130 74 4 18 45
542
54392 63 96 34 9
54414 25 89 4 98
54523 67 90 74 77
54661 3 93 55 82
54751 65 48 36 8
548
54970 65 20 76 50
55017 1 33 15 22
551 3 98 44 91 55
55259 83 2 67 87
55314 63 56 86 37
554
55561 53 45 88 36
55696 74 15 39 42
55732 55 44 46 86
55879 94 27 54 57
55965 3 21 17 75
560
56114 26 18 54 20
56224 22 90 0 16
56370 28 33 12 27
56445 95 30 65 75
56519 81 39 2 84
566
56748 23 67 75 86
56840 46 87 27 93
56979 44 13 3 53
57024 98 54 34 8
57149 1 39 28 35
572
573 3 29 16 5 80
57486 50 6 45 88
57522 71 73 34 94
57681 28 68 20 30
57733 37 95 35 57
578
57949 1 6 26 30
58056 5 63 79 15
58134 52 87 19 66
58259 96 80 42 58
58350 32 72 38 74
584
58583 90 18 23 64
58687 89 62 31 66
58781 49 9 8 56
58851 85 63 32 50
58995 47 11 98 17
590
59168 83 80 25 48
59272 39 7 20 30
59332 38 12 66 64
59429 10 44 23 45
59558 15 91 81 95
596
59790 67 30 22 58
59835 50 63 59 19
59954 4 41 38 65
60043 47 56 96 20
60187 57 33 37 49
diff --git a/2021/day4/input.example b/2021/day4/input.example
new file mode 100644
index 0000000..669a51d
--- /dev/null
+++ b/2021/day4/input.example
@@ -0,0 +1,19 @@
17,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
2
322 13 17 11 0
4 8 2 23 4 24
521 9 14 16 7
6 6 10 3 18 5
7 1 12 20 15 19
8
9 3 15 0 2 22
10 9 18 13 17 5
1119 8 7 25 23
1220 11 10 24 4
1314 21 16 12 6
14
1514 21 17 24 4
1610 16 15 9 19
1718 8 23 26 20
1822 11 13 6 5
19 2 0 12 3 7
diff --git a/2021/day4/src/main.rs b/2021/day4/src/main.rs
new file mode 100644
index 0000000..4b6150f
--- /dev/null
+++ b/2021/day4/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 fd = reader.lines();
19 let mut called_numbers: Vec<i32> = Vec::new();
20
21 if let Ok(called_nums) = fd.next().unwrap() {
22 called_numbers = called_nums.split(",").map(|x| x.parse().unwrap()).collect();
23 }
24
25 fd.next(); /* Skip the empty line */
26
27 let mut boards: Vec<(Vec<(i32, bool)>, bool)> = Vec::new();
28
29 'outer: loop {
30 let mut miniboard: Vec<(i32, bool)> = Vec::new();
31 for _ in 0..5 {
32 /* New Board */
33 if let Some(maybe_line) = fd.next() {
34 if let Ok(line) = maybe_line {
35 for (a, b) in line
36 .split_whitespace()
37 .map(|x| x.parse().unwrap())
38 .zip([false].iter().cycle())
39 {
40 miniboard.push((a, *b));
41 }
42 }
43 } else {
44 break 'outer;
45 }
46 }
47 boards.push((miniboard.clone(), false));
48 fd.next(); /* Skip the empty line */
49 }
50
51 for bingo in called_numbers {
52 for (board, _) in boards.iter_mut() {
53 for (num, mark) in board.iter_mut() {
54 if *num == bingo {
55 *mark = true;
56 }
57 }
58 }
59
60 let mut count;
61 for (board, won) in boards.iter_mut() {
62 for row in 0..5 {
63 count = 0;
64 for i in row * 5..(row * 5) + 5 {
65 if board[i].1 {
66 count += 1;
67 }
68 }
69 if count == 5 {
70 *won = true;
71 }
72 }
73
74 for column in 0..5 {
75 count = 0;
76 for i in (0..5).map(|x| 5 * x + column) {
77 if board[i].1 {
78 count += 1;
79 }
80 }
81 if count == 5 {
82 *won = true;
83 }
84 }
85 }
86 if boards.len() == 1 {
87 let mut score: i32 = 0;
88 if let Some(board) = boards.get(0) {
89 for (winnynum, marked) in board.0.iter() {
90 if !marked {
91 score += winnynum;
92 }
93 }
94 println!("score: {}", score * bingo);
95 }
96 }
97 boards.retain(|x| !x.1);
98 }
99}