Koru Just Solved All of Advent of Code 2015

· 8 min read AI Authored
language designkoruadvent of codecorpusstdlib

Koru solves every part of Advent of Code 2015 — all 49 of them, since day 25 is the only single-part puzzle in the event’s history. Every solution is pure .k: no host escapes, no puzzle-specific workarounds. The whole corpus runs through run_regression.sh with leak checking enabled: 49 passed, 0 failed.

Green doesn’t mean polished, and it shouldn’t be read that way. Read the solutions and you’ll find real warts — coordinates packed into an i64 because there’s no point type, parity arithmetic selecting actors without a branch, eight parameters threaded through four recursive tors. This post is not a victory lap; it’s a receipt and a roadmap. The follow-up posts walk the tor five days at a time, read every solution, and ask whether each spelling is worth keeping — where it isn’t, we fix the language.

If you have never seen Koru, this post is the orientation.

Koru, in one page

Koru is a language built around two ideas that are usually separate: flows with named branches, and compile-time obligations.

A flow pipes a value left to right with |>, and operations answer with named outcomes instead of returning-or-throwing:

std/fs:read-lines(path: "input.txt")
! line l |> for(l)
    ! each c |> ...
| done _ |> _
| failed e |> std/io:print.ln("FAILED {{ e:s }}")

! arms are effects that can resume; | done and | failed are the only ways the stream ends. There is no exception hierarchy — outcomes are part of the type surface.

State that must persist across steps lives in capture cells — immutable-looking records threaded through the flow:

capture { floor: 0[i32] }
! as acc |> ...
    |> captured { floor: acc.floor + 1 }
| captured result |> std/io:print.ln("{{ result.floor:d }}")

Owned values carry phantom types that track custody at compile time: <heap!> issues an obligation (this must be freed), <heap> borrows it, <!heap> consumes and discharges it. The checker follows the obligation path-by-path, so a std/set created inside a loop is freed whether you meant it to be or not — leaks are type errors.

And modules can synthesize code at compile time. std/list:new(<element>) doesn’t instantiate a generic — it materializes a concrete List_i64 or List_string container into your module on first use. There is no List<T> type system; specialization is synthesis, not generics.

The corpus

Every row below is a real regression test under tests/regression/810_AOC_2015/ — an executable .k file, a real expected.txt, verified by the same harness as everything else, leak-checked. Click a check to read the test and its generated learn page.

