adventofcode

git clone https://git.ce9e.org/adventofcode.git

commit
0919a026c650a162267b5dd40b6306cd0714ecdf
parent
8e8e79228da54b61f1b9356a51bc8c22ec65311f
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2021-12-20 08:19
day 20

Diffstat

A 2021/20/input.txt 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A 2021/20/part1.rs 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A 2021/20/test.txt 7 +++++++

3 files changed, 203 insertions, 0 deletions


diff --git a/2021/20/input.txt b/2021/20/input.txt

@@ -0,0 +1,102 @@
   -1     1 #..###.##....#.#.#...#.#.#...##...####......##.##..###...#.####..#..#..#####..#.##.....#..#.###.##...#.#.....#...##.##.##...#####.#.#.#.##.###.#.##..#.##.##.#..#...####.#.#.....#..#.....###.#..#.#.#.#...#.###..#.###..##.#..#...##...####.#.........###..#.##.#..#.#...##.#.#.##.####.###....#####..###...##..#####..###..##..#.#.#..###.##.###..#.#######.####..#....###.##...#.####..#.#######...###...##.##.###...##..#.....#.###....#..#.#..###.#...#######.#...##..#.#..##.#...##.#..##.##..#...#.#.##.####........#..#.
   -1     2 
   -1     3 #.###...#..#...###..#..#.#....#####.#...#..#####...#.##...#....#.#.#..#.#.........#..##..#..#.#..#..
   -1     4 ###.#..##.#.##.#......#.###.##..###.#..#.###..####.#...#.....#...#.#.#..#######..#.#.##.#.#####..###
   -1     5 .....##...#..#.#..#....###.##..######.#..#.###.###.###.#.#.##.####.#..#.#....##..#.##.#.........#.#.
   -1     6 .#####...##...###..####.##.##.#..###.#.#..##..###...##.#..#...###.###.#.##.#.#....##.#.##.#.#..##.#.
   -1     7 ..#.....#####.#.###.###..#..##.##...#######.#...#...#.##.#.###....#.....####..#.....##.#####.###..##
   -1     8 .###.#####.#..#...#.#....####.#..###.##...##..#..##..#........###..#..###....###.#.#..##..#...#.#..#
   -1     9 ##.##.#.##.#...###.###.###....#...##.#.#..#.##...#..#.#.#..####.#.###....#.##.#.##.#..#.#.....###.#.
   -1    10 ..#..#.###..##..#####.##.#.#..#..##...##..#..##..##....###..#.###..##..#.#..#..#..#.##.#..##.##.....
   -1    11 ###.#.#.#...#.#...#.###..#.###....##..#.##.###.....###.#..#..#####.##.#.##..#..##..###.###..###....#
   -1    12 ###......##.##..#.......####...##.#......#.#.....#..#.#.###........##.#.####.#.#.##...#..###.#.#....
   -1    13 .##.##...#.#..#...#####.#####..#.......##..##.#.....####.###.#..#...##.#.###.#..##.##..####..#..##.#
   -1    14 .#...#.#.#.#....###......##.....#...##...###...##....#..##.#..##.#.##.#...##.#.#.......##.#....#.#.#
   -1    15 #..#....####...#.#.#.####...###.###.######..#.#...##.#...#.##......#.#...#.#.###.#.#.#..####..#.#...
   -1    16 ..#..##....##.##...###..###.#...#.#...#.##..#..#.#.####..#...#..#.#.##....#....#.##..#.#..#.#....#.#
   -1    17 .#.#.########.##.#....##.#..###....#.#.###.#...##....###......##########.#..#.#.####...#..##...#..##
   -1    18 #..##..#..#.#.#####..#####...#.###..####..##.#.#.####.#..#.#..####......#..#..##....##...#..###.#.##
   -1    19 ..###.#.#.##.##.#.#...###.#..#....####..###.##..##.#..##....###.#####.#..#...###.#.#..##...#...#####
   -1    20 #.##.##.#..##.###.#.#.##..#...#...###..#..##.#..##....#####...#.#.#.##...####.##....#.#.##.#.##.#...
   -1    21 ##...##.#.###.####.##......##...#..#..#.....##..##..#.###.#..##.#.##.##..#..####.#########...##..#..
   -1    22 .###..#.#..##..##..#..#...#.#..###.....#.#####..##.####...#.#...######.........####...........#..#.#
   -1    23 #..#...##.#.##...##..##..........#..#...#..#..#.#..#.........######..####.#.##...##.##.##.....##..##
   -1    24 #..##.#.###..##..####.#..######....#....####...##.#.#.#.#..#.##..#..#..###..#.#.#.#.#....#..##.#.#..
   -1    25 .....##.......#.#.##.......##.##.#..###...###....##.###....##.#.####.###.##..##.#..##...###........#
   -1    26 ##..##....#.###..###.##.....##.#...##....##..#....#....###...#..#...###..#.####....#.......##.....##
   -1    27 #..#..#.#..#...##.#.##.##...#.#.##..##..#.##..#.#...#...#.###.####..#....#####...#.###.##.####..#.##
   -1    28 ..##..##....####..#...###.############.##.#.##.####.#..#.##.#..##....#####..#.#####...###.#..#.##...
   -1    29 ...#.##.#.###.....###.#.#...##.#.#.##.##.#..##.#.##.#.##.#..#.###...#####.#..#.#..##...#.####..##..#
   -1    30 #.#.###.####..#.#.###...##...###..#......#..##.....###.....#.##...##.#..#.#.####.#..#.#.#.##.####.##
   -1    31 .###...####..##..#..###.#.#.#.##..##.##.#..#..##.##.#..#....#.#.###...#...#....##..##.#.##.######.##
   -1    32 ###.##....##.#.####..#########.....#.###...######.#.#....#...##...####.##.#.#.#####.....##....#.#.##
   -1    33 ######.#..#......#......###.##.#.###.#####..#.###.......####.#.###.#.#.##.##.....####...#..#.#.#####
   -1    34 ....##.#.#.#.##..##.###..#.###.##.##..###..#..#####.#####........#..##.#..##........#.##..#..#..#...
   -1    35 .###..###.##.###.#.##.####.##.#.#...####.#..#.######.#.#####.#......#####.##..##.#.#.#.##.#..###.#.#
   -1    36 #.##....###...#.....#..##.##.##.#..#.#..#.#.#...##..####.#..#.......####.###.#.####..#.#..##..##.#..
   -1    37 ##.#########..#....#....##...##.###..##.......####...####.#..#...###..##..##.#.#####..##.#....#..###
   -1    38 ..#..#####..####.####...#..#.#...####...#..###..#...#...########..#.#.#..###....####.....###.##...#.
   -1    39 ##.###.##..###.#.#..#..#.####...#.#...###..##...#.###.....##.#.#.#.##.#.#...###..###.###..#.#..#.#.#
   -1    40 #.##..#.##..##.##...#.##.....##.#..#..###.##..##.#.##..#####..#####.###.##..###...#.#.#.##.##...###.
   -1    41 ..#.##.###.##.###.##.##.##......###..#.##.#..#...##.##.###..#..###.#......#.#..##..#.#..#####.......
   -1    42 ..##.#.###.###.###.##..####.#..####.#.##.#..#.######.#..##.#.#.#...#######.##..###..####.##..#..###.
   -1    43 .####.##...#.#..#...##..#.#....#.#.##.##.#.#.#..##.#.#......####....###.####..#######.##.#....#.#.#.
   -1    44 ###.#.##.......#...###...##.#.##.##...#..#.#..###.####.#.###.#.#.##.#.#.###....#....#.#....#..#.#..#
   -1    45 .#......#...#.#...#.##.#.###...#...##...#.###.#..#..#...#.#.#.###.##...###..##..###..#.#.##.#...####
   -1    46 ##.##...##..##...##..#...###...###..##.##.#......#..###.....##..#.#.#####...#######.#.####.#........
   -1    47 #..#.....##..##.#.##..#.####......###..#...#.###.######..#.###..#.##..#.#....###..#..########..#.#..
   -1    48 ##.#..#......###.#.##....#.#...##...##.#..##......###..####.###.###.#.....#...##....#####.###..##..#
   -1    49 ###.......#.#.##....##..#.#..####.#.#..#.#..#...###.#.#.###.#####...####.#..#.##..#.....#.#.....#...
   -1    50 .##.#..###....#.#####..###...##.#...####.###...#.######.#..###...#...#######.#.##.#....##.#.##...###
   -1    51 #....#..#.#.#####..###.##..#.####..##.#.#....#.#.##.....##...#..#.###.#...#....#..#....###.#..#.###.
   -1    52 #..#.#######.##..#.#..#.##..#######..####....#.#.###..###......###.####.#.....##..#.##.#.....###..##
   -1    53 .###.#..##.......###..#.##....#......#.##..#.##..##.####.....###.#...#.#..##....#.#.###.###.#.#.##.#
   -1    54 ...#####.#.###....#..#.....###.#.....#..#..#..###...###.###..#....#####.#.###..#.....#..######....#.
   -1    55 #..####...#...##..##.####..###.....##.###..#.###.#.#.##..#..#####...##.#...##.#..##.##.##.#....#....
   -1    56 #...#.####......#.##..##.#.##.#.#####..#######.##.##.#.#.......#.#.#...##.####.###..#..#....#####.##
   -1    57 ..##...###...##.#..##..#...##.#.......#..#...#.#....#.###..##.#.###..##.#..####.######..#..#.##.#..#
   -1    58 .##.....#.##..#..##......###.###.#......####..####.#.#.#...#..#..#..###..#..#...#.####.#.##..###.###
   -1    59 ........###.####...#...##..##..#.###..##..##.#...#..##.##....###.##.#.#..###.#.####....##..#.####.##
   -1    60 .#..#...###.##.#....#.#.##...#..##..#.##...#...#.######.##.##.#####....##.###..####.....#.#..#...#.#
   -1    61 #.#....##....#.######.##..#...##.##.#..#..#.#...#.##..##....###.#.#.#.#.#.######...##..###.#.##...#.
   -1    62 ..#....####.#.#############.####...#####......####.##.##.##..#..##.#....####..####.#..####.#.#..#.##
   -1    63 #.##.#..#..####...#######....#...####.#..####....#####..##..#######..#.#....##.......#.#..#..####.#.
   -1    64 #.#..#.#.#...####..#.....####.#...###...###.#.##.##.#.##.#........#.##..##.###.##.......#.###.....##
   -1    65 ..#.#.###.#.###.#.....###.#...##...#..#.....###.#..#..#..#...##..#..#.##..#..#.#.##..##.....#...##..
   -1    66 ..##.###.##.##..##.##.####..##..#.#.##.##.#...##...##....###..##...#..###.##.##.....###.#.##.##.#.##
   -1    67 #.####...##.###..####...#.##.#####.#....#.#..#.....#......##..##.######...#....####...#..#.#..#..#.#
   -1    68 .###..#.#.##.#.....###.#..#..#..#...####.#.####.#...###.##....###..##.##..#.##.#...#.#.#...####..##.
   -1    69 ##..##..####.####.#.##.#.#..####..##.######.###...##.##..#.#####.###..##...#.....##.##..#..###..####
   -1    70 #..##.####.#.#.#####.....##.####..##..###...##...##...##....#.#.#...######...#...........##..###.##.
   -1    71 ...###.##...#.#.##...####...#..#.######.####.##.##.#....#.#.###..#.##...###.#...##.....##...###..##.
   -1    72 .....#.###.##.....##.###.#.#..###..##..#..#.#.###..####......#.#####..##.####..###..##.#######.##.##
   -1    73 #...#..#...##..#...####.##...#.#..##.......#.#...#.##.####.....#########...##....####.......#.###.##
   -1    74 .#.....#.....####...#####..###.#.........##..##.##.#...#.#..#.#.#.#.#.###.#####..####..#......##.#.#
   -1    75 #..#..###.#..#######.#..###.##..#..##.#..#..#.##.###.#..###.###..........#.###.#####.#.##.#.#...##..
   -1    76 .##..###.#.#####.##.####.#.#..#..###....##.#.#..###.#.##..#.#..###..##.#.##.....#.#.#.#.#.####.#.##.
   -1    77 ..##.###...#....##.##.#####.##....#..##.######....###..#.#.##.###.#.......######.#..#..#..###.##....
   -1    78 #.#..#.##.###...#..#####.##.#.###.#.###.......#######....#.####...####.##..#.#.###.##..#..#..###..#.
   -1    79 #......#.##.##....##.#..#.#....##..#.###.....#.##...#.#..#...##.####......##..#.#.#...#...#.....#.##
   -1    80 #....##.#.##.#.......#.##.#.###..#...###.#.###.##......#..#####..##.#..#....######.##.#..#...##..##.
   -1    81 ######.#.....#.#..##.##.....##.#..####.###..#.#.###....#..###..#..##.#..#.###..#.##...####....#.#...
   -1    82 #.#..###..#####..#....#.#...##....###..##....#.##.#......#.#.###.....#.#.#..#.#..###.#....####......
   -1    83 #..#.###.###.#.#.#..#.##..#.#.###.#...........#.#...#.##..#....##..#.#.##..##.#...##..#.##.##.#..#.#
   -1    84 #.....#..#.#..##.##.######.#######....###.###...#####..#.....#..##.#.##.#.###...#..##.....###...#...
   -1    85 #####.#.#..#.#.##..#.####.#...###.##.#....##.##.####.#...####.##.#...####..#..#.####.###....###.....
   -1    86 #...#.......##.#...#..####...#.#..#.##.###..#.#.#.##...#.###......##....########..##.####.##.####.##
   -1    87 #..#...##.###....##.##.#.##.##..#.#.#...###..#.#........###########.##..###.###.#..#....#.###....##.
   -1    88 .#.#..##..##.##.#....##....###.##..###..#.###.#.###.#.###..######..###...####.....#.#...#.#.#..##.#.
   -1    89 ##...#..######.#.#...#.##....#...#..#..#.##.######.#..#..#.#...##..#..#...###..###.#.#......#....#.#
   -1    90 .#.......#...#..#####.#####.#########..#......#..#.#.##.#.#.##.#.#.........####.##.#.###...#.##..#..
   -1    91 ..###.#.....###..#..#.#..#.#..####..###.....#.####.##...#.#..#.#.#.#.#.#..###..#......#..#####.#.#..
   -1    92 #...#....###..####...#.##.#......###.#.##.###.#..###..##....##....#..#.....#...###.##.#......#..####
   -1    93 #.####....######.####.#.####..#.#..#..#...####..#.#####..#..##.##..#.##...###...###.#.#..###.#.##.#.
   -1    94 .####.###.#.##...###........#...##.#.#.######..#...##.###.##..#.#.######.#####..###....##...##.##...
   -1    95 #.###.#.###..#.#.##..####.##..#...###...####..#..####.#.######.#####..#.##.##.##.##..#.#.##..#.##...
   -1    96 .#.......#....##..#.#.##.#.#.##..###..#..##.....####.#...###....##.##......#....#.......#...##.##...
   -1    97 .####.###..####.#.##..#.#.#..#...#..#.##.###..######.##.##.#.####.##.##.#..#..##..######.####..#..##
   -1    98 #.###.##..#.#......##.#...#..#..#.##.#....##.....#......#..#...#..##....#.#####..####.#.#..#..##.##.
   -1    99 #.#....#.#.#....#.#####.......#..#..#..#.##...#.#########..##..##...####.##..##.##.##..#.##...#.#.##
   -1   100 .####....#..#.####.###.#.##########.###.#...#..##.##.#.####.##.###.##.#..#..#.##.##....###.#...##.#.
   -1   101 ...####.#.......#......#.######.#.##...###....####.##...##...##.#..#.##..##......#...####.#..##...##
   -1   102 .#####...#....#....##.#.####....###..#.##...#.##..##..###.#.##.##.##...#.###..###.###...###...##.#..

