Rust
fn count_sides(grid: &[Vec<char>], x: usize, y: usize) -> usize {
let mut count = 0;
for i in y.saturating_sub(1)..=y + 1 {
for j in x.saturating_sub(1)..=x + 1 {
if i == y && j == x {
continue;
}
if let Some(row) = grid.get(i) {
if let Some(col) = row.get(j) {
if *col == '@' {
count += 1;
}
}
}
}
}
count
}
#[test]
fn test_y2025_day4_part1() {
let input = std::fs::read_to_string("input/2025/day_4.txt").unwrap();
let grid = input
.lines()
.map(|l| l.chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut total = 0;
let width = grid[0].len();
let height = grid.len();
for y in 0..height {
for x in 0..width {
if grid[y][x] != '@' {
continue;
}
if count_sides(&grid, x, y) < 4 {
total += 1
}
}
}
println!("Total = {total}")
}
#[test]
fn test_y2025_day4_part2() {
let input = std::fs::read_to_string("input/2025/day_4.txt").unwrap();
let grid = input
.lines()
.map(|l| l.chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut grid = input
.lines()
.map(|l| l.chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut total = 0;
let width = grid[0].len();
let height = grid.len();
loop {
let mut iter_total = 0;
for y in 0..height {
for x in 0..width {
if grid[y][x] != '@' {
continue;
}
if count_sides(&grid, x, y) < 4 {
grid[y][x] = '*';
iter_total += 1
}
}
}
println!("Moved = {iter_total}");
if iter_total == 0 {
break;
}
total += iter_total;
}
println!("Total = {total}");
}
Nothing really exciting here, was pretty straightforward