Metadata
Date
Tagged
Part of series
Older post
Advent of Code 2026 Day 3
Day 3: Foggy Forest
https://adventofcode.com/2026/day/3
CHOO, CHOO! The train arrives at a large forest.
The forest is partly natural, partly artificial. The artificial part has evenly spaced trees.
Todays input is a map of the forest.
An example input looks like this:
#...#...............#...##is a tree.is empty space
Part 1
The question asks the number of times that four trees occur exactly at the four corners of a grid-aligned square.
I loop through the input (2 nested loops, row indexes first, then columns).
Inside that second loop the (row, col) indexes indicate the top-left corner of a possible square.
I try every possible square size.
For each one, if all corners are '#', that’s a bingo!
pub fn part_1(input: &str) -> usize { let grid: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect(); let rows = grid.len(); let cols = grid[0].len();
let mut count = 0; for row in 0..rows { for col in 0..cols { if grid[row][col] != '#' { continue; }
let max_size = (rows - row).min(cols - col); for size in 1..max_size { if grid[row + size][col] == '#' && grid[row][col + size] == '#' && grid[row + size][col + size] == '#' { count += 1; } } } }
count}Part 2
Trees don’t need to be planted in a grid-aligned way, the squares can be rotated! As long as all sides are the same length, it’s valid.
That means an input like this is also a square:
.#.......#.....#.......#.I loop through the input again, picking corner1 as the top-left corner of a possible square.
Then, I loop through the grid below and to the right to pick corner2.
Next, I calculate the row and column offsets between those two corners (let diff = (row2 - row1, col2 - col1)).
Rotating those offsets 90 degrees gives the new offsets I add to corner1 and corner2 to calculate corner3 and corner4 respectively.
pub fn part_2(input: &str) -> usize { let grid: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect(); let rows = grid.len() as i32; let cols = grid[0].len() as i32;
let is_tree = |(row, col): (i32, i32)| -> bool { (0..rows).contains(&row) && (0..cols).contains(&col) && grid[row as usize][col as usize] == '#' };
let mut count = 0; for row1 in 0..rows { for col1 in 0..cols { let corner1 = (row1, col1); if !is_tree(corner1) { continue; }
for row2 in row1..rows { for col2 in col1 + 1..cols { let corner2 = (row2, col2); if !is_tree(corner2) { continue; }
let diff = (row2 - row1, col2 - col1); let rot = (diff.1, -diff.0);
let corner3 = (row1 + rot.0, col1 + rot.1); let corner4 = (row2 + rot.0, col2 + rot.1);
if is_tree(corner3) && is_tree(corner4) { count += 1; } } } } }
count}Make it faster
The nested loops scan every single cell in the grid, even the empty spaces. If the forest is mostly empty, that wastes a lot of time.
Instead, we can collect all tree coordinates into a Vec up front, and only loop over pairs of actual trees.
- We keep the 2D grid for
is_treelookups. - Because we collect trees row-by-row from left to right,
treesis already sorted by row, then column. - Time complexity drops from (all cells) to (just the trees).
pub fn part_2(input: &str) -> usize { let grid: Vec<&[u8]> = input.lines().map(str::as_bytes).collect(); let rows = grid.len() as i32; let cols = grid[0].len() as i32;
let is_tree = |(row, col): (i32, i32)| -> bool { (0..rows).contains(&row) && (0..cols).contains(&col) && grid[row as usize][col as usize] == b'#' };
let mut trees = Vec::new(); for row in 0..rows { for col in 0..cols { if grid[row as usize][col as usize] == b'#' { trees.push((row, col)); } } }
let mut count = 0; for (i, &corner1) in trees.iter().enumerate() { for &corner2 in &trees[i + 1..] { // Keep the same top-left then down-right ordering if corner2.1 <= corner1.1 { continue; }
let diff = (corner2.0 - corner1.0, corner2.1 - corner1.1); let rot = (diff.1, -diff.0);
let corner3 = (corner1.0 + rot.0, corner1.1 + rot.1); let corner4 = (corner2.0 + rot.0, corner2.1 + rot.1);
if is_tree(corner3) && is_tree(corner4) { count += 1; } } }
count}Final code
pub fn part_1(input: &str) -> usize { let grid: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect(); let rows = grid.len(); let cols = grid[0].len();
let mut count = 0; for row in 0..rows { for col in 0..cols { if grid[row][col] != '#' { continue; }
let max_size = (rows - row).min(cols - col); for size in 1..max_size { if grid[row + size][col] == '#' && grid[row][col + size] == '#' && grid[row + size][col + size] == '#' { count += 1; } } } }
count}
pub fn part_2(input: &str) -> usize { let grid: Vec<&[u8]> = input.lines().map(str::as_bytes).collect(); let rows = grid.len() as i32; let cols = grid[0].len() as i32;
let is_tree = |(row, col): (i32, i32)| -> bool { (0..rows).contains(&row) && (0..cols).contains(&col) && grid[row as usize][col as usize] == b'#' };
let mut trees = Vec::new(); for row in 0..rows { for col in 0..cols { if grid[row as usize][col as usize] == b'#' { trees.push((row, col)); } } }
let mut count = 0; for (i, &corner1) in trees.iter().enumerate() { for &corner2 in &trees[i + 1..] { // Keep the same top-left, then down-right ordering if corner2.1 <= corner1.1 { continue; }
let diff = (corner2.0 - corner1.0, corner2.1 - corner1.1); let rot = (diff.1, -diff.0);
let corner3 = (corner1.0 + rot.0, corner1.1 + rot.1); let corner4 = (corner2.0 + rot.0, corner2.1 + rot.1);
if is_tree(corner3) && is_tree(corner4) { count += 1; } } }
count}