From e5acbbf150c60a6591d4fb7f16635435db6119b8 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Fri, 7 Jan 2022 22:26:46 +0100 Subject: [PATCH] Added day 20 code for part 1 and 2 Signed-off-by: Louis Vallat --- .gitlab-ci.yml | 5 +++ day20/Cargo.toml | 8 ++++ day20/input | 102 ++++++++++++++++++++++++++++++++++++++++++++++ day20/src/main.rs | 94 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 209 insertions(+) create mode 100644 day20/Cargo.toml create mode 100644 day20/input create mode 100644 day20/src/main.rs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 44f5c9e..404cfc8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -98,3 +98,8 @@ day-19: stage: build script: - cd day19; cargo run --release ./input + +day-20: + stage: build + script: + - cd day20; cargo run --release ./input diff --git a/day20/Cargo.toml b/day20/Cargo.toml new file mode 100644 index 0000000..9bb7445 --- /dev/null +++ b/day20/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day20" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day20/input b/day20/input new file mode 100644 index 0000000..a8933d8 --- /dev/null +++ b/day20/input @@ -0,0 +1,102 @@ +##......##.###.#...#.#..##...##.#......##........#.#...#...####..###.#...###.....#...###..#...#.......##.#.#.#.##..##.##..#..#....##....##.####...##.#..###......##.#..####....##..#.....#......###.#...##...#.....#.######..#.###..####.#.#....###...#..#.#.##.##.###.####.##.#.###......#..##...#..###.....#..##.#.#.#..##.#.....##......#.###.#..#...######.#...#.#.#....####...##.#.#.#..#..##..###.###..###.##...#.#.##....#..##..##...####..#.##..####...#...#####.####...#......##..#...##.###..#..##...##.###..####.###. + +###.###.#.#....##...#.####.##..#.######..#...####.#####..#....#....#.##.#.##.....#....#.####.#..#### +#...#.....#..#.###..######..#..#...##.##.#..#...##..#####.###########.#..#....#####.......###.##.##. +.#...###...#.##..###.###..#.#.#.#####.#.#.####.#.#.###...##.#.##########.##.#.#..####.##.####.##.##. +#.##......#.#.###...#...###.#..#..##.........##..#.#..####.#.####..#.#.#.##.#....###..#..##.##.##.## +....#..##.#.##.##...##.#....###.......#.#.##..##.#.####.#...#.#..##.##..#.....##.#####.####....#.#.. +.#.##..#..#.........#..##..##.##.#......#..#...####..#.#.##......#.##.#....######.#...#.###..#....## +########..#.#..#.#.##..#.#...#.##.#...#....#..##..####.#######.#.##..##..#..###.##.#####.#.###.##..# +.#.#..#...##.#.#.#...#.#..##.#######.##..#..####.##..###.#..###...#.#.#.##.##..###.#..##...#.#.###.. +###.##..#.....#.####....##..#...#.#.#...#####..###.#..##.#.#.#####.##########...######.#.#.####.##.. +..#.#..#.#......#..#..#.#..#.....#..#..#..###.#####.#..##..####.##.#.#.#.###..#.#..#####.#....###.## +.##..#....#...###..#...#....#.##....#.........####..######...#.#####..#....#..#..#######....####.#.. +#...##...#.........##.##..###..##.##.#########....######.#.##...##..#.#.#...##..#.#.....##.####....# +.##..##.#..#####.#.###..####....#..#.#####..####....##..#..####.#..#..#..#....#......#...##..##..##. +...##.#.###.#.#.#.#####..#...###.###..#######..#....##..#..###.###.##.#.#.#.#..#...###.#.#.##.#..#.# +....##.#..#.....#.#.#...#.#..###.#....#.###..#..#.#.#...#....#.###..#...##..#....###...#######....#. +##..##..........#..#..#....###....#...##...#..###.#..#...#.###........#...........#..#....###.###.## +.##.#####.##.###..###..#.#.....#.###..##.##.######...#.###..#.####.......##..#.#....###.###...###### +##.##..#.####..#..#.#.###.#.#.#.##..#.####.###.#...#.#...#...#..#....#.#...##..########.##...#####.# +.##..#.##.#..#..#...#.......##....##..#....#..##..#.##..#.#.##..##.#.######.#..##..#..#......##.#.## +.#.#....#.#.##.##.....#..#####.##.#.###..#.#.....##....#..##.#####......#....##.##.####.#...#.##.#.# +.##.......###..##.###.#.#......#...##.####.....#.##..#.###..##.#.#.###...####.#..#...##..####..#...# +.#.#...####.#...#.#..##.######....###.#.##.#...#...##....#...#.#..#..##..#.#.#..##..##..###.#.###### +......#.....######.#.##..###.#.....#...#..#..#..####..##.##.#..####..##..#.#.##.#.....####.##...##.# +...#..##...#.#..#####.#.#..#...####.#.#.#..###.#..##.#.##......####.##..##.#..##.#...#..##..#.###.#. +..###....#...#...#..###.....#.#####..##......#######.##.#.#.##.#.#.#.####.###..#......#..####.#.##.. +#..#...#####.#..#..##..#######.#...###..#.##.####.#######.###.....#.#....###.####.##..#..#.#.#.##... +#..####..#.###..##...###.##.#####.###.#..##.....#...###.##.#....#.##...##..#..#..#.#.#..#.###.#....# +.##.#.#.###...#...#...##...#..#.#.#......#.#.##.#.#.#.#...#########...#.####.#####.##.#.#.#...##..## +#.#..###....#######..###.##....####..#####..#.##...#.#.#.#.####..#..#..#..#..##..#.###..#.##..###.#. +..#.#...####.##.#.####....#####...#...#...#....###.#...##.##.####...#..#..#.####.##.##...##.#.#..### +#####.#.#.#.#.#..##.#....#..##.##..###.##..##...#.#.#.###.##.#....#...#..#...#...#.###....#.##.##### +..#.#.#..####..##...####..#.#.#.##.##..#..#####.##.######....##.....#.##.....####......#..###..##... +##.##....######.#.####...#.###...#.#..#..#.####.#.#...##...##..#.###...#..#..#.#..##....##....##.... +.#.#.######.#.#....##.#.#.##....###.#...#.###.#..##.#....##.#..##.###.#.#.##.#..#...##.####.#.##.... +.###...###.#.##...###.#.##.#.###.###.##.#.######..#.###...#######...##.##..##..###....#...###....#.. +#.....#.####...#..#..#...##.#.##.#...#####....#..#.###.#.##..######.#.##..###...#.#.####.##.#.#..... +....#.#.####..#..###...###..##.##..#.....##.##..###.#..#.###.#..###..####.##...##...#..#..#.##.#..## +##....#.#.##.#.#.#.#.#.#.##.#.#..#...##.#.#.##....##.#.#..##....#######..###.####...#####.#.#..###.. +.#..####.##.###.#.##.#..######..##.####..##..#.#....#.##.##.#..#.#.##..#.....#....#.###..###...#.#.. +.##.#......###.###.####.#.###.##.###.##.#.#..#####..#.###..#.#..###...#.....##.###.#..##.#..###...#. +##.##.######.##..#..#..#...##.###.##.####.#.#.#####.....###...##.#..###.##..###....#..####.......### +.######.....##..#.##.##.#.#####....###.#.###.#.#...#.####...#...##.##.##....#.#.###.......#..###.#.# +##.##.#.#.##.###.##.##..##.##..##..#...#..###...#.....##.#.#.####.###...#.#..#...#.#..#...#.##..#..# +.###.###.#..##..#.###.#.###....#..#.##.##.#######..#....######..#..#####....###..#...##..#..#####... +.###.####.##.####.##.###.##..#..###.###...###.######.##.#.####.#.#...##....#...##.##.#.##.#.#.#.#..# +.##....##................#..#.#...#....#.#..###..#..#.#####.#####....####..####.####...#.#####.###.# +..###.######.#.##....#.#..##..#...##..#.####.#.###.......#...#....#.#.#..#...#######...####...####.. +##.##..###..#.#..###..####....#..#.#.#..###...####.##..##.......###...##.#.###.#....##.##..#.#...### +..#..#.######.###.....#..####.##.....#....#..##.###.##..#.##.#......###....######.#.#.##.##.#.#...## +####.##...#.##.###.#.#..#.#...######.#...#...####.####..#.#..#..#.#.#.####.#..#.#..##.#..#......##.. +...#..#.##.#......#..#.##.###.#.#.##..#.##.#####.#...##.##..#.#...##.#...###....#...##..#...#.####.. +..###..##...#.##.#..#.#..#.#.##.#..#.#..##.#.#..#.#.....########..##..#.#..#..##.#.###....#.####.#.# +.#.##...###..#.#..###.#...#.###...###.####.#.##.#..#...#..#.##..####.#.....####..#..##.##.#...##.... +.#....#.#.##.#....#..#.#.##.#.###.###..##..##.###.#.##...##.####...#..###..######.##..#.#...###.##.# +#.#.#.##..#..###.....#.###.##.###.#..######....##.##.###......###.####.##....#...#..#.###....#..#### +.#.###.#.#...#..###....##.#.####..#..#..#.###..###.#.#.....#.#####......####.#.....##...#.####...... +..####.#....#......##..###.....####.#..#.....##..#.......##.#.#.....#.##..#.#.#.#....###.##..##..... +######.#.##.#.#.####.#.###.##.##.##..##....#####..#####...####.##...####.##..##.#.##...##.#..##.#### +.##.#...##.....##.#..#.##.#.#....##..##.###..#..####.....#.##...#.#.##...##.#.##....#.####..##.##.## +.###...#.......#.#####..##..##.####..#####.####.#####.....##...###.###....#.......##...#.#....#.#..# +#.#..####..##.##...###..##......##.#..###..#####.####.###.#####.##...#..#.#.###..###.#....#.##.##### +##...#..##...#..#.#..###.#.##.#......#.##...#.....#.#......##...##..#####..#..##.##.##...#..##.##.#. +##.##...###..##.#.#......##.###..#...###..##.#.....##..#.#......#.#####.#...#.#....#...##..#.#...#.. +##..#.#####.##.#.#.#...#..#...########..##.#...###.#.....#.###.###.##.##.#.#..#.###.###...#.###..##. +.#.#####..#..###.###..##..#........#.##....#.#..####...#..##.###..##...#..######....##.#...##.#####. +.....#..#...####..##..#....#..#.#..#.....###..###.##.##.##...##....####..######.#....#....#...###.## +.#####.......###...###.###..##....#.########..##.....###.#.#.#........#.###...###.......##..###..#.# +..##.#.#..###...#....#...#.####.#.#####...###.####.....######..##.##...####....###..##.##.######.##. +#.#.#..#...#..########.##.###.###.##...#..####.#########.#...#..##.##.###.......#.#.###...#.#..#.#.# +..####..#.##.#.###....##.##.#......####.##.##..#.#.###......##.######..##........#...#....#####..### +.###.#..#....##..#..#..#.#.#.#..##..#.#..#...#.#....#......#..#.#####.#.##....###.###..#....#####... +..##..##.#..###.#....###..##.####.##...#..#.##..#....#.#....#..##.#.###.#.....###..####.####.#....#. +#.####.....#..##..#.##..##.###.#.#..###.....#.##.##..#..###..#.##.#...#.##..#....###.###.#####.###.# +.##.#####.##..###.####.##.#.##.###.#..#...##.#..#.##..#..##..##.#.#######.#...#...##.#..####.##.#.#. +..##.##.#.#.###..#.#.###...##..##..#...#..#..####.#.#.####.##.#......#.#...#.#...#.....##..#..#..#.# +.#.###..##..#.......##.#..#.##.#..#....##.#...###.###.###.##..#.#.#.#.#.#########...##.###..#.#..#.. +#.###..#.##.#.#..####.###..#.#.####...#...####.#.#####..####.####.#..###...###.##.##.#...#..#.#....# +...##.#.#.#.......####.####....###.#.............###..#..#.#..#....####..##.#.######....##..##.#.##. +..#####....#..####....##..####...###...#.#...##...#..###...#..#..#..#..######..####.##.###.##.#..### +##..#.###.#..#.###.##.#.###...#.#.###..####.####.##....#..##.#..#.#.##.#.#.#.##......#...##......... +..##.##..#.##.####...#...#...###..##.#.###...###..##.#.##.##..#.##.#..#....##.#....#.##...###...##.. +..#.#.#.#.#..#.##.#.#.#.##..##.......#..#.##.##.####..#####...####.#..##..#####.##.#..#.....#.#####. +#....###..###.######.###.###..###.##...#.#...##.##..#.###..####...#.##..#.#..##.#.###.##.###.......# +....##.#.#...##...#.#......#.#.####.#..#...##...##..#.###.###.#..#..#####.#.#.##..#.#.##.##.#...#..# +##..#.###.#.#.#.##.....#.#.#.#..#..#.#..####.##..######.#######...#.#.......##.#..##.##.##...##.#..# +#.#.#..######.#.##.....#####....#..#....###..#...########..#..##...##..#.#..##.####..##..##..#.#.#.# +.##...#.#.#.#.##.####..###.......####...#.......######...#.##.####..##....###......###.###.##.#.#.#. +###.#.#..####..#.#..#..##.#....######..#.###.##..#.##..##.....##.##....#.###.........###...##.##.... +.#.#..######.#########.#..##.###..#...#.#.#.#.##..##.###..#..#....###..##.#....###.#.##..####...#..# +#..#.###.#.##....####..##.#...####...##.#######...#......##.......#..##..##...###.##.###....#..#.#.. +#.#..#...##.#.####....#....#.##.#.#...#.#....#..##..#.#.####.#.###....###..#..##.##.###....#.##..... +.###..##...##....###...#####.#...#.##.#.#.###.###.##..##.##....#.##.#####..#.#####..#####.##.....#.# +#..###..###.###..#.##.###.##..##...#..##.##.#...###..#.##...##...#....#..#.###..#....#...#.#.....### +#.#..##.####.###..##.####.##..########..##.#...###..#####..#.###..##.......##.#..###.##.#...#...##.. +.#...#.#..#.##.......#.....##.#####.#.#.###..###.#.......###.#..####.#.#.#.#.###..#.#...#.#..#.#.#.# +###.#####....#.##...######.###...#.#..#.#......##.#.#.##.#..##..#.#....#....##.##..####...#.#.###.## +##.#.##.##...###..##.#.#..###.#.##...###..#.#.#.#..##..#.##..####..#.###.#.##..#.#..##...###...####. +###..#..#..#..###..##..###.#.#.##.#.###.#..#...##.#.#.#.#.#######.#...##..##.#...##.......#.#.##.#.. +.#####.....###...##..##...#.#.##..#####.#.#.##.#.#.#.##.#..##..#.#.##.#.##.....###...###...###.###.# +.#.#..##.##.##.#....##.#..#..######..#.##.###.##..#.##.#....#....#.##.##.#.##.######..####.#.#.#.##. diff --git a/day20/src/main.rs b/day20/src/main.rs new file mode 100644 index 0000000..944d74e --- /dev/null +++ b/day20/src/main.rs @@ -0,0 +1,94 @@ +use std::{fs, env, collections::HashSet}; + +#[derive(Debug)] +struct Image { + pub h: usize, + pub w: usize, + pub i: HashSet<(usize, usize)> +} + +fn read_input(path: &str) -> String { + return fs::read_to_string(path).expect("Cannot read file."); +} + +fn parse_input(s: &str) -> (HashSet, Image) { + let mut e = HashSet::new(); + let mut p = HashSet::new(); + let mut r = 0; + + for l in s.lines() { + if e.is_empty() { + for (i, c) in l.chars().enumerate() { + if c == '#' { e.insert(i); } + } + } else if !l.is_empty() { + for (i, c) in l.chars().enumerate() { + if c == '#' { p.insert((r, i)); } + } + r += 1; + } + } + + let c = get_min_max_coord(&p); + return (e, Image { h: c.0.1, w: c.1.1, i: p }); +} + +fn get_min_max_coord(p: &HashSet<(usize, usize)>) -> ((usize, usize), (usize, usize)) { + let (mut lh, mut uh, mut lw, mut uw) = (usize::MAX, usize::MIN, usize::MAX, usize::MIN); + + for i in p.iter() { + lh = std::cmp::min(lh, i.0); + uh = std::cmp::max(uh, i.0); + lw = std::cmp::min(lw, i.1); + uw = std::cmp::max(uw, i.1); + } + + return ((lh, uh), (lw, uw)); +} + +fn shift_image_for_safety(i: &mut Image, s: usize) { + let mut h = HashSet::new(); + for e in i.i.clone() { + h.insert((e.0 + s, e.1 + s)); + } + i.h += s * 2; + i.w += s * 2; + i.i = h; +} + +fn enhance_image(i: &mut Image, a: &HashSet) { + let mut e = HashSet::new(); + for r in 1..i.h { + for c in 1..i.w { + let mut z = 0; + for y in (r - 1) as i32..=(r + 1) as i32 { + for x in (c - 1) as i32..=(c + 1) as i32 { + z <<= 1; + if i.i.contains(&(y as usize, x as usize)) { z += 1; } + } + } + if a.contains(&z) { e.insert((r, c)); } + } + } + i.i = e; + i.h -= 1; + i.w -= 1; +} + +fn main() { + let args: Vec = env::args().collect(); + for arg in args.iter().skip(1) { + let input = read_input(&arg); + let mut vec_in = parse_input(&input); + shift_image_for_safety(&mut vec_in.1, 100); + let mut p1 = 0; + for i in 0..50 { + if i == 2 { p1 = vec_in.1.i.len(); } + enhance_image(&mut vec_in.1, &vec_in.0); + } + println!("[{}]", &arg); + println!("\t[Part 1] => Answer is '{}'.", p1); + println!("\t[Part 2] => Answer is '{}'.", vec_in.1.i.len()); + } +} +