From cd4f399ed6535e33bca5353199234c2310997008 Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 11 Dec 2020 12:04:38 +0100 Subject: [PATCH] Add 2020/11 part 1 --- 2020/inputs/day11 | 91 ++++++++++++++++++++++ 2020/setup_day.sh | 10 +-- 2020/src/bin/day11.rs | 177 ++++++++++++++++++++++++++++++++++++++++++ 2020/src/grid.rs | 4 +- 4 files changed, 275 insertions(+), 7 deletions(-) create mode 100644 2020/inputs/day11 create mode 100644 2020/src/bin/day11.rs diff --git a/2020/inputs/day11 b/2020/inputs/day11 new file mode 100644 index 0000000..7e491f4 --- /dev/null +++ b/2020/inputs/day11 @@ -0,0 +1,91 @@ +LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLL.L.LLLLLLL.LLLLLLL.LLLLLL.LL.LLLL.LLLLLLLLLLLLLLL +LLL.LLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL +LLLLLLLLL.LLLL.LLLLLLLLLL.LLLLLLL.LLLL.LLL....LLL.LLL.LLLLLLL.LLL.LLLLL.LLLLLLLL.LLLLLLLLLLL +LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL.LLLL.LLL.LLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLL +LLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLL.LLLLL.LLLLLLLL.LLLLLLLLLLL +LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLL.LLLL.LLLLLLL.L.LLL.LLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLL +LLLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLL..LL.LLL +LLLLLLLLL.LLLLLLLLLLL.LLLL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LLL.LLLLLLLL.LLLLLLLLLLL +.L..L......L.L....L...L..........L.L....L...LL....LL....LL...LLL....LL..L...L...L.........L. +LLLLLLLLL.LLLLLLLLL.LLLLLLLLLLL.LLL.LL.LLLLLLLLLLLLLLLLLLLLL..LLLLLLLLLLLLLLLLLL.LLLLLLLLLLL +LLLLLLLLL.LLLL.LLLLLLLL.LLLLLLL.LLLLLL.LLLLLLLLLLL.LL.LLLLLLL.LLLLLLL.L.LLL.LLLL.LLLLLLLLLLL +LLLLLL.LL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLL.LL.LLLLLLLLLLLLLLLLLL.LLLLLLLLLLL +LLLLLLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL..LLLLLLLLLL +LLLLLLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLL.L.LLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLL +LLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLL.LL.L.LLLLLLLLLLLLLL.L.LLLLLLLLLLL +LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LL.LLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLL +LLLLLLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLL +..L.L.L..L.L.....LL..L........L..L....L...LL.........L.L.L.L..L......L.........L..L....L.... +LLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLL.L.LLLL.LLLLLLLLL.LLLLLLL.LLLLLLL.L.LLLLLLLLLLLLLLL.LLLL +LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLL.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LLLLLLLLLLLL +LLLLLLLLL.LLLLLLL.LLL.LLLLLLLLL.LLLLLL.LLLL.LLLLLLLLL.LLLLLL..LLLLLL.LLLLLLLLLLLLLLLLLLLLLLL +L.LLLLLLLL.LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLL.LLLLL.LLL.LLLLLL..LLLLLLLLLLLLLLLLLL.LLL.LLLLLLL diff --git a/2020/setup_day.sh b/2020/setup_day.sh index 5ec4716..fe8d057 100755 --- a/2020/setup_day.sh +++ b/2020/setup_day.sh @@ -8,7 +8,7 @@ sed -i -e '$a\' inputs/day$today echo '#![feature(test)] extern crate test; -use aoc::common::*; +use aoc2020::common::*; type Parsed = Vec; @@ -44,9 +44,9 @@ mod tests { const TEST_INPUT: &str = ""; - test!(part1 == 0); - test!(part2 == 0); - bench!(part1 == 0); - bench!(part2 == 0); + test!(part1() == 0); + test!(part2() == 0); + bench!(part1() == 0); + bench!(part2() == 0); bench_input!(len == 0); }' > src/bin/day$today.rs diff --git a/2020/src/bin/day11.rs b/2020/src/bin/day11.rs new file mode 100644 index 0000000..c11c6cc --- /dev/null +++ b/2020/src/bin/day11.rs @@ -0,0 +1,177 @@ +#![feature(test)] +extern crate test; +use aoc2020::{common::*, grid::*}; +use std::{ + collections::HashMap, fmt::{self, Display, Formatter} +}; + +#[derive(Debug, PartialEq, Clone, Copy)] +enum Tile { + Floor, + Empty, + Occupied, +} + +impl Display for Tile { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Tile::Floor => ".", + Tile::Empty => "L", + Tile::Occupied => "#", + }) + } +} + +impl From for Tile { + fn from(c: char) -> Self { + match c { + '.' => Tile::Floor, + 'L' => Tile::Empty, + '#' => Tile::Occupied, + _ => unreachable!(), + } + } +} + +impl Default for &Tile { + fn default() -> Self { + &Tile::Floor + } +} + +impl Default for Tile { + fn default() -> Self { + Tile::Floor + } +} + +type Parsed = HashMap; + +fn read_input() -> String { + read_file(11) +} + +fn parse_input(raw: &str) -> Parsed { + raw.lines() + .enumerate() + .flat_map(move |(y, l)| { + l.chars() + .enumerate() + .map(move |(x, c)| (Position2D { x: x as i64, y: y as i64 }, Tile::from(c))) + }) + .collect() +} + +fn occupied_neighbors(pos: &Position2D, grid: &Parsed) -> usize { + pos.moore() + .iter() + .filter(|p| grid.get(&p).unwrap_or_default() == &Tile::Occupied) + .count() +} + +fn make_step(previous: &Parsed) -> (Parsed, bool) { + let mut grid = previous.to_owned(); + let mut changed = false; + for (pos, tile) in grid.iter_mut() { + match tile { + Tile::Floor => (), + Tile::Empty => { + if occupied_neighbors(&pos, &previous) == 0 { + *tile = Tile::Occupied; + changed = true; + } + } + Tile::Occupied => { + if occupied_neighbors(&pos, &previous) >= 4 { + *tile = Tile::Empty; + changed = true; + } + } + } + } + (grid, changed) +} + +fn part1(parsed: &Parsed) -> usize { + let mut p = parsed.to_owned(); + loop { + let (parsed, changed) = make_step(&p); + if !changed { + break; + } + // println!("{}", draw_ascii(&parsed)); + p = parsed; + } + p.iter().filter(|(_, t)| t == &&Tile::Occupied).count() +} + +const DIRECTIONS: [(i64, i64); 8] = [(0, 1), (1, 0), (1, 1), (0, -1), (-1, 0), (-1, -1), (-1, 1), (1, -1)]; + +fn part2(parsed: &Parsed) -> usize { + unimplemented!() +} + +fn main() { + let input = parse_input(&read_input()); + println!("Part 1: {}", part1(&input)); + // println!("Part 2: {}", part2(&input)); +} + +#[cfg(test)] +mod tests { + use super::*; + use aoc2020::*; + use paste::paste; + use test::black_box; + + const TEST_INPUT: &str = "L.LL.LL.LL +LLLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLLL +L.LLLLLL.L +L.LLLLL.LL"; + + const AFTER_1_STEP: &str = "#.##.##.## +#######.## +#.#.#..#.. +####.##.## +#.##.##.## +#.#####.## +..#.#..... +########## +#.######.# +#.#####.##"; + + const AFTER_2_STEPS: &str = "#.LL.L#.## +#LLLLLL.L# +L.L.L..L.. +#LLL.LL.L# +#.LL.LL.LL +#.LLLL#.## +..L.L..... +#LLLLLLLL# +#.LLLLLL.L +#.#LLLL.##"; + + #[test] + fn step_test() { + let input = parse_input(TEST_INPUT); + let (first_step, changed) = make_step(&input); + let after_first = parse_input(AFTER_1_STEP); + assert_eq!(draw_ascii(&first_step, Tile::Floor), draw_ascii(&after_first, Tile::Floor)); + assert_eq!((&first_step, changed), (&after_first, true)); + let (second_step, changed) = make_step(&first_step); + let after_second = parse_input(AFTER_2_STEPS); + assert_eq!((second_step, changed), (after_second, true)); + } + + test!(part1() == 37); + //test!(part2() == 0); + //bench!(part1() == 0); + //bench!(part2() == 0); + //bench_input!(len == 0); +} diff --git a/2020/src/grid.rs b/2020/src/grid.rs index 9b8d0f6..a0d2e68 100644 --- a/2020/src/grid.rs +++ b/2020/src/grid.rs @@ -34,12 +34,12 @@ fn get_boundaries(input: &[&Position2D]) -> Boundaries { Boundaries { x_min, x_max, y_min, y_max } } -pub fn draw_ascii(coordinates: &HashMap, default: T) -> String { +pub fn draw_ascii(coordinates: &HashMap) -> String { let b = get_boundaries(&coordinates.keys().collect::>()); join( (b.y_min..=b.y_max).rev().map(|y| { (b.x_min..=b.x_max) - .map(|x| coordinates.get(&(x, y).into()).unwrap_or(&default).to_string()) + .map(|x| coordinates.get(&(x, y).into()).unwrap_or(&T::default()).to_string()) .collect::() }), "\n",