Complete day 11
This commit is contained in:
parent
97b86428ad
commit
7dd99cab6d
8
Cargo.lock
generated
8
Cargo.lock
generated
@ -91,3 +91,11 @@ dependencies = [
|
||||
"anyhow",
|
||||
"common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day11"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"common",
|
||||
]
|
||||
|
10
crates/day11/Cargo.toml
Normal file
10
crates/day11/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "day11"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
common = { path = "../common" }
|
10
crates/day11/src/input/input.txt
Normal file
10
crates/day11/src/input/input.txt
Normal file
@ -0,0 +1,10 @@
|
||||
7222221271
|
||||
6463754232
|
||||
3373484684
|
||||
4674461265
|
||||
1187834788
|
||||
1175316351
|
||||
8211411846
|
||||
4657828333
|
||||
5286325337
|
||||
5771324832
|
10
crates/day11/src/input/test.txt
Normal file
10
crates/day11/src/input/test.txt
Normal file
@ -0,0 +1,10 @@
|
||||
5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526
|
10
crates/day11/src/input/test2.txt
Normal file
10
crates/day11/src/input/test2.txt
Normal file
@ -0,0 +1,10 @@
|
||||
1111100000
|
||||
1999100000
|
||||
1919100000
|
||||
1999100000
|
||||
1111100000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
160
crates/day11/src/main.rs
Normal file
160
crates/day11/src/main.rs
Normal file
@ -0,0 +1,160 @@
|
||||
use anyhow::{anyhow, Error, Result};
|
||||
use common::instrument;
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
const INPUT: &str = include_str!("input/input.txt");
|
||||
|
||||
struct Cavern {
|
||||
grid: [[u8; 10]; 10],
|
||||
}
|
||||
|
||||
impl FromStr for Cavern {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self> {
|
||||
let mut grid: [[u8; 10]; 10] = [[0; 10]; 10];
|
||||
for (y, line) in s.trim().lines().enumerate() {
|
||||
for (x, char) in line.trim().chars().enumerate() {
|
||||
let energy = char.to_digit(10).ok_or(anyhow!("Invalid energy level"))? as u8;
|
||||
grid[y][x] = energy;
|
||||
}
|
||||
}
|
||||
Ok(Cavern { grid })
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Cavern {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
for y in 0..10 {
|
||||
for x in 0..10 {
|
||||
if self.grid[y][x] > 9 {
|
||||
write!(f, "#")?;
|
||||
} else {
|
||||
write!(f, "{}", self.grid[y][x])?;
|
||||
}
|
||||
}
|
||||
write!(f, "\n")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Cavern {
|
||||
fn step(&mut self) -> Vec<(usize, usize)> {
|
||||
let mut flashed = vec![];
|
||||
for y in 0..10 {
|
||||
for x in 0..10 {
|
||||
self.grid[y][x] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for y in 0..10 {
|
||||
for x in 0..10 {
|
||||
let mut to_visit = vec![];
|
||||
if self.grid[y][x] > 9 && !flashed.contains(&(x, y)) {
|
||||
flashed.push((x, y));
|
||||
to_visit.append(&mut self.flash(x, y));
|
||||
|
||||
while to_visit.len() > 0 {
|
||||
let (x, y) = to_visit.pop().unwrap();
|
||||
if self.grid[y][x] > 9 && !flashed.contains(&(x, y)) {
|
||||
flashed.push((x, y));
|
||||
to_visit.append(&mut self.flash(x, y));
|
||||
}
|
||||
}
|
||||
|
||||
self.grid[y][x] = 0;
|
||||
for (x, y) in &flashed {
|
||||
self.grid[*y][*x] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
flashed
|
||||
}
|
||||
|
||||
fn flash(&mut self, x: usize, y: usize) -> Vec<(usize, usize)> {
|
||||
let mut to_visit = vec![];
|
||||
if y > 0 {
|
||||
self.grid[y - 1][x] += 1;
|
||||
to_visit.push((x, y - 1));
|
||||
}
|
||||
if y < 9 {
|
||||
self.grid[y + 1][x] += 1;
|
||||
to_visit.push((x, y + 1));
|
||||
}
|
||||
if x > 0 {
|
||||
self.grid[y][x - 1] += 1;
|
||||
to_visit.push((x - 1, y));
|
||||
}
|
||||
if x < 9 {
|
||||
self.grid[y][x + 1] += 1;
|
||||
to_visit.push((x + 1, y));
|
||||
}
|
||||
if y > 0 && x > 0 {
|
||||
self.grid[y - 1][x - 1] += 1;
|
||||
to_visit.push((x - 1, y - 1));
|
||||
}
|
||||
if y > 0 && x < 9 {
|
||||
self.grid[y - 1][x + 1] += 1;
|
||||
to_visit.push((x + 1, y - 1));
|
||||
}
|
||||
if y < 9 && x > 0 {
|
||||
self.grid[y + 1][x - 1] += 1;
|
||||
to_visit.push((x - 1, y + 1));
|
||||
}
|
||||
if y < 9 && x < 9 {
|
||||
self.grid[y + 1][x + 1] += 1;
|
||||
to_visit.push((x + 1, y + 1));
|
||||
}
|
||||
to_visit
|
||||
}
|
||||
}
|
||||
|
||||
fn solve_part1(input: &str) -> Result<usize> {
|
||||
let mut cavern = Cavern::from_str(input)?;
|
||||
|
||||
let mut flashes = 0;
|
||||
for _ in 0..100 {
|
||||
flashes += cavern.step().len();
|
||||
}
|
||||
|
||||
Ok(flashes)
|
||||
}
|
||||
|
||||
fn solve_part2(input: &str) -> Result<i32> {
|
||||
let mut cavern = Cavern::from_str(input)?;
|
||||
|
||||
let mut step = 0;
|
||||
loop {
|
||||
step += 1;
|
||||
let flashed = cavern.step();
|
||||
if flashed.len() == 100 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(step)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
instrument!(solve_part1(INPUT).unwrap(), solve_part2(INPUT).unwrap());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TEST_INPUT: &str = include_str!("input/test.txt");
|
||||
|
||||
#[test]
|
||||
fn solves_part1() {
|
||||
assert_eq!(solve_part1(TEST_INPUT).unwrap(), 1656);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solves_part2() {
|
||||
assert_eq!(solve_part2(TEST_INPUT).unwrap(), 195);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user