Day 18 part 2

This commit is contained in:
2024-06-28 10:50:35 +02:00
parent 340fe7025b
commit 2f097ef852

View File

@@ -37,6 +37,35 @@ fn part1(input: &Field) -> usize {
current.into_iter().flatten().filter(|x| *x).count()
}
#[aoc(day18, part2)]
fn part2(input: &Field) -> usize {
let mut current = input.clone();
let mut next = input.clone();
let l = current.len() - 2;
for _ in 0..100 {
for x in 1..current.len() - 1 {
for y in 1..current.len() - 1 {
if x == 1 && y == 1 || x == 1 && y == l || x == l && y == 1 || x == l && y == l {
next[x][y] = true;
continue;
}
let now = current[x][y];
let neighbors_on: usize = count_neighbors(&current, 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] {