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.

Worksheet
Problem0/4
Solved0
Total0
123 328  51 64 
 45 64  387 23 
  6 98  215 314
*   +   *   +  
Event Log
Waiting for events...

Part 2: Cephalopod Math (Vertical Reading) 🐙

Goal: Read numbers top-to-bottom in each column. Each column becomes one number!

Worksheet
Problem0/4
Solved0
Total0
123 328  51 64 
 45 64  387 23 
  6 98  215 314
*   +   *   +  
Event Log
Waiting for events...

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:

for (let i = lastLine.length - 1; i >= 0; i--) {
    const char = lastLine[i]

Clean Abstraction

Both parts share the same parsing and solving logic - only the number extraction (getColumn) differs:

function solve(problems: Problem[]) {
    let sum = 0;
    for (const problem of problems) {
        const { operation, initial } = operations[problem.operation]

The operations lookup table handles both + (initial: 0) and * (initial: 1) cleanly.

Performance

Part

Time

Part 1

2.01ms

Part 2

636µs