Advent of Code 2026 in SQL: A DuckDB Starter Kit for Analysts

Advent of Code 2026 starts 1 December at 10:30 am IST. How to load puzzle inputs, parse lines and grids, and run loops in DuckDB SQL, plus what changed this year.

Divya NairDivya NairAuthor30 November 20265 min read 1 views

Researched and drafted with AI and reviewed by the Publisha team.

Advent of Code 2026 in SQL: A DuckDB Starter Kit for Analysts
In this article▾
  1. What's different about Advent of Code this year
  2. Why SQL is a good fit for many puzzles
  3. Step 1: load the input with a line number you can trust
  4. Step 2: parse numbers out of each line
  5. Step 3: turn a grid into rows and columns
  6. Step 4: loops, and when to stop using SQL
  7. A routine that fits around work

Advent of Code 2026 starts on 1 December, and if you work with data, there's a good case for solving it in SQL rather than in Python. Puzzles unlock at midnight US Eastern time, which is 10:30 am in India. Each one is a text input file plus a question, and a surprising share of them are really about parsing, grouping, joining and counting, which is exactly what analysts do all day. Below is a starter kit for DuckDB: how to load a puzzle input, split it into rows and grids, and when to admit SQL is the wrong tool.

What's different about Advent of Code this year

The Advent of Code about page answers the questions people ask every year, and a few answers have changed:

  • Fewer days. Creator Eric Wastl explains that the number of days per event changed. Puzzles still begin on 1 December and come out daily, ending mid-December, so you're looking at roughly two weeks, not twenty-five days.

  • No global leaderboard. It was removed after ten years. Private leaderboards still exist, so a team or a college group can run its own.

  • On AI, the answer is a plain "No." The FAQ compares it to sending a friend to the gym on your behalf. If you want the practice, write the queries yourself.

  • Don't publish inputs. If you post your solutions in a repository, leave out the puzzle text and your input files.

The site also promises that every problem has a solution that finishes in at most 15 seconds on ten-year-old hardware. That's a useful benchmark: if your query has been running for five minutes, the approach is wrong, not your laptop.

Why SQL is a good fit for many puzzles

Think of a puzzle input as a messy CSV that nobody documented. Your first job is the same as on any real dataset: get it into a table with one row per meaningful thing, with an index you trust. After that, a lot of puzzles become a GROUP BY or a self-join.

DuckDB is a comfortable place to do this. It runs in a single process, reads files directly, and has list functions that make splitting text easy. If you haven't installed or upgraded it recently, this DuckDB 2.0 upgrade checklist covers what changed in the current major version. The snippets below were run on DuckDB 2.0.1.

Step 1: load the input with a line number you can trust

The read_text function returns a whole file as a single text value. Split it on newlines, and use generate_subscripts to keep each line's position:

CREATE TABLE lines AS
SELECT generate_subscripts(parts, 1) AS line_no,
       unnest(parts)                 AS line
FROM (
  SELECT string_split(trim(content, chr(10)), chr(10)) AS parts
  FROM read_text('input.txt')
);

Two details matter here. chr(10) is the newline character, because a '\n' inside a SQL string is just a backslash and the letter n. And the trim removes the trailing newline most input files end with, which would otherwise give you an empty last row.

The common mistake is reaching for row_number() OVER () to number lines. Without an ORDER BY, nothing guarantees the order you get, and many puzzles depend on line order. Taking the position from the list itself avoids the question entirely.

Step 2: parse numbers out of each line

Say a toy input has comma-separated numbers on each line: 3,9,4, then 12,1, then 7,7,7,2, and you need the sum of each line's largest number. Cast the split strings to an integer list and use the list functions:

SELECT sum(list_max(string_split(line, ',')::INTEGER[])) AS answer
FROM lines;

On the toy input, the largest values are 9, 12 and 7, so the answer is 28. When numbers are buried in words, such as move 12 from 3 to 7, pull them out with a regular expression:

