day4
This commit is contained in:
120
src/day4.rs
Normal file
120
src/day4.rs
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
#[aoc(day4, part1)]
|
||||||
|
pub fn part1(input: &str) -> u32 {
|
||||||
|
let array = make_array(&input);
|
||||||
|
get_accessible(&array)
|
||||||
|
.iter()
|
||||||
|
.flatten()
|
||||||
|
.fold(0, |acc, x| acc + (*x as u32))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day4, part2)]
|
||||||
|
pub fn part2(input: &str) -> u32 {
|
||||||
|
let mut array = make_array(&input);
|
||||||
|
remove_accessible(&mut array)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_array(input: &str) -> Vec<Vec<u8>> {
|
||||||
|
let n = input.split("\n").next().unwrap().len();
|
||||||
|
let m = input.split("\n").count();
|
||||||
|
|
||||||
|
// Top row padded with 0s for convenience and performance
|
||||||
|
let mut array = vec![vec![0_u8; m + 2]; n + 2];
|
||||||
|
|
||||||
|
for (i, line) in input.split("\n").enumerate() {
|
||||||
|
for (j, ch) in line.chars().enumerate() {
|
||||||
|
if ch == '@' {
|
||||||
|
array[i + 1][j + 1] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// println!("{:?}", array);
|
||||||
|
array
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_accessible(array: &Vec<Vec<u8>>) -> Vec<Vec<u8>> {
|
||||||
|
let n = array.len();
|
||||||
|
let m = array[0].len();
|
||||||
|
|
||||||
|
let mut accessible = vec![vec![0_u8; m - 2]; n - 2];
|
||||||
|
|
||||||
|
for i in 1..n - 1 {
|
||||||
|
for j in 1..m - 1 {
|
||||||
|
accessible[i - 1][j - 1] = array[i][j]
|
||||||
|
* ((array[i - 1][j - 1]
|
||||||
|
+ array[i - 1][j]
|
||||||
|
+ array[i - 1][j + 1]
|
||||||
|
+ array[i][j - 1]
|
||||||
|
+ array[i][j + 1]
|
||||||
|
+ array[i + 1][j - 1]
|
||||||
|
+ array[i + 1][j]
|
||||||
|
+ array[i + 1][j + 1])
|
||||||
|
< 4) as u8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// println!("{:?}", accessible);
|
||||||
|
accessible
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_accessible(rolls: &mut Vec<Vec<u8>>) -> u32 {
|
||||||
|
let n = rolls.len();
|
||||||
|
let m = rolls[0].len();
|
||||||
|
|
||||||
|
let mut removed: u32 = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut removed_new: u32 = 0;
|
||||||
|
let accessible = get_accessible(rolls);
|
||||||
|
|
||||||
|
for i in 0..n - 2 {
|
||||||
|
for j in 0..m - 2 {
|
||||||
|
rolls[i + 1][j + 1] = rolls[i + 1][j + 1] - accessible[i][j];
|
||||||
|
removed_new = removed_new + accessible[i][j] as u32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if removed_new == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
removed = removed + removed_new;
|
||||||
|
}
|
||||||
|
removed
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part2_1() {
|
||||||
|
let input = String::from(
|
||||||
|
"..@@.@@@@.
|
||||||
|
@@@.@.@.@@
|
||||||
|
@@@@@.@.@@
|
||||||
|
@.@@@@..@.
|
||||||
|
@@.@@@@.@@
|
||||||
|
.@@@@@@@.@
|
||||||
|
.@.@.@.@@@
|
||||||
|
@.@@@.@@@@
|
||||||
|
.@@@@@@@@.
|
||||||
|
@.@.@@@.@.",
|
||||||
|
);
|
||||||
|
assert_eq!(part2(&input), 43)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part1_1() {
|
||||||
|
let input = String::from(
|
||||||
|
"..@@.@@@@.
|
||||||
|
@@@.@.@.@@
|
||||||
|
@@@@@.@.@@
|
||||||
|
@.@@@@..@.
|
||||||
|
@@.@@@@.@@
|
||||||
|
.@@@@@@@.@
|
||||||
|
.@.@.@.@@@
|
||||||
|
@.@@@.@@@@
|
||||||
|
.@@@@@@@@.
|
||||||
|
@.@.@@@.@.",
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(part1(&input), 13)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ extern crate aoc_runner_derive;
|
|||||||
|
|
||||||
// pub mod day1;
|
// pub mod day1;
|
||||||
// pub mod day2;
|
// pub mod day2;
|
||||||
pub mod day3;
|
// pub mod day3;
|
||||||
|
pub mod day4;
|
||||||
|
|
||||||
aoc_lib! { year = 2025 }
|
aoc_lib! { year = 2025 }
|
||||||
|
|||||||
Reference in New Issue
Block a user