← Back to list

Advent of Code 2025 Day 7

01 · 02 · 03 · 04 · 05 · 06 · 07 · 08 · 09 · 10· 11 · 12

Kav · 2026-05-13 14:06 · 0 claps · 2.5 min read
#advent-of-code #programming #rust #software-development #algorithms
Open on Medium ↗
Wiki topics: 💻 · Programming

Advent of Code 2025 Day 7

01 · 02 · 03 · 04 · 05 · 06 · 07 · 08 · 09 · 10· 11 · 12

We’re on Day 7 and after implementing the first six days in C++ and translating the code to Rust, it’s time to go Rust all the way.

The day 7 problem statement and test input are as follows:

.......S.......
...............
.......^.......
...............
......^.^......
...............
.....^.^.^.....
...............
....^.^...^....
...............
...^.^...^.^...
...............
..^...^.....^..
...............
.^.^.^.^.^...^.
...............

Here’s how this problem goes. At the starting point, indicated by S, a laser beam | starts and makes it way downwards. When it encounters a ^ the beam splits into two, on either side of the splitter. Any overlapping beams simply combine into one, and continue to make their way down. After a few steps, this is what the input would look like:

.......S.......
.......|.......
......|^|......
......|.|......
.....|^|^|.....
.....|.|.|.....

The first problem is to find how many unique splitters the beams hit. To do so, I just wrote a loop to go down line by line and follow the beams’ paths. On any given line, I go character by character, and if the given character is:

  • an empty position ., if the character above is a laser beam | then the current character becomes a laser as well (simulating the laser travelling straight down)
  • a splitter ^, if the character above is a laser beam | then the character to the left and right of the splitter becomes a laser (simulating the laser splitting in two). Count it as a split

At the end, we would have the number of beam splits.

For part two, we just add quantum to the problem. Because that just solves everything. And then borrowing a little something from Dr. Strange, we need to find out the number of timelines where a single beam completes its possible journeys through the manifold. A beam splitting in two makes two timelines, but sometimes timelines combine back up into one. So we go to our old friend that more powerful than the time stone: recursion.

Let’s start by defining our function:

fn get_n_paths(grid: &Vec<&[u8]>, r: usize, c: usize) -> i64

Our base case is when the beam reaches the bottom. That’s the one and only path for that beam:

if r >= grid.len() - 1 {
    return 1;
}

Then the case where the beam needs to travel straight down. Once again, a single path. We just call the function again on the next row:

if grid[r + 1][c] == b'.' {
    return get_n_paths(grid, r + 1, c, counts);
}

The only fun scenario is when we hit a splitter. In this case, we call it twice, one for each path taken, left and right:

if grid[r + 1][c] == b'^' {
    return get_n_paths(grid, r + 1, c - 1, counts)
        + get_n_paths(grid, r + 1, c + 1, counts);
}

The final little thing left to do was to maintain a cache of already-calculated paths so we’re not going down paths we’ve already been down and making our code exponentially slow. Here’s the full function with the cache:

let mut cache: Vec<Vec<i64>> = vec![vec![-1; grid[0].len()]; grid.len()];
fn get_n_paths(grid: &Vec<&[u8]>, r: usize, c: usize, cache: &mut Vec<Vec<i64>>) -> i64 {
    if cache[r][c] != -1 {
        return cache[r][c];
    }
    if r >= grid.len() - 1 {
        cache[r][c] = 1;
        return 1;
    }
    if grid[r + 1][c] == b'^' {
        cache[r][c] = get_n_paths(grid, r+1, c-1, cache)
                     + get_n_paths(grid, r+1, c+1, cache);
        return cache[r][c];
    }

    cache[r][c] = get_n_paths(grid, r+1, c, cache);
    return cache[r][c];
}

And there we have it.

Full source code: https://github.com/kabbagepatch/AdventOfCode2025/blob/main/day07/rust/src/main.rs


메타데이터
post_id
f3ca32dc596f
slug
advent-of-code-2025-day-7-f3ca32dc596f
url
https://medium.com/@kabbagepatch/advent-of-code-2025-day-7-f3ca32dc596f
canonical_url
https://medium.com/@kabbagepatch/advent-of-code-2025-day-7-f3ca32dc596f
author_url
https://medium.com/@kabbagepatch
status
ok
fetched_at
2026-07-10 03:02:36