Post

Advent of Code 2026 — Day 1, with Snowflake

5 min read

The Advent of Code 2026 has started. This year I decided to solve the challenges (time permitting, as December is always a crazy month) using Snowflake.

If you have been following me for a while, you know that back in 2022 I did the same with T-SQL and Azure SQL. A lot has changed since then: this year I joined Snowflake, and in my new role I work on the SQL language itself. Doing the Advent of Code is a great excuse to get closer to the technology, play with it every day, and find out what works well and what could be improved.

There is also another reason. Snowflake has, at least from what I have seen so far, one of the most complete and extensive SQL surfaces out there: lambdas, semi-structured data, QUALIFY, MATCH_RECOGNIZE, higher-order functions, custom aggregates…you name it. That makes solving puzzles with SQL much more interesting, and I am sure I am going to learn a lot along the way. And I want to share what I learn.

I hope these posts will be useful to those who are relatively new to Snowflake, as they will show some features in action on fun, well-defined problems. And maybe they will be useful to some veterans too. Who knows?

To make it easy to follow along, I’ll be using a Snowflake trial account. That way, if you want to try the solutions yourself, you can do it without spending any money!

Well, time to get started!

Advent Of Code 101

If you have never heard of it, the Advent of Code is an Advent calendar of small programming puzzles: from December 1st, a new puzzle is unlocked every day, each with two parts, and you can solve them in any language you like. Or, in my case, in SQL.

Today’s challenge (Day 1) is very easy, and with SQL it is going to be solved in a second. Great to start stretching our SQL muscles!

Loading the input

The first thing to do is to get the puzzle input into Snowflake. Snowflake has the super convenient feature of providing a staging area right out of the box: every user gets a personal stage, called the user stage and referenced as @~. That makes uploading files to Snowflake, so that they can then be used to import data, super easy.

Everything can be done via the UI too, but I just prefer to use the CLI tool available: the Snowflake CLI (snow). Copying the file into my user stage is a one-liner:

snow stage copy -c TRIAL ./day-01-input.txt @~/day-01-input.txt

The -c option means that I’m using a connection named TRIAL, which points to my trial account.

Then, in Snowflake, I took a look at the file content. Snowflake can query a staged file directly, without loading it first. All you need is a file format. Since I just want each line as-is, I created a CSV file format with no field delimiter, so that every line becomes a single column:

USE WAREHOUSE COMPUTE_WH;
USE DATABASE AdventOfCode;

LIST @~;

CREATE FILE FORMAT IF NOT EXISTS raw_lines
    TYPE = CSV
    FIELD_DELIMITER = NONE
    COMPRESSION = NONE
    MULTI_LINE = FALSE;

SELECT METADATA$FILE_ROW_NUMBER AS row_num, $1 AS line
FROM @~/day-01-input.txt (FILE_FORMAT => 'raw_lines')
ORDER BY row_num;

$1 is the first (and only) column of the file, and METADATA$FILE_ROW_NUMBER returns the line number.

With that set, now I can use the powerful COPY INTO command to move the data from the file into a table:

CREATE OR REPLACE TABLE DAY01.INPUT (row_num INT, id NUMBER);

COPY INTO DAY01.INPUT (row_num, id)
FROM (SELECT METADATA$FILE_ROW_NUMBER, $1 FROM @~/day-01-input.txt)
FILE_FORMAT = (FORMAT_NAME = 'raw_lines')
ON_ERROR = ABORT_STATEMENT;

SELECT * FROM DAY01.INPUT;

That’s it, super easy. We’re now ready for the challenge!

Part 1

The first part asks for the product of the three most common items in the list. If you are familiar with relational databases, “most common” should immediately sound like a GROUP BY:

SELECT id, COUNT(*) AS c
FROM DAY01.INPUT
GROUP BY id
ORDER BY c DESC;

Then take the top three with LIMIT 3, multiply them, and we are done. Easy, right? Well, almost. SQL has SUM, but there is no PRODUCT aggregate. In T-SQL, I would have used the good old math trick EXP(SUM(LN(x))), which works but is not exactly readable, and really looks like a hack. And there is no need for a hack, right now :)

In Snowflake, in fact, I can use ACCUMULATE, which lets you define any aggregate right in SQL using lambdas. I wrote about it here, and the Day 1 puzzle is the perfect occasion to use it:

WITH cte AS (
    SELECT id, COUNT(*) AS c
    FROM DAY01.INPUT
    GROUP BY id
    ORDER BY c DESC
    LIMIT 3
)
SELECT
    ACCUMULATE(
        id,
        (v) -> v,               -- Initialize
        (s, v) -> s * v,        -- Accumulate
        (s1, s2) -> s1 * s2,    -- Combine
        (s) -> s                -- Terminate
    ) AS prod_of_rows
FROM
    cte;

The first argument is the input expression. Then you define four lambdas:

  • Initialize: creates the state from the first value it sees. Here it is just the value itself.
  • Accumulate: adds a new value to the existing state. For a product, multiply.
  • Combine: merges two partial states. Snowflake processes data in parallel, so partial results computed on different workers must be combined. Again, multiply.
  • Terminate: turns the final state into the result. Here, nothing to do.

Part 1, done.

Part 2

Part two asks for the same product, but over the three least common items. That only requires flipping the direction of the ORDER BY:

WITH cte AS (
    SELECT id, COUNT(*) AS c
    FROM DAY01.INPUT
    GROUP BY id
    ORDER BY c ASC
    LIMIT 3
)
SELECT
    ACCUMULATE(
        id,
        (v) -> v,
        (s, v) -> s * v,
        (s1, s2) -> s1 * s2,
        (s) -> s
    ) AS prod_of_rows
FROM
    cte;

Part 2, done. I told you this one was easy. 😅

Conclusion

Day 1 was a warm-up, but it was already a nice tour of what Snowflake gives you right out of the box: a user stage to drop files into, the ability to query those files directly, the powerful COPY INTO to load them into a table, and ACCUMULATE to build the PRODUCT aggregate that SQL is missing. No hacks needed.

I’m pretty sure the next days won’t be this easy, but that’s exactly where the fun begins. See you tomorrow for Day 2!