The Problem
You've fallen into a garbage compactor (classic! 🎬) and while waiting for rescue, a family of cephalopods asks for help with math homework. Their worksheet has problems arranged in columns, with the operation (+ or *) at the bottom.
Summary
Part | Question | Strategy |
|---|---|---|
1 | Numbers read horizontally (row-wise) | Slice substrings from each row |
2 | Numbers read vertically (column-wise) | Build numbers digit-by-digit down each column |
Part 1: Human Math (Horizontal Reading)
Goal: Read numbers left-to-right within each problem's columns, one number per row.
Part 2: Cephalopod Math (Vertical Reading) 🐙
Goal: Read numbers top-to-bottom in each column. Each column becomes one number!
Spaces are skipped when building the vertical number!
Parsing Strategy: Right-to-Left
The clever part is parsing problems from right to left. We scan the bottom row (operators) backwards, tracking the width of each problem:
Clean Abstraction
Both parts share the same parsing and solving logic - only the number extraction (getColumn) differs:
The operations lookup table handles both + (initial: 0) and * (initial: 1) cleanly.
Performance
Part | Time |
|---|---|
Part 1 | 2.01ms |
Part 2 | 636µs |