SELECT regexp_extract_all(line, '\d+')::INTEGER[] AS nums
FROM lines;

That returns the list 12, 3, 7. Keep the parsed table separate from the raw lines. When an answer comes out wrong, the first question is always whether the parsing is right, and you'll want to check it on the example from the puzzle text before blaming the logic.

Step 3: turn a grid into rows and columns

Grid puzzles, where the input is a block of characters like #.. and .#., are where SQL shines. Explode every character into its own row with a row and column number:

CREATE TABLE grid AS
WITH rows AS (
  SELECT generate_subscripts(parts, 1) AS r, unnest(parts) AS line
  FROM (SELECT string_split(trim(content, chr(10)), chr(10)) AS parts
        FROM read_text('grid.txt'))
)
SELECT r,
       generate_subscripts(string_split(line, ''), 1) AS c,
       unnest(string_split(line, ''))                 AS ch
FROM rows;

Now "count the neighbours of each cell" is a self-join. Write it against a small table of the eight neighbour offsets, so the join is on exact coordinates:

WITH offsets AS (
  SELECT dr, dc
  FROM (VALUES (-1), (0), (1)) AS t1(dr),
       (VALUES (-1), (0), (1)) AS t2(dc)
  WHERE NOT (dr = 0 AND dc = 0)
)
SELECT a.r, a.c, count(*) AS hash_neighbours
FROM grid a
CROSS JOIN offsets o
JOIN grid b ON b.r = a.r + o.dr AND b.c = a.c + o.dc
WHERE b.ch = '#'
GROUP BY a.r, a.c;

Why not the more obvious ON abs(a.r - b.r) <= 1 AND abs(a.c - b.c) <= 1? It gives the same answer, but it's an inequality condition. Run EXPLAIN on it and DuckDB shows a nested-loop join, which compares every cell with every other cell. On a 140 by 140 grid that's 19,600 cells and about 384 million pairs. The offsets version needs eight lookups per cell, about 157,000. Same result, very different bill. If a query suddenly takes minutes, check the join condition first.

Step 4: loops, and when to stop using SQL

Some puzzles are simulations: apply a rule, then apply it again, until something happens. SQL handles this with a recursive CTE, which DuckDB documents under its WITH clause. Here's the shape, using the classic "halve if even, else triple and add one" rule:

WITH RECURSIVE steps(n, value) AS (
  SELECT 0, 27
  UNION ALL
  SELECT n + 1,
         CASE WHEN value % 2 = 0 THEN value // 2 ELSE 3 * value + 1 END
  FROM steps
  WHERE value <> 1
)
SELECT max(n) AS steps_to_one FROM steps;

Starting from 27 it takes 111 steps. That's fine. But recursive CTEs get awkward when each step has to update a whole grid, track visited states, or search for shortest paths with priorities. That's usually the later, harder puzzles. A sensible rule: if your recursive query needs more than one level of nesting, or you're fighting the engine to keep state between iterations, solve that day in Python and come back to SQL the next morning. If you go that way, the newer pandas 3.1 select() and filter() methods make the parsing step a little tidier.

A routine that fits around work

At 10:30 am IST the puzzle drops in the middle of most people's working day, and with no global leaderboard there's nothing to race for. Read the puzzle over lunch, write the parsing query in the evening, and check it against the worked example in the puzzle before running it on your full input. Keep a file of the loading and grid snippets above. By the fourth or fifth day you'll find you mostly copy those and spend your time on the one query that's actually new, which is a decent picture of real analysis work too.

Divya Nair

Written by

Divya Nair

A Publisha editorial profile. Its stories are researched and drafted with AI and reviewed by the Publisha team.

Keep reading

More from Divya Nair

More from Divya Nair

More in Guides & Tutorials

Have a story of your own?

Publisha is free to start. Write with AI that keeps your voice, and publish in a click.