1
0
Fork 0
This commit is contained in:
Lars Martens 2021-12-11 11:59:12 +01:00
parent 51508d91c0
commit cf754c5205
3 changed files with 133 additions and 0 deletions

113
src/bin/11.rs Normal file
View file

@ -0,0 +1,113 @@
fn main() {
let (flashes, sync) = solve();
println!("First solution: {}", flashes);
println!("Second solution: {}", sync);
}
fn solve() -> (usize, usize) {
let mut field = input();
let mut flashes = 0;
let mut i = 0;
loop {
i += 1;
// Add one to each octopus
for row in 0..field.len() {
for col in 0..field[0].len() {
field[row][col].energy += 1;
}
}
// Do the flashing
let mut changed = true;
while changed {
changed = false;
for row in 0..field.len() {
for col in 0..field[0].len() {
let current = &mut field[row][col];
if current.energy > 9 && !current.did_flash {
current.did_flash = true;
changed = true;
if i <= 100 {
flashes += 1;
}
for (r, c) in neighbors(row, col) {
field[r][c].energy += 1;
}
}
}
}
}
// Have all octopi flashed?
if field.iter().flat_map(|r| r.iter()).all(|o| o.did_flash) {
return (flashes, i);
}
// Reset octopi > 9
for row in 0..field.len() {
for col in 0..field[0].len() {
let current = &mut field[row][col];
if current.energy > 9 {
current.energy = 0;
current.did_flash = false;
}
}
}
}
}
fn neighbors(row: usize, col: usize) -> Vec<(usize, usize)> {
let mut neighbors = Vec::new();
let mut rs = vec![row];
let mut cs = vec![col];
if row > 0 {
rs.push(row - 1);
}
if row < 9 {
rs.push(row + 1);
}
if col > 0 {
cs.push(col - 1);
}
if col < 9 {
cs.push(col + 1);
}
for r in rs {
for &c in &cs {
if r == row && c == col {
continue;
}
neighbors.push((r, c));
}
}
neighbors
}
type Field = Vec<Vec<Octopus>>;
struct Octopus {
energy: u32,
did_flash: bool,
}
fn input() -> Field {
include_str!("../../input/11.txt")
.lines()
.map(|l| {
l.chars()
.map(|c| c.to_digit(10).unwrap())
.map(|n| Octopus {
energy: n,
did_flash: false,
})
.collect()
})
.collect()
}