-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.rs
78 lines (71 loc) · 1.71 KB
/
day11.rs
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
fn solve(data: &str, n: usize) -> usize {
let mut counts = Vec::<usize>::new();
solve1(
&data
.lines()
.map(|line| {
let mut count = 0;
counts.extend(std::iter::repeat(0).take(line.len()).skip(counts.len()));
for (i, c) in line.char_indices() {
if c == '#' {
counts[i] += 1;
count += 1;
}
}
count
})
.collect::<Vec<_>>(),
n,
) + solve1(&counts, n)
}
fn solve1(data: &[usize], n: usize) -> usize {
data.iter()
.enumerate()
.map(|(i, &a)| {
if a == 0 {
0
} else {
data[i + 1..]
.iter()
.scan(0, move |m, &b| {
*m += if b == 0 { n } else { 1 };
Some(*m * a * b)
})
.sum()
}
})
.sum()
}
pub fn part1(data: &str) -> usize {
solve(data, 2)
}
pub fn part2(data: &str) -> usize {
solve(data, 1000000)
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
static EXAMPLE: &str = indoc! {"
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
"};
#[test]
fn part1_examples() {
assert_eq!(374, solve(EXAMPLE, 2));
}
#[test]
fn part2_examples() {
assert_eq!(1030, solve(EXAMPLE, 10));
assert_eq!(8410, solve(EXAMPLE, 100));
}
}