
The Problem
The printing department has rolls of paper (@) arranged on a grid. Forklifts can only access a roll if it has fewer than 4 neighbors (out of 8 possible adjacent positions). Help optimize the forklifts to clear the way to the cafeteria!
Summary
Part | Question | Strategy |
|---|---|---|
1 | How many rolls can forklifts initially access? | Count rolls with < 4 adjacent neighbors |
2 | How many total rolls can be removed? | Iteratively remove accessible rolls until stable |
Part 1: Count Accessible Rolls
Goal: Count rolls of paper (@) that have fewer than 4 filled neighbors.
The 8 Neighbors
Each cell has up to 8 adjacent positions (including diagonals):
Counting Neighbors
A roll is accessible if adjacentCount < 4. Simple scan of the grid:
Part 2: Cascade Removal 🎯
Goal: Remove accessible rolls, then check again. Repeat until no more can be removed.
This is a cellular automaton-style simulation! Removing rolls exposes new rolls that become accessible.
The Cascade Effect
Rolls on the edges or with few neighbors get removed first. This exposes interior rolls, which then become removable.
The Algorithm
Why Batch Removal?
We collect all accessible rolls before removing any. This ensures we don't accidentally affect the neighbor count of rolls we haven't checked yet in this iteration.
If we removed one-by-one, the order would matter and we'd get inconsistent results!
Performance
Part | Time |
|---|---|
Part 1 | 6.11ms |
Part 2 | 40.77ms |
Part 2 is slower because it runs multiple iterations until the grid stabilizes. Each iteration requires a full grid scan. There is probably some optimization to do there.