diff options
Diffstat (limited to '2021/day4/src/main.rs')
| -rw-r--r-- | 2021/day4/src/main.rs | 99 |
1 files changed, 99 insertions, 0 deletions
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 @@ | |||
| 1 | use std::env; | ||
| 2 | use std::fs::File; | ||
| 3 | use std::io::{BufRead, BufReader}; | ||
| 4 | |||
| 5 | fn 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 | } | ||
