do day 11

This commit is contained in:
Adam Jeniski 2022-09-19 19:11:06 -04:00
parent 5424f4fcf3
commit 8ddc70a40c

View File

@ -3,72 +3,49 @@
* Author: Adam Jeniski; @Ajetski * Author: Adam Jeniski; @Ajetski
*/ */
type Data = Vec<Vec<u8>>; fn part_1_solution(mut input: Vec<Vec<i32>>) -> u64 {
fn flash(data: &mut Vec<Vec<i32>>, row: usize, col: usize, count: &mut u64) {
*count += 1;
//println!("flash {row} {col}");
data[row][col] = 0;
fn parse_input(input: &str) -> Data { for (i, j) in (-1i32..=1).flat_map(|i| (-1i32..=1).map(move |j| (i, j))) {
input // compute coords as i32 so we can do bounds checking
.split_ascii_whitespace() let x = i + row as i32;
.map(|s| s.chars().map(|c| c.to_string().parse().unwrap()).collect()) let y = j + col as i32;
.collect() //print!("{row}, {col}; {x}, {y}. ");
}
fn flash(data: &mut Data, row: usize, col: usize) { // skip out of bounds
data[row][col] += 1; if x < 0 || y < 0 || x >= data.len() as i32 || y >= data[x as usize].len() as i32 {
if row > 0 && col > 0 { //println!("exiting");
data[row - 1][col - 1] += 1; continue;
} }
if row > 0 {
data[row - 1][col] += 1;
}
if row > 0 && col < data[row].len() - 1 {
data[row - 1][col + 1] += 1;
}
if col < data[row].len() - 1 {
data[row][col + 1] += 1;
}
if row < data.len() - 1 && col < data[row].len() - 1 {
data[row + 1][col + 1] += 1;
}
if row < data.len() - 1 {
data[row + 1][col] += 1;
}
if row < data.len() - 1 && col > 0 {
data[row + 1][col - 1] += 1;
}
if col > 0 {
data[row][col - 1] += 1;
}
}
fn part_1_solution(input: &str) -> u64 { // convert coords to usize for easy indexing
let mut data = parse_input(input); let x = x as usize;
let y = y as usize;
if data[x][y] != 0 {
data[x][y] += 1;
//println!("updated data to {}", data[x][y]);
}
if data[x][y] > 9 {
//println!("flushing");
flash(data, x, y, count);
}
}
}
let mut count = 0; let mut count = 0;
for _ in 0..100 { for _ in 0..100 {
// increase energy for row in &mut input {
for row in 0..data.len() { for cell in row {
for col in 0..data[row].len() { *cell += 1;
data[row][col] += 1;
} }
} }
for i in 0..input.len() {
// while any flashable cells exits; flash them for j in 0..input[i].len() {
while data.iter().any(|row| row.iter().any(|cell| *cell == 10)) { if input[i][j] > 9 {
for row in 0..data.len() { flash(&mut input, i, j, &mut count);
for col in 0..data[row].len() {
if data[row][col] == 10 {
flash(&mut data, row, col);
}
}
}
}
// count all the flashed cells and reset them to zero
for row in 0..data.len() {
for col in 0..data[row].len() {
if data[row][col] > 9 {
data[row][col] = 0;
count += 1;
} }
} }
} }
@ -76,37 +53,89 @@ fn part_1_solution(input: &str) -> u64 {
count count
} }
fn part_2_solution(mut input: Vec<Vec<i32>>) -> u64 {
fn flash(data: &mut Vec<Vec<i32>>, row: usize, col: usize) {
//println!("flash {row} {col}");
data[row][col] = 0;
for (i, j) in (-1i32..=1).flat_map(|i| (-1i32..=1).map(move |j| (i, j))) {
// compute coords as i32 so we can do bounds checking
let x = i + row as i32;
let y = j + col as i32;
//print!("{row}, {col}; {x}, {y}. ");
// skip out of bounds
if x < 0 || y < 0 || x >= data.len() as i32 || y >= data[x as usize].len() as i32 {
//println!("exiting");
continue;
}
// convert coords to usize for easy indexing
let x = x as usize;
let y = y as usize;
if data[x][y] != 0 {
data[x][y] += 1;
//println!("updated data to {}", data[x][y]);
}
if data[x][y] > 9 {
//println!("flushing");
flash(data, x, y);
}
}
}
for count in 1..100000 {
for row in &mut input {
for cell in row {
*cell += 1;
}
}
for i in 0..input.len() {
for j in 0..input[i].len() {
if input[i][j] > 9 {
flash(&mut input, i, j);
}
}
}
if input.iter().all(|row| row.iter().all(|cell| cell == &0)) {
return count;
}
}
todo!()
}
#[cfg(test)] #[cfg(test)]
mod part1 { mod part1 {
use super::*; use super::*;
fn parse_input(input: &str) -> Vec<Vec<i32>> {
input
.split_ascii_whitespace()
.map(|s| s.chars().map(|c| c.to_string().parse().unwrap()).collect())
.collect()
}
#[test] #[test]
fn run_sample() { fn day_11_sample_part_one() {
let input = include_str!("../inputs/11_test.txt"); let input = include_str!("../inputs/11_test.txt");
let sol = part_1_solution(input); let sol = part_1_solution(parse_input(input));
assert_eq!(sol, 1656); assert_eq!(sol, 1656);
} }
// #[test] #[test]
// fn run() { fn day_11_test_part_one() {
// let input = include_str!("../inputs/11.txt"); let input = include_str!("../inputs/11.txt");
// assert_eq!(part_1_solution(input), 339477); assert_eq!(part_1_solution(parse_input(input)), 1725);
// } }
#[test]
fn day_11_sample_part_two() {
let input = include_str!("../inputs/11_test.txt");
assert_eq!(part_2_solution(parse_input(input)), 195);
}
#[test]
fn day_11_test_part_two() {
let input = include_str!("../inputs/11.txt");
assert_eq!(part_2_solution(parse_input(input)), 308);
}
} }
// #[cfg(test)]
// mod part2 {
// use super::*;
// #[test]
// fn run_sample() {
// let input = include_str!("../inputs/11_test.txt");
// assert_eq!(part_2_solution(input), 288957);
// }
// #[test]
// fn run() {
// let input = include_str!("../inputs/11.txt");
// assert_eq!(part_2_solution(input), 3049320156);
// }
// }