feat: solve day 11, part 1 & 2
This commit is contained in:
parent
3a18776a44
commit
b0914ebb2b
9
Cargo.lock
generated
9
Cargo.lock
generated
@ -3,3 +3,12 @@
|
||||
[[package]]
|
||||
name = "advent-of-code-2021"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
|
||||
|
@ -7,3 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
arrayvec = "0.7.2"
|
||||
|
10
input/11.txt
Normal file
10
input/11.txt
Normal file
@ -0,0 +1,10 @@
|
||||
3265255276
|
||||
1537412665
|
||||
7335746422
|
||||
6426325658
|
||||
3854434364
|
||||
8717377486
|
||||
4522286326
|
||||
6337772845
|
||||
8824387665
|
||||
6351586484
|
171
src/day11.rs
Normal file
171
src/day11.rs
Normal file
@ -0,0 +1,171 @@
|
||||
use arrayvec::ArrayVec;
|
||||
|
||||
pub fn parse_input(input: &str) -> [[u32; 10]; 10] {
|
||||
input.lines()
|
||||
.map(|s| s.chars()
|
||||
.map(|s| s.to_digit(10).unwrap())
|
||||
.collect::<ArrayVec<u32, 10>>()
|
||||
.into_inner()
|
||||
.unwrap())
|
||||
.collect::<ArrayVec<[u32; 10], 10>>()
|
||||
.into_inner()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn display_grid(grid: &[[u32; 10]; 10]) {
|
||||
for i in 0..grid.len() {
|
||||
for j in 0..grid[i].len() {
|
||||
print!("{:X}", grid[i][j]);
|
||||
}
|
||||
print!("\n");
|
||||
}
|
||||
print!("\n");
|
||||
}
|
||||
|
||||
fn bump_energy(grid: &mut [[u32; 10]; 10]) {
|
||||
for i in 0..grid.len() {
|
||||
for j in 0..grid[i].len() {
|
||||
grid[i][j] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn bump_energy_around(grid: &mut [[u32; 10]; 10], i: usize, j: usize) {
|
||||
if j > 0 {
|
||||
grid[i+0][j-1] += 1;
|
||||
}
|
||||
if j < 9 {
|
||||
grid[i+0][j+1] += 1;
|
||||
}
|
||||
if i > 0 {
|
||||
grid[i-1][j+0] += 1;
|
||||
}
|
||||
if i < 9 {
|
||||
grid[i+1][j+0] += 1;
|
||||
}
|
||||
|
||||
if i > 0 && j > 0 {
|
||||
grid[i-1][j-1] += 1;
|
||||
}
|
||||
if i < 9 && j > 0 {
|
||||
grid[i+1][j-1] += 1;
|
||||
}
|
||||
if i > 0 && j < 9 {
|
||||
grid[i-1][j+1] += 1;
|
||||
}
|
||||
if i < 9 && j < 9 {
|
||||
grid[i+1][j+1] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn perform_flashes(grid: &mut [[u32; 10]; 10]) -> u32 {
|
||||
let mut flashes = 0;
|
||||
let mut has_flashed: [[bool; 10]; 10] = [[false; 10]; 10];
|
||||
let mut anyone_flashed = true;
|
||||
|
||||
while anyone_flashed {
|
||||
anyone_flashed = false;
|
||||
|
||||
for i in 0..grid.len() {
|
||||
for j in 0..grid[i].len() {
|
||||
if grid[i][j] > 9 && !has_flashed[i][j] {
|
||||
flashes += 1;
|
||||
has_flashed[i][j] = true;
|
||||
anyone_flashed = true;
|
||||
bump_energy_around(grid, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return flashes;
|
||||
}
|
||||
|
||||
fn reset_energy(grid: &mut [[u32; 10]; 10]) {
|
||||
for i in 0..grid.len() {
|
||||
for j in 0..grid[i].len() {
|
||||
if grid[i][j] > 9 {
|
||||
grid[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn do_step(grid: &mut [[u32; 10]; 10]) -> u32 {
|
||||
bump_energy(grid);
|
||||
let flashes = perform_flashes(grid);
|
||||
reset_energy(grid);
|
||||
return flashes;
|
||||
}
|
||||
|
||||
fn has_all_zeros(grid: &[[u32; 10]; 10]) -> bool {
|
||||
for i in 0..grid.len() {
|
||||
for j in 0..grid[i].len() {
|
||||
if grid[i][j] != 0 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn part1(grid: &[[u32; 10]; 10]) -> u32 {
|
||||
let mut flashes = 0;
|
||||
let mut active_grid = grid.clone();
|
||||
for _ in 0..100 {
|
||||
flashes += do_step(&mut active_grid);
|
||||
}
|
||||
return flashes;
|
||||
}
|
||||
|
||||
pub fn part2(grid: &[[u32; 10]; 10]) -> u32 {
|
||||
let mut active_grid = grid.clone();
|
||||
let mut step = 0;
|
||||
while !has_all_zeros(&active_grid) {
|
||||
do_step(&mut active_grid);
|
||||
step += 1;
|
||||
}
|
||||
return step;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn part1_example() {
|
||||
let input = [
|
||||
[5, 4, 8, 3, 1, 4, 3, 2, 2, 3],
|
||||
[2, 7, 4, 5, 8, 5, 4, 7, 1, 1],
|
||||
[5, 2, 6, 4, 5, 5, 6, 1, 7, 3],
|
||||
[6, 1, 4, 1, 3, 3, 6, 1, 4, 6],
|
||||
[6, 3, 5, 7, 3, 8, 5, 4, 7, 8],
|
||||
[4, 1, 6, 7, 5, 2, 4, 6, 4, 5],
|
||||
[2, 1, 7, 6, 8, 4, 1, 7, 2, 1],
|
||||
[6, 8, 8, 2, 8, 8, 1, 1, 3, 4],
|
||||
[4, 8, 4, 6, 8, 4, 8, 5, 5, 4],
|
||||
[5, 2, 8, 3, 7, 5, 1, 5, 2, 6]
|
||||
];
|
||||
let result = part1(&input);
|
||||
assert_eq!(result, 1656);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_example() {
|
||||
let input = [
|
||||
[5, 4, 8, 3, 1, 4, 3, 2, 2, 3],
|
||||
[2, 7, 4, 5, 8, 5, 4, 7, 1, 1],
|
||||
[5, 2, 6, 4, 5, 5, 6, 1, 7, 3],
|
||||
[6, 1, 4, 1, 3, 3, 6, 1, 4, 6],
|
||||
[6, 3, 5, 7, 3, 8, 5, 4, 7, 8],
|
||||
[4, 1, 6, 7, 5, 2, 4, 6, 4, 5],
|
||||
[2, 1, 7, 6, 8, 4, 1, 7, 2, 1],
|
||||
[6, 8, 8, 2, 8, 8, 1, 1, 3, 4],
|
||||
[4, 8, 4, 6, 8, 4, 8, 5, 5, 4],
|
||||
[5, 2, 8, 3, 7, 5, 1, 5, 2, 6]
|
||||
];
|
||||
let result = part2(&input);
|
||||
assert_eq!(result, 195);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ mod day7;
|
||||
mod day8;
|
||||
mod day9;
|
||||
mod day10;
|
||||
mod day11;
|
||||
|
||||
use std::{env, process};
|
||||
use std::fs::File;
|
||||
@ -44,6 +45,8 @@ fn run(day: i32, part: i32, input_filename: &str) {
|
||||
"9.2" => println!("{}", day9::part2(day9::parse_input(&contents))),
|
||||
"10.1" => println!("{}", day10::part1(&day10::parse_input(&contents))),
|
||||
"10.2" => println!("{}", day10::part2(&day10::parse_input(&contents))),
|
||||
"11.1" => println!("{}", day11::part1(&day11::parse_input(&contents))),
|
||||
"11.2" => println!("{}", day11::part2(&day11::parse_input(&contents))),
|
||||
_ => println!("Day {} part {} not found", day, part)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user