From 861e31efee248753108ae1ce281a80fb2c5d8881 Mon Sep 17 00:00:00 2001 From: Johannes Date: Sat, 27 May 2023 01:00:21 +0200 Subject: [PATCH] Day 18 part 1 --- input/day18.txt | 50 +++++++++++++++++++++++ src/main.rs | 2 +- src/tasks/day18.rs | 99 ++++++++++++++++++++++++++++++++++++++++++++++ src/tasks/mod.rs | 1 + 4 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 input/day18.txt create mode 100644 src/tasks/day18.rs diff --git a/input/day18.txt b/input/day18.txt new file mode 100644 index 0000000..e637a9e --- /dev/null +++ b/input/day18.txt @@ -0,0 +1,50 @@ +|#..|#...|..|.#..|###|.....#.|.......||#..|....||. +#||..##.#........||#...##.|..###.|.||...|.#.|.|.#. +##.#.###....##....|..||#.||##.|.###|........||.##. +#.|.||#...|..####......|.#|#..#.#|##...||..#..|... +.....#..#.|.####..#..#...|||...||.|...#......#..|. +.|..#..#.......|...#.|...|.##....|#|..#|###..#..#. +.##..#..##..|.#|||.##..|..#.##..|....#..#|.##|.|#. +|#..|#...|...|.|.......#.#......|...|.#.|||.|||#.# +|....#...|..#..#.....#.|..#.#..|#|.#|...|..|#..|#| +.#...##..|#.##......##...#|||..|.....#.|..|...|..# +#.....|..|...||.|.|.....|....#|..|#...#|...#.....# +...|...###.||......|..#|..|...|.##|........#|#|..| +|.|.#.#......||#|||..|#....|#.|...#|..|.|.#|#.|.|. +###.#.|....|.......##.#|###.|#.#..#.|.#...#...###. +|###...|.....#.|.##..#...|#.#.|.##.#........#..|.. +|.||.|...##...##|......#..|.##.##|..#..|#..#.##... +#....|#.....|...|...|............#..#|.....|.#.|.# +...#..|..|||#.|.......#|...#...##|.......####.|... +.#..|..#..|....||#.##.....|||...#..|.#..|.#..|..## +....#...##.........#....|..#.......#...|.....##.#. +|...|...|....#|####||###..|.|..|.||.#......#.|#... +.#.#|.|.|....#.....||...||..|...##.#..|.|.#......| +..|.......|||.|..#.#......|.|..##.||....|###....#. +##....#.......#.|#.##.........|.|....#...|.#|.|.#| +|#.##...|||||#.##.#...#.|#...|.||.|...|..#...#..|. +...#||..#.......||..|.###.#.|#......||..|.#.....#. +#..|.||#.#...|..........#.....#...#...###||.#..... +#..#.|###|#|..|##...##.#......#|.#.#|..#.......|#. +.|.....|.|..#.###|.#|.##.....|.|..|..|..#..|...##. +.|........#...#..|.|..||#....|....#..|.|........|# +....#.|...#|||...#......#...##......|#....#.||.#.. +.|.....|....#......#.|#.|.|.|..#.#.|..##.#||.....# +.....#...|.#|..#..#|#.#|.|..|.#........#|..#|....# +|.||..##...|#.#||..|..#.|..|..#..|..#.|.#|.#...|#. +...|#.###...#..|#..##..||....#.||..#.|.|#.#..|..|| +......|#|.#.#|.|....#..##|##|#...|.#.|.#....##|#.. +#..||.....#....#....#.#.....|.....#....|....|...#. +.#....#.##..........|.||.#.....#|#|||.#..#|......| +..||..|....#..........#.|...#|.|#.|#..|#||.#...|#| +..#..#.#|......#|.....||.#..##.|.#..#.||...|.|||.. +.#....|....#.|#...#..||..||.##..#.||....|.#|....|. +..#|.|.....#....#..|..||..#..##.|.||..||||#.#..|.| +.|#.|.||........#|.#|#....||..#||#...|..........## +..#|.|..|||..###..|||.#..#.#||||.#.|##...|#......| +..|...#|...|.#.#|.#...#.|..||##.#..#.|...#.#.#|#.. +#..#..|##.#|......#...|#|##..#.|...#.#.....#..##.. +..#.|..###|.|#.|........|.....|.....#..|.|.#...|.# +..#|.|#.#.|#..|....|#...|.....|........|.|##.|#||# +#.....##.#..#..#...|#||.#.#.#..|....|||.|.|......# +...#|#....|.#.#..##.|.....#....|.|||..##.|.#.|.##. diff --git a/src/main.rs b/src/main.rs index 0d0825e..3e50f68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,3 @@ fn main() { - aoc_2018::tasks::day17::task1(); + aoc_2018::tasks::day18::task1(); } diff --git a/src/tasks/day18.rs b/src/tasks/day18.rs new file mode 100644 index 0000000..678a1e8 --- /dev/null +++ b/src/tasks/day18.rs @@ -0,0 +1,99 @@ +use std::collections::HashMap; + +use itertools::Itertools; + +use crate::utils; + +pub fn task1() { + let input = utils::read_file("input/day18.txt"); + let initial: HashMap = input + .lines() + .enumerate() + .flat_map(|(y, line)| { + line.chars() + .enumerate() + .map(|(x, c)| (Pos(x as i32, y as i32), c)) + .collect_vec() + }) + .collect(); + let fin = (0..10).fold(initial, |map, _| { + printmap(&map); + map.keys() + .map(|p| { + let old = map[p]; + let new = match old { + '.' => { + if neighbors(&map, *p) + .into_iter() + .filter(|c| *c == '|') + .count() + >= 3 + { + '|' + } else { + '.' + } + } + '|' => { + if neighbors(&map, *p) + .into_iter() + .filter(|c| *c == '#') + .count() + >= 3 + { + '#' + } else { + '|' + } + } + '#' => { + if neighbors(&map, *p).contains(&'#') && neighbors(&map, *p).contains(&'|') + { + '#' + } else { + '.' + } + } + _ => panic!("unknown type"), + }; + (*p, new) + }) + .collect() + }); + let trees = fin.values().filter(|c| **c == '|').count(); + let lumberyards = fin.values().filter(|c| **c == '#').count(); + println!("Part 1 - {trees} x {lumberyards} = {}", trees * lumberyards); +} + +fn printmap(map: &HashMap) { + println!(); + let (x_min, x_max) = map.keys().map(|p| p.0).minmax().into_option().unwrap(); + let (_y_min, y_max) = map.keys().map(|p| p.1).minmax().into_option().unwrap(); + + for y in 0..=y_max { + for x in x_min..=x_max { + let s = map[&Pos(x, y)]; + print!("{s}"); + } + print!("\n"); + } +} + +#[derive(Hash, Eq, PartialEq, Clone, Copy)] +struct Pos(i32, i32); + +fn neighbors(map: &HashMap, p: Pos) -> Vec { + [ + Pos(p.0 - 1, p.1 - 1), + Pos(p.0 - 1, p.1), + Pos(p.0 - 1, p.1 + 1), + Pos(p.0, p.1 - 1), + Pos(p.0, p.1 + 1), + Pos(p.0 + 1, p.1 - 1), + Pos(p.0 + 1, p.1), + Pos(p.0 + 1, p.1 + 1), + ] + .into_iter() + .flat_map(|pos| map.get(&pos).map(|c| *c)) + .collect_vec() +} diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index fc77a7e..bbb97cf 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -14,6 +14,7 @@ pub mod day13; pub mod day14; pub mod day15; pub mod day17; +pub mod day18; pub mod day20; pub mod day22; pub mod day23;