★ 4 of 24days solved: 2 of 12language: PowerShell
- 01Dusty Shed★★
- 02Fulminating Farmland★★
- 03Dec 3★★
- 04Dec 4★★
- 05Dec 5★★
- 06Dec 6★★
- 07Dec 7★★
- 08Dec 8★★
- 09Dec 9★★
- 10Dec 10★★
- 11Dec 11★★
- 12Dec 12★★
Blog · · 6 min read
Advent of Code 2026 in PowerShell
Twelve days of puzzles, two stars a day. We solve each one on the day it opens and log it here: a short explanation and the PowerShell that found the answer.
Dear friend,
we will write to you all December: a puzzle a day, two stars each and a little PowerShell.
To: everyone who collects stars
From: Tech Tea Studio
00 · Prologue
What this calendar is
Advent of Code is an advent calendar of programming puzzles that Eric Wastl has run every December since 2015. Each day opens a puzzle in two parts, and each part earns a star. The second part opens only after the first, and it usually turns the puzzle around so that the first solution has to change. Since 2025 the calendar is shorter: twelve days instead of twenty-five, so a season holds 24 stars.
Every player gets an input of their own, so the answers differ and cannot be copied. No language is required: any will do, even a spreadsheet.
This is our log of the 2026 season. The rules:
- every day gets a section: the puzzle in our own words, a short explanation and the code;
- the code is PowerShell, the very code that produced the answer. The object pipeline of PowerShell ships with every Windows, and it is more than enough for puzzles like these;
- we do not reprint the puzzles or our input: the author of the calendar asks players not to. Each section links to the puzzle itself.
Day 01 · December 1 · ★★
Dusty Shed: the most and the least common
The puzzle
The Elves are loading a supply train. The shed holds hundreds of containers, each labelled with the number of the kind of thing inside, and there is a list of those numbers, one per line. Nobody remembers what any number means. Part one asks for the three numbers that appear most often, multiplied together. Part two asks for the three that appear least often. The puzzle on the site.
The solution
This is a frequency table puzzle: group equal numbers, order the groups by size and take three from the end the part asks for. The two parts differ only in the end, so they share the grouping. In PowerShell the pipeline does it all: Group-Object counts the repeats, Sort-Object lays the groups out from rare to common, and ForEach-Object builds the product with its three blocks: before the first item, on every item and after the last one.
$g = gc input.txt | group | sort Count
# part 1: the three most common
$g[-3..-1] | % { $p = 1 } { $p *= $_.Name } { $p }
# part 2: the three least common
$g[0..2] | % { $p = 1 } { $p *= $_.Name } { $p }
Three fine points. A group's $_.Name is a string, but the number stands on the left of the multiplication, so PowerShell turns the string into a number by itself. A product past int makes PowerShell switch to double without a word, and a double loses precision on really big numbers; where an answer may grow, start with $p = [long]1. And the real trap of the puzzle: a tie at the edge of the three. If the third and the fourth place appear equally often, the answer is not unique, so look at the neighbours before you submit: $g[-4] for part one and $g[3] for part two.
Day 02 · December 2 · ★★
Fulminating Farmland: a robot on coloured waypoints
The puzzle
The train reaches a farm that collects thunder from passing storms. The Elves are tinkering with a robot that follows an endless line of coloured waypoints, six colours round and round, red, orange, yellow, green, blue, violet, watering, fertilising and weeding as it goes. Its program is a string of colours, and each letter sends it to the nearest next waypoint of that colour. Part one asks how far the robot travels on its program. Part two lets the Elves rearrange the program so that at every step the robot takes the instruction that carries it furthest, and asks the distance again. The puzzle on the site.
The solution
The colours go round in a circle, so each colour is just its index in roygbv, and the distance to a waypoint is the difference of indexes modulo 6. The trap: an instruction of the same colour does not move the robot by 0 but by a full 6, to the next waypoint of that colour. The formula ($t - $c + 5) % 6 + 1 gives 1 to 6 and takes care of it by itself.
In part two the order of the instructions no longer matters, only how many of each colour are left. So we count the colours first and then go greedily: the same colour gives 6, the colour one step back round the circle 5, two steps back 4, and so on. $k is how many steps back we went, and the move is worth 6 - $k.
$p = (gc input.txt -Raw).Trim().ToCharArray()
# part 1: the program as it is
$c = 0; $s = 0
$p | % {
$t = 'roygbv'.IndexOf($_)
$s += ($t - $c + 5) % 6 + 1
$c = $t
}
$s
# part 2: always the farthest colour left
$n = @(0) * 6
$p | % { $n['roygbv'.IndexOf($_)]++ }
$c = 0; $s = 0
while (($n | measure -Sum).Sum) {
$k = 0..5 | ? { $n[($c - $_ + 6) % 6] } | select -f 1
$c = ($c - $k + 6) % 6
$n[$c]--
$s += 6 - $k
}
$s
Small things to trip on. Trim() drops the line break at the end of the file: without it the last character is not in roygbv, IndexOf returns -1 and the distance goes wrong. @(0) * 6 in PowerShell is an array of six zeros, not a number. And select -f 1 stops at the first colour that is left, which is the farthest one because $k runs up from 0.
With warmth and gingerbread,
Tech Tea Studio
Glossary
- Advent of Code
- A yearly December calendar of programming puzzles. One puzzle in two parts a day; each answer is a single number or string.
- Star
- The reward for a right answer to one part of a puzzle. A day gives two, a season of twelve days 24.
- Input
- The data of a puzzle. The site gives every player their own, so every player has their own answer too.
- PowerShell pipeline
- A chain of commands joined by
|. Unlike a classic shell it carries objects with properties, not lines of text, so the commands understand each other without parsing. - Modulo
- What is left after whole division: 8 modulo 6 is 2. Handy for anything that goes round in a circle, like waypoint colours or the hours on a clock face.
- Greedy algorithm
- Solving a problem step by step, each time taking the best option right now without looking ahead. It does not always work, but here the puzzle itself asks for it.
- Frequency table
- A table of each value and how many times it appears. It takes one pass over the data and settles almost any "find the most common" puzzle.