diff --git a/2021/20/part1.rs b/2021/20/part1.rs

@@ -0,0 +1,94 @@
   -1     1 use std::env::args;
   -1     2 use std::fs::File;
   -1     3 use std::io::BufRead;
   -1     4 use std::io::BufReader;
   -1     5 
   -1     6 fn get_data() -> (Vec<bool>, Vec<Vec<bool>>) {
   -1     7     let path = args().nth(1).unwrap();
   -1     8     let file = File::open(path).unwrap();
   -1     9 
   -1    10     let mut lines = BufReader::new(file).lines();
   -1    11 
   -1    12     let cypher = lines
   -1    13         .next()
   -1    14         .unwrap()
   -1    15         .unwrap()
   -1    16         .chars()
   -1    17         .map(|c| c == '#')
   -1    18         .collect();
   -1    19 
   -1    20     assert_eq!(lines.next().unwrap().unwrap(), "");
   -1    21 
   -1    22     let img = lines
   -1    23         .map(|l| l
   -1    24             .unwrap()
   -1    25             .chars()
   -1    26             .map(|c| c == '#')
   -1    27             .collect())
   -1    28         .collect();
   -1    29 
   -1    30     return (cypher, img);
   -1    31 }
   -1    32 
   -1    33 fn enhance(img: &Vec<Vec<bool>>, outside: bool, cypher: &Vec<bool>) -> (Vec<Vec<bool>>, bool) {
   -1    34     let rows = img.len();
   -1    35     let cols = img[0].len();
   -1    36 
   -1    37     let mut result = vec![];
   -1    38 
   -1    39     for y in 0..rows+2 {
   -1    40         let mut row = vec![];
   -1    41 
   -1    42         for x in 0..cols+2 {
   -1    43             let mut i = 0;
   -1    44 
   -1    45             for dy in [0, 1, 2].iter() {
   -1    46                 if y + dy < 2 || y + dy >= rows + 2 {
   -1    47                     i <<= 3;
   -1    48                     if outside {
   -1    49                         i |= 0b111;
   -1    50                     }
   -1    51                     continue;
   -1    52                 }
   -1    53 
   -1    54                 for dx in [0, 1, 2].iter() {
   -1    55                     i <<= 1;
   -1    56                     if x + dx < 2 || x + dx >= cols + 2 {
   -1    57                         if outside {
   -1    58                             i |= 1;
   -1    59                         }
   -1    60                     } else if img[y + dy - 2][x + dx - 2] {
   -1    61                         i |= 1;
   -1    62                     }
   -1    63                 }
   -1    64             }
   -1    65 
   -1    66             row.push(cypher[i]);
   -1    67         }
   -1    68         result.push(row);
   -1    69     }
   -1    70 
   -1    71     let outside_i = if outside {0b111111111} else {0};
   -1    72     return (result, cypher[outside_i]);
   -1    73 }
   -1    74 
   -1    75 fn main() {
   -1    76     let (cypher, mut img) = get_data();
   -1    77     let mut outside = false;
   -1    78 
   -1    79     for _ in 0..50 {
   -1    80         let data = enhance(&img, outside, &cypher);
   -1    81         img = data.0;
   -1    82         outside = data.1;
   -1    83     }
   -1    84 
   -1    85     let count: usize = img
   -1    86         .iter()
   -1    87         .map(|row| row
   -1    88             .iter()
   -1    89             .filter(|b| **b)
   -1    90             .count())
   -1    91         .sum();
   -1    92 
   -1    93     println!("{}", count);
   -1    94 }

diff --git a/2021/20/test.txt b/2021/20/test.txt

@@ -0,0 +1,7 @@
   -1     1 ..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
   -1     2 
   -1     3 #..#.
   -1     4 #....
   -1     5 ##..#
   -1     6 ..#..
   -1     7 ..###