diff options
-rw-r--r-- | day20/Cargo.toml | 9 | ||||
-rw-r--r-- | day20/resources/input.txt | 102 | ||||
-rw-r--r-- | day20/src/main.rs | 138 |
3 files changed, 249 insertions, 0 deletions
diff --git a/day20/Cargo.toml b/day20/Cargo.toml new file mode 100644 index 0000000..6696ef0 --- /dev/null +++ b/day20/Cargo.toml | |||
@@ -0,0 +1,9 @@ | |||
1 | [package] | ||
2 | name = "day20" | ||
3 | version = "0.1.0" | ||
4 | authors = ["Federico Igne <git@federicoigne.com>"] | ||
5 | edition = "2018" | ||
6 | |||
7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
8 | |||
9 | [dependencies] | ||
diff --git a/day20/resources/input.txt b/day20/resources/input.txt new file mode 100644 index 0000000..3fec6c5 --- /dev/null +++ b/day20/resources/input.txt | |||
@@ -0,0 +1,102 @@ | |||
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 | .#...#.####...##..#.#.####.###..#.#.#..####.###.####.##.#.#.##...#.#...#..#.##.......#....#..###...# | ||
diff --git a/day20/src/main.rs b/day20/src/main.rs new file mode 100644 index 0000000..a11d49a --- /dev/null +++ b/day20/src/main.rs | |||
@@ -0,0 +1,138 @@ | |||
1 | use std::fs; | ||
2 | use std::path::Path; | ||
3 | use std::collections::VecDeque; | ||
4 | use std::fmt::Display; | ||
5 | use std::fmt::Formatter; | ||
6 | use std::fmt::Result; | ||
7 | |||
8 | #[derive(Debug)] | ||
9 | struct Map { | ||
10 | step: usize, | ||
11 | pixels: VecDeque<VecDeque<u8>> | ||
12 | } | ||
13 | |||
14 | impl Map { | ||
15 | fn pad(&mut self, pix: u8) { | ||
16 | let h = self.pixels.len(); | ||
17 | let w = self.pixels[0].len(); | ||
18 | let pix = if pix == 0 { 0 } else { u8::MAX }; | ||
19 | if self.pixels[h-1].iter().any(|&b| b > 0) || self.pixels[h-2].iter().any(|&b| b > 0) { | ||
20 | let empty = VecDeque::from([pix].repeat(w)); | ||
21 | self.pixels.push_back(empty.clone()); | ||
22 | self.pixels.push_back(empty); | ||
23 | } | ||
24 | if self.pixels[0].iter().any(|&b| b > 0) || self.pixels[1].iter().any(|&b| b > 0) { | ||
25 | let empty = VecDeque::from([pix].repeat(w)); | ||
26 | self.pixels.push_front(empty.clone()); | ||
27 | self.pixels.push_front(empty); | ||
28 | } | ||
29 | if self.pixels.iter().any(|v| v[w-1] & 3 != 0) { | ||
30 | self.pixels.iter_mut().for_each(|v| v.push_back(pix)); | ||
31 | } | ||
32 | if self.pixels.iter().any(|v| v[0] >= (1<<6)) { | ||
33 | self.pixels.iter_mut().for_each(|v| v.push_front(pix)); | ||
34 | } | ||
35 | } | ||
36 | |||
37 | fn enhance(mut self, algorithm: &[u8]) -> Map { | ||
38 | let padpix = if algorithm[0] == 0 { 0 } else { algorithm[[algorithm.len()-1,0][self.step % 2]] }; | ||
39 | self.pad(padpix); | ||
40 | let h = self.pixels.len(); | ||
41 | let w = self.pixels[0].len(); | ||
42 | let mut pixels = VecDeque::with_capacity(h); | ||
43 | for i in 0..h { | ||
44 | let mut row = VecDeque::with_capacity(w); | ||
45 | for j in 0..w { | ||
46 | let mut byte = 0; | ||
47 | for p in 0..8 { | ||
48 | let pix = { | ||
49 | if i < 1 || i > h-2 || (j < 1 && p < 1 ) || (j > w-2 && p > 6) { | ||
50 | if algorithm[0] == 0 { 0 } else { algorithm[[0,algorithm.len()-1][self.step % 2]] } | ||
51 | } else { | ||
52 | algorithm[self.index(i,8*j+p)] | ||
53 | } | ||
54 | }; | ||
55 | byte = (byte<<1) | pix; | ||
56 | } | ||
57 | row.push_back(byte); | ||
58 | } | ||
59 | pixels.push_back(row); | ||
60 | } | ||
61 | Map { step: self.step+1, pixels } | ||
62 | } | ||
63 | |||
64 | fn pix(&self, y: usize, x: usize) -> u8 { | ||
65 | (self.pixels[y][x/8] >> (7-x%8)) & 1 | ||
66 | } | ||
67 | |||
68 | fn index(&self, y: usize, x: usize) -> usize { | ||
69 | let mut idx = 0; | ||
70 | for i in y-1..y+2 { | ||
71 | for j in x-1..x+2 { | ||
72 | idx = (idx << 1) | self.pix(i,j) as usize | ||
73 | } | ||
74 | } | ||
75 | idx | ||
76 | } | ||
77 | |||
78 | fn lights(&self) -> u32 { | ||
79 | self.pixels.iter().flat_map(|v| v.iter().map(|&b| b.count_ones())).sum() | ||
80 | } | ||
81 | } | ||
82 | |||
83 | impl Display for Map { | ||
84 | fn fmt(&self, f: &mut Formatter<'_>) -> Result { | ||
85 | self.pixels.iter().for_each(|v| { | ||
86 | v.iter().for_each(|b| write!(f, "{}", format!("{:08b}",b).chars().map(|c| if c == '1' { '#' } else { '.' }).collect::<String>()).unwrap()); | ||
87 | write!(f, "\n").unwrap() | ||
88 | }); | ||
89 | Ok(()) | ||
90 | } | ||
91 | } | ||
92 | |||
93 | fn main() { | ||
94 | let input = Path::new("resources").join("input.txt"); | ||
95 | let content = fs::read_to_string(input).expect("Unable to read input file"); | ||
96 | let (mut map, algorithm) = parse_input(&content); | ||
97 | map = map.enhance(&algorithm).enhance(&algorithm); | ||
98 | println!("Ex1: The result is {}", map.lights()); | ||
99 | for _ in 0..50-2 { | ||
100 | map = map.enhance(&algorithm); | ||
101 | } | ||
102 | println!("Ex2: The result is {}", map.lights()); | ||
103 | } | ||
104 | |||
105 | fn parse_input(input: &str) -> (Map, Vec<u8>) { | ||
106 | let (algorithm,pixels) = input.split_once("\n\n").expect("Malformed input"); | ||
107 | let algorithm: Vec<u8> = algorithm.chars().map(|c| (c == '#') as u8).collect(); | ||
108 | let pixels: VecDeque<VecDeque<u8>> = pixels.lines().map(|l| { | ||
109 | let bits: Vec<u8> = l.chars().map(|c| (c == '#') as u8).collect(); | ||
110 | bits.chunks(8).map(|c| c.iter().fold(0, |a,b| (a<<1)|b) << (8-c.len())).collect() | ||
111 | }).collect(); | ||
112 | (Map { step: 0, pixels }, algorithm) | ||
113 | } | ||
114 | |||
115 | #[cfg(test)] | ||
116 | mod tests { | ||
117 | use super::*; | ||
118 | |||
119 | const INPUT: &str = "..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..# | ||
120 | |||
121 | #..#. | ||
122 | #.... | ||
123 | ##..# | ||
124 | ..#.. | ||
125 | ..###"; | ||
126 | |||
127 | #[test] | ||
128 | fn input_parse() { | ||
129 | let (mut map,algorithm) = parse_input(INPUT); | ||
130 | println!("{}", map); | ||
131 | map = map.enhance(&algorithm); | ||
132 | println!("{}", map); | ||
133 | map = map.enhance(&algorithm); | ||
134 | println!("{}", map); | ||
135 | assert_eq!(35, map.lights()); | ||
136 | |||
137 | } | ||
138 | } | ||