-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-20.rs
135 lines (118 loc) · 4.34 KB
/
day-20.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
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
use std::{collections::HashMap, fmt::Display, str::FromStr};
const INPUT: &str = include_str!("day-20.input");
#[derive(Default)]
struct Image {
pixels: HashMap<(isize, isize), bool>,
unset: bool,
}
impl Image {
fn lit(&self) -> usize {
self.pixels.iter().filter(|(_, &set)| set).count()
}
fn get(&self, x: isize, y: isize) -> bool {
*self.pixels.get(&(x, y)).unwrap_or(&self.unset)
}
fn set(&mut self, x: isize, y: isize, value: bool) {
self.pixels.insert((x, y), value);
self.pixels.entry((x - 1, y - 1)).or_insert(self.unset);
self.pixels.entry((x, y - 1)).or_insert(self.unset);
self.pixels.entry((x + 1, y - 1)).or_insert(self.unset);
self.pixels.entry((x - 1, y)).or_insert(self.unset);
self.pixels.entry((x + 1, y)).or_insert(self.unset);
self.pixels.entry((x - 1, y + 1)).or_insert(self.unset);
self.pixels.entry((x, y + 1)).or_insert(self.unset);
self.pixels.entry((x + 1, y + 1)).or_insert(self.unset);
}
fn chunk(&self, x: isize, y: isize) -> usize {
let mut chunk = self.get(x - 1, y - 1) as usize;
chunk = (chunk << 1) + self.get(x, y - 1) as usize;
chunk = (chunk << 1) + self.get(x + 1, y - 1) as usize;
chunk = (chunk << 1) + self.get(x - 1, y) as usize;
chunk = (chunk << 1) + self.get(x, y) as usize;
chunk = (chunk << 1) + self.get(x + 1, y) as usize;
chunk = (chunk << 1) + self.get(x - 1, y + 1) as usize;
chunk = (chunk << 1) + self.get(x, y + 1) as usize;
chunk = (chunk << 1) + self.get(x + 1, y + 1) as usize;
chunk
}
fn enhance(&mut self, lut: &[bool]) {
let mut new = Self {
unset: lut[self.unset as usize * 0x1ff],
..Default::default()
};
for (&(x, y), _) in self.pixels.iter() {
new.set(x, y, lut[self.chunk(x, y)]);
}
*self = new;
}
}
impl FromStr for Image {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut image = Image::default();
for (y, line) in s.lines().enumerate() {
for (x, c) in line.chars().enumerate() {
image.set(x as isize, y as isize, c == '#');
}
}
Ok(image)
}
}
impl Display for Image {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let (mut min_x, mut max_x) = (isize::MAX, isize::MIN);
let (mut min_y, mut max_y) = (isize::MAX, isize::MIN);
for (&(x, y), _) in self.pixels.iter() {
min_x = min_x.min(x);
max_x = max_x.max(x);
min_y = min_y.min(y);
max_y = max_y.max(y);
}
for y in min_y..=max_y {
for x in min_x..=max_x {
write!(f, "{}", if self.get(x, y) { '#' } else { '.' })?;
}
writeln!(f)?;
}
Ok(())
}
}
fn main() {
let (lut, mut image) = parse(INPUT);
image.enhance(&lut);
image.enhance(&lut);
println!("part 1: {}", image.lit());
for _ in 2..50 {
image.enhance(&lut);
}
println!("part 2: {}", image.lit());
}
fn parse(input: &str) -> (Vec<bool>, Image) {
let (lut, image) = input.trim().split_once("\n\n").unwrap();
let lut: Vec<_> = lut.trim().chars().map(|c| c == '#').collect();
let image: Image = image.trim().parse().unwrap();
(lut, image)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example() {
let input = "..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
#..#.
#....
##..#
..#..
..###";
let (lut, mut image) = parse(input);
assert!(lut[34]);
eprintln!("{}", image);
assert_eq!(image.chunk(2, 2), 34);
image.enhance(&lut);
eprintln!("{}", image);
assert!(image.get(2, 2));
image.enhance(&lut);
eprintln!("{}", image);
assert_eq!(image.lit(), 35);
}
}