-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay20.rs
69 lines (56 loc) · 1.72 KB
/
Day20.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
use std::collections::HashMap;
use std::env;
use std::io;
use std::iter::repeat;
type Img = HashMap<(i32, i32), bool>;
fn bools_to_size<T>(bools: T) -> usize
where
T: IntoIterator<Item = bool>,
{
bools.into_iter().fold(0, |a, b| a * 2 + b as usize)
}
fn main() {
let mut lines = io::stdin().lines().map(Result::unwrap);
let algo: Vec<_> = lines.next().unwrap().chars().map(|c| c == '#').collect();
let mut img: Img = lines
.skip(1)
.enumerate()
.flat_map(|(y, row)| {
row.chars()
.enumerate()
.map(move |(x, v)| ((y as i32, x as i32), v == '#'))
.collect::<Vec<_>>()
})
.collect();
let top = if env::args().nth(1).unwrap() == "1" {
2
} else {
50
};
for i in 1..=top {
let empty = if i % 2 == 0 {
algo[0]
} else {
algo[bools_to_size(repeat(algo[0]).take(9))]
};
let (mut min_y, mut max_y, mut min_x, mut max_x) = (0, 0, 0, 0);
for (y, x) in img.keys() {
min_y = min_y.min(*y);
max_y = max_y.max(*y);
min_x = min_x.min(*x);
max_x = max_x.max(*x);
}
let mut new_img: Img = HashMap::new();
for y in min_y - 1..=max_y + 1 {
for x in min_x - 1..=max_x + 1 {
let img = &img; // Move reference, not img
let idx = bools_to_size((-1..=1).flat_map(|dy| {
(-1..=1).map(move |dx| *img.get(&(y + dy, x + dx)).unwrap_or(&empty))
}));
new_img.insert((y, x), algo[idx]);
}
}
img = new_img;
}
println!("{}", img.values().filter(|x| **x).count());
}