Skip to main content

Command Palette

Search for a command to run...

Advent of Code Day 7: Comparing Classical and Quantum Beams

Published
3 min read
Advent of Code Day 7: Comparing Classical and Quantum Beams
R

A techie Girl diving in Techo-Verse and philomath who is always ready to explore and learn. Keep spreading knowledge and love!

A Clean, Modular & Powerful Approach (Pseudocode Only)

When I saw Day 7 of Advent of Code, I knew this puzzle was going to be fun. A grid full of mirrors, a single light source, and two entirely different interpretations, Classical and Quantum.
So I decided to write a clean modular solution that works beautifully on Google Colab, is easy to debug, and super scalable.

Below is the full blog-style write‑up in Markdown, ready for upload.
(No actual code, only structured pseudocode and reasoning.)


Problem Summary

You're given a grid with:\

  • S --- the starting column\

  • . --- an empty cell\

  • ^ --- a splitter that duplicates the beam\

  • other characters ignored

From the row where S appears, a beam travels downward row by row.

There are two simulations:\

  1. Classical mode → beams split but don't accumulate.\

  2. Quantum mode → beams split and accumulate counts (timelines).


Why This Approach Rocks

  • Modular functionsload_grid, locate_source, classical simulator, quantum simulator.\

  • Handles uneven line lengths safely.\

  • Dictionary-based timelines make quantum counts efficient.\

  • No overthinking, no recursion, no complicated structures.\

  • Perfect for Colab --- runs instantly, extremely readable.


Pseudocode


Load and Normalize Grid

We pad shorter rows to avoid index errors.

FUNCTION load_grid(path):
    read all non-empty lines
    width = length of longest line
    return each line right-padded with '.' to width

Locate the Starting Point

Simple and clean search for S.

FUNCTION locate_source(grid):
    for each row index r and row value:
        if row contains 'S':
            return (r, column_of_S)

Part 1 --- Classical Beam Simulation

Beams split, but you only track positions, not counts.

FUNCTION simulate_classical(grid):
    start_row, start_col = locate_source(grid)
    beams = set containing start_col
    split_count = 0

    for each row below the start row:
        next_beams = empty set
        for each beam column:
            if cell == '^':
                increment split_count
                add (col - 1) and (col + 1) to next_beams
            else:
                add col to next_beams

        filter next_beams to keep indices >= 0 and < row_length

    return split_count

Part 2 --- Quantum Simulation

Now beams split and their counts accumulate.
This makes quantum behaviour exponential but super manageable with defaultdict.

FUNCTION simulate_quantum(grid):
    start_row, start_col = locate_source(grid)
    timelines = map { start_col: 1 }

    for each row below the start row:
        next_timelines = empty map

        for (col, count) in timelines:
            if cell == '^':
                add count to (col - 1)
                add count to (col + 1)
            else:
                add count to col

        remove invalid columns from next_timelines
        timelines = next_timelines

    return sum of all values in timelines

Final Output Format

You simply call:

grid = load_grid("yourfile.txt")

print Part 1 result
print Part 2 result

Why This Beats Most Solutions on the Internet

  • Zero dependencies.

  • Works for any grid size.

  • Handles all edge cases gracefully.

  • Quantum simulation scales without slowing down.

  • Super readable during AoC rush.


Connect with Me

If you liked this breakdown, check out more of my work:

  • LinkedIn: https://linkedin.com/in/raksha-pahariya-409842227\

  • GitHub: https://github.com/RP2025

I post daily breakdowns, clean coding approaches, and Advent of Code fun!
Let's make this go viral 🚀🔥