summaryrefslogtreecommitdiffstats
path: root/2021
diff options
context:
space:
mode:
Diffstat (limited to '2021')
-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
4 files changed, 727 insertions, 0 deletions
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}