Day 11
This commit is contained in:
parent
51508d91c0
commit
cf754c5205
3 changed files with 133 additions and 0 deletions
10
input/11-test.txt
Normal file
10
input/11-test.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
5483143223
|
||||||
|
2745854711
|
||||||
|
5264556173
|
||||||
|
6141336146
|
||||||
|
6357385478
|
||||||
|
4167524645
|
||||||
|
2176841721
|
||||||
|
6882881134
|
||||||
|
4846848554
|
||||||
|
5283751526
|
10
input/11.txt
Normal file
10
input/11.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
7612648217
|
||||||
|
7617237672
|
||||||
|
2853871836
|
||||||
|
7214367135
|
||||||
|
1533365614
|
||||||
|
6258172862
|
||||||
|
5377675583
|
||||||
|
5613268278
|
||||||
|
8381134465
|
||||||
|
3445428733
|
113
src/bin/11.rs
Normal file
113
src/bin/11.rs
Normal 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()
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue