-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.rs
More file actions
147 lines (125 loc) · 3.3 KB
/
day04.rs
File metadata and controls
147 lines (125 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use aoc_runner_derive::{aoc, aoc_generator};
use ndarray::Array2;
type Parsed = (Vec<u32>, Vec<Array2<(u32, bool)>>);
#[aoc_generator(day4)]
fn parse_input(input: &str) -> Parsed {
let mut split = input.split("\n\n");
let nums_str = split.next().unwrap().to_owned();
let cards_str: Vec<&str> = split.collect();
let nums: Vec<u32> = nums_str
.split(',')
.map(|s| s.parse::<u32>().unwrap())
.collect();
let cards: Vec<Array2<(u32, bool)>> = cards_str
.iter()
.map(|c_s| {
Array2::from_shape_vec(
(5, 5),
c_s.split_ascii_whitespace()
.map(|s| (s.parse().unwrap(), false))
.collect(),
)
.unwrap()
})
.collect();
(nums, cards)
}
fn check_board(board: &Array2<(u32, bool)>) -> bool {
board.rows().into_iter().any(|r| r.iter().all(|c| c.1))
|| board.columns().into_iter().any(|r| r.iter().all(|c| c.1))
}
#[aoc(day4, part1)]
fn part1(input: &Parsed) -> u32 {
let nums = &input.0;
let mut boards = input.1.to_owned();
for n in nums {
boards
.iter_mut()
.for_each(|b| b.iter_mut().filter(|c| c.0 == *n).for_each(|c| c.1 = true));
for b in &boards {
if check_board(b) {
return b.iter().filter(|c| !c.1).map(|(n, _)| *n).sum::<u32>() * n;
}
}
}
unreachable!()
}
#[aoc(day4, part2)]
fn part2(input: &Parsed) -> u32 {
let nums = &input.0;
let mut boards: Vec<(Array2<(u32, bool)>, i32)> =
input.1.iter().map(|b| (b.clone(), -1)).collect();
let mut won = 0;
for n in nums {
boards
.iter_mut()
.filter(|(_, w)| *w == -1)
.for_each(|(b, _)| b.iter_mut().filter(|c| c.0 == *n).for_each(|c| c.1 = true));
boards
.iter_mut()
.filter(|(_, w)| *w == -1)
.for_each(|(b, w)| {
if check_board(b) {
*w = won;
won += 1;
}
});
if boards.iter().filter(|(_, w)| *w == -1).count() == 0 {
return boards
.iter()
.find(|(_, w)| *w == won - 1)
.unwrap()
.0
.iter()
.filter(|c| !c.1)
.map(|(n, _)| *n)
.sum::<u32>()
* n;
}
}
unreachable!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sample1() {
let input = "7,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
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7";
assert_eq!(part1(&parse_input(input)), 4512);
}
#[test]
fn sample2() {
let input = "7,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
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7";
assert_eq!(part2(&parse_input(input)), 1924);
}
}