diff --git a/src/day18.rs b/src/day18.rs new file mode 100644 index 0000000..1daa062 --- /dev/null +++ b/src/day18.rs @@ -0,0 +1,68 @@ +use aoc_runner_derive::{aoc, aoc_generator}; + +type Field = Vec>; + +#[aoc_generator(day18)] +fn parse(input: &str) -> Field { + let w = input.lines().next().unwrap().len(); + let h = input.lines().count(); + let mut result = vec![vec![false; h + 2]; w + 2]; + input.lines().enumerate().for_each(|(y, line)| { + line.chars() + .enumerate() + .for_each(|(x, c)| result[x + 1][y + 1] = c == '#'); + }); + result +} + +#[aoc(day18, part1)] +fn part1(input: &Field) -> usize { + let mut current = input.clone(); + let mut next = input.clone(); + for _ in 0..100 { + for x in 1..current.len() - 1 { + for y in 1..current.len() - 1 { + let now = current[x][y]; + let neighbors_on: usize = count_neighbors(¤t, x, y); + let new = if now { + neighbors_on == 2 || neighbors_on == 3 + } else { + neighbors_on == 3 + }; + next[x][y] = new; + } + } + (current, next) = (next, current); + } + current.into_iter().flatten().filter(|x| *x).count() +} + +fn count_neighbors(field: &Field, x: usize, y: usize) -> usize { + let mut result = 0; + if field[x - 1][y - 1] { + result += 1; + } + if field[x][y - 1] { + result += 1; + } + if field[x + 1][y - 1] { + result += 1; + } + if field[x - 1][y] { + result += 1; + } + if field[x + 1][y] { + result += 1; + } + if field[x - 1][y + 1] { + result += 1; + } + if field[x][y + 1] { + result += 1; + } + if field[x + 1][y + 1] { + result += 1; + } + + result +} diff --git a/src/lib.rs b/src/lib.rs index a6a81d4..1380dfb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,5 +10,6 @@ mod day14; mod day15; mod day16; mod day17; +mod day18; aoc_lib! { year = 2015 }