✅1·1 — floor countinga scalar fold; no collection needed at all
✅1·2 — basement positionthe same fold, asked to stop early
✅2·1 — wrapping paperarithmetic over parsed dimension triples
✅2·2 — ribbonsame parse, different metric
✅3·1 — visited housesset membership: "visited at least once"
✅3·2 — two Santastwo actors sharing one visited set, alternating
✅4·1 — MD5 prefix huntcrypto primitive + search-until-predicate
✅4·2 — six zerosthe same search, a harder predicate
✅5·1 — nice stringschar-level pair/triple predicates
✅5·2 — nicer stringsstring-map borrows across user-tor params
✅6·1 — light gridinstruction simulation over `std/grid`
✅6·2 — brightnesssame grid, different accumulator
✅7·1 — wire circuitfixpoint resolution over a rule table
✅7·2 — override wire ba user tor owning a map lifecycle, run twice
✅8·1 — escape codeschar-level state machine over string data
✅8·2 — re-encode lengthencode/decode round-trip
✅9·1 — shortest routepermutation search over city distances
✅9·2 — longest routethe same search, inverted objective
✅10·1 — look-and-saythe sequence as a packed-i64 number
✅10·2 — six roundssame encoding, one more round
✅11·1 — next passwordvalidity as arithmetic over base-26 digits
✅11·2 — second passwordthe same search, iterated once more
✅12·1 — JSON numbersa scanner that skips strings
✅12·2 — ignore objectsdepth-tracking parse
✅13·1 — seating chartcircular permutation optimization
✅13·2 — seat yourselfthe same optimizer, new constraint
✅14·1 — reindeer raceper-second state update over a roster
✅14·2 — points raceleader-change detection every tick
✅15·1 — cookie scoringnested-loop search over ingredient space
✅15·2 — 500 caloriesthe same search, plus a filter
✅16·1 — Sue matchingmulti-field record comparison
✅16·2 — ranged matchinginequality predicates over the same records
✅17·1 — container subsetscombinatorial enumeration, count the hits
✅17·2 — minimum containerssame enumeration, different objective
✅18·1 — Game of Lifegrid cellular automaton
✅18·2 — stuck cornerssame automaton, pinned cells
✅19·1 — one replacementstring rewriting + dedup set
✅19·2 — fabricate itIDDFS over rewrite rules — recursion with borrowed collections
✅20·1 — house numbersdivisor-sum number theory
✅20·2 — lazy elvesthe same theory, skipping non-divisors
✅21·1 — cheapest winshop loadout vs boss stats
✅21·2 — priciest lossthe same comparison, inverted
✅22·1 — scripted fightsturn-based state machine replay
✅22·2 — least manaDijkstra over packed states — needed `std/heap`
✅23·1 — register machineparse a program, interpret it
✅23·2 — a=1the same machine, the real 47-instruction input
✅24·1 — 3-way balancesubset-sum partitioning
✅24·2 — 4-way balancesame partition, deeper search
✅25·1 — diagonal tableclosed-form indexing + modular exponentiation — no iteration at all

What the solutions demonstrate

Four pieces of the corpus are worth reading closely, because each one exercises a different part of the language under real load.

Named groups are the destructure (day 2). std/regex:match returns capture groups as a branch payload schema — the pattern is the contract:

| `(?<l>[0-9]+)x(?<w>[0-9]+)x(?<h>[0-9]+)` { l: i64, w: i64, h: i64 } |> ...

The i64 types convert text to int at the splice; a non-matching line is a named | no-match arm. No unwrap, no error path — the regex either matches the schema or it doesn’t.

Unbounded search is a label fold (day 4). #search/@search threads a candidate through miss/found payloads with no mutation:

mine = #search std/crypto:md5-check(key, n: 1, zeros)
| miss v |> @search(key, n: v, zeros)
| found r -> r

The search protocol is named — miss keeps going, found delivers — rather than hidden inside a predicate.

Collections obey the phantom protocol under recursion (days 7, 19, 22). A user tor can take *List_i64<list> or *Map_i64_i64<map> as a borrow parameter and recurse through it — the obligation stays with the caller, the callee reads and writes in place, and free discharges at the boundary. Day 19 part 2 is a recursive IDDFS over rewrite rules doing exactly this; day 22 part 2 is Dijkstra over packed combat states doing it while a std/heap priority queue drains the frontier.

std/heap exists because the corpus needed it (day 22 part 2). Optimal play is uniform-cost search: states pack into i64 (spent << 47 | state), the heap key packs cost and state into one i64 so plain integer order is the search order, and std/map holds distances with lazy stale-entry deletion. The heap is a binary min-heap over i64 keys with the same <heap>/<heap!>/<!heap> protocol as every other collection — born from the puzzle, shipped as stdlib.

Boundaries

Two tests pin the statement’s numbers rather than the real puzzle input, and their headers say why: day 10’s real input runs 50 look-and-say rounds producing a ~20-million-digit sequence — a different surface entirely, documented in 810_101’s header and inherited by 810_102. Day 25 is single-part by the tor’s own design. Everything else runs the real input.

The series

Five posts follow, one per five-day group, each a design review of the actual solutions: days 1–5 (strings and grids), 6–10 (state and wires), 11–15 (search), 16–20 (simulation), 21–25 (machines). The question each post asks is the same — is every spelling worth keeping? — and the answer is allowed to be “fix the language.”