← all posts
Sun 06 Dec 2026 · 12 min read

Advent of Code 2026

It’s December, so I’m giving Advent of Code another go. Last time I wrote about it I used it to learn Rust, and this year it’s Gleam’s turn1.

Rather than one write-up at the end (or, let’s be honest, a few weeks into January), I’m going to try and add some thoughts here each day.

Day 1 - Reading Is Hard

As usual, Day 1 was pretty easy, once I’d read it correctly.

I’m sure my Gleam is terrible, but the implementation ended up being a pipeline: take the list, sort it2, group it by value, map those groups to counts, sort again, take the top three, and multiply them together…

If this year’s AoC is anything like previous years, we will be doing some parsing of text files every day, so the little parse function I have will get expanded.

gleam
1fn parse(input: String) -> List(Int) {
2  input
3  |> string.trim
4  |> string.split("\n")
5  |> list.map(int.parse)
6  |> result.values
7}

Part 1 - Read the Question

My biggest issue with part 1 was my reading comprehension. I solved completely the wrong problem before I re-read the puzzle.

gleam
 1pub fn part1(input: String) -> String {
 2  parse(input)
 3  // Un-needed sort was here.
 4  |> list.group(fn(x) { x })
 5  |> dict.to_list
 6  |> list.map(fn(x) { #(x.0, list.length(x.1)) })
 7  |> list.sort(fn(a, b) { int.compare(b.1, a.1) })
 8  |> list.take(3)
 9  |> list.map(fn(x) { x.0 })
10  |> int.product()
11  |> int.to_string()
12}

Part 2 - Flip the Sort

Part 2 just needs the least common values instead of the most common. With my implementation, this just meant swapping the order of a sort.

gleam
 1pub fn part2(input: String) -> String {
 2  parse(input)
 3  |> list.group(fn(x) { x })
 4  |> dict.to_list
 5  |> list.map(fn(x) { #(x.0, list.length(x.1)) })
 6  |> list.sort(fn(a, b) { int.compare(a.1, b.1) }) // <-- a/b swapped here.
 7  |> list.take(3)
 8  |> list.map(fn(x) { x.0 })
 9  |> int.product()
10  |> int.to_string()
11}

Day 2 - A Trench Coat of Letters

Day 2’s problem again turned out to be quite easy, but was wrapped in a trench coat of letter mapping that obscured the problem. I approached this with a similar mindset to Day 1, using a set of transforms to map the problem into something easier to understand and then working over the lists.

The input is a single line of colours, one letter each: r, o, y, g, b and v, the colours of the rainbow3. So parsing is just a trim and splitting it into characters.

gleam
1fn parse(input: String) -> List(String) {
2  input
3  |> string.trim
4  |> string.to_graphemes
5}

Part 1 - Round the Rainbow

normalise turns each colour into its place in the rainbow, red as 0 up to violet as 5, which makes the colours a wheel of six steps.

p1 then puts a red (0) on the front as the starting point, and uses list.window_by_2 to walk through the list in overlapping pairs. For each pair it works out how many steps forward round the wheel it takes to get from the first colour to the second. If the next colour is further on, that’s just b - a. If it’s behind, or the same colour, it has to go all the way round, so it’s 6 - (a - b), which makes the same colour twice a full lap of 6.

part1 adds those up.

gleam
 1pub fn part1(input: String) -> String {
 2  input
 3  |> parse
 4  |> p1()
 5  |> int.sum
 6  |> int.to_string
 7}
 8
 9fn p1(i) -> List(Int) {
10  i
11  |> list.map(fn(x) { normalise(x) })
12  |> list.prepend(0)
13  |> list.window_by_2
14  |> list.map(fn(x) {
15    let #(a, b) = x
16    case a >= b {
17      True -> 6 - { a - b }
18      False -> b - a
19    }
20  })
21}
22
23pub fn normalise(current: String) -> Int {
24  case current {
25    "r" -> 0
26    "o" -> 1
27    "y" -> 2
28    "g" -> 3
29    "b" -> 4
30    "v" -> 5
31    _ -> -1
32  }
33}

Part 2 - Bunching the Colours

Part 2 was again mostly a problem of understanding the task, not implementing it. I did try and do this in “one step” and work out the results directly from my list of how many reds in a row (and so on) we had, but as the problem was still pretty small just generating an input to part 1 worked fine.

It reuses all of part 1. count counts how many of each colour there are, in the order r, v, b, g, y, o, which is the rainbow backwards starting from red. count_to_string then builds a new list from those counts with each colour bunched together, in that same order, and the result goes through p1 exactly as before.

list.repeat and list.flatten did most of the work there, and let assert is a handy way of pulling the six counts back out of the list, as long as you’re sure there are six of them.

gleam
 1pub fn part2(input: String) -> String {
 2  input
 3  |> parse
 4  |> count
 5  |> count_to_string
 6  |> p1()
 7  |> int.sum
 8  |> int.to_string
 9}
10
11pub fn count(i: List(String)) -> List(Int) {
12  let r = ["r", "v", "b", "g", "y", "o"] // optimal order
13  list.map(r, fn(x) { list.count(i, fn(y) { y == x }) })
14}
15
16pub fn count_to_string(i: List(Int)) -> List(String) {
17  let assert [r, v, b, g, y, o] = i
18  let ll = [
19    list.repeat("r", times: r),
20    list.repeat("v", times: v),
21    list.repeat("b", times: b),
22    list.repeat("g", times: g),
23    list.repeat("y", times: y),
24    list.repeat("o", times: o),
25  ]
26  list.flatten(ll)
27}

Day 3 - Finding Squares

Another classic AoC puzzle: dealing with input that’s a grid and answering questions about it. This problem feels very verbose in Gleam, at least the way I have written it, with “loops, in loop”4.

The input is a grid of #s and .s. parse splits it into rows and characters, records the grid’s width and height, and walks it with a pair of recursive functions, rows and row, putting every # into a Dict keyed on its coordinates. The .s aren’t stored at all, so a missing key means an empty spot.

gleam
 1pub type Grid {
 2  Grid(width: Int, height: Int, data: dict.Dict(#(Int, Int), Bool))
 3}
 4
 5fn parse(input: String) -> Grid {
 6  let x =
 7    input
 8    |> string.trim
 9    |> string.split("\n")
10    |> list.map(string.to_graphemes)
11
12  let #(width, height) = width_and_height(x)
13  rows(x, 0, dict.new())
14}
15
16fn width_and_height(i: List(List(String))) -> #(Int, Int) {
17  let assert Ok(row) = list.first(i)
18  let row_length = list.length(row)
19  let row_count = list.length(i)
20  #(row_length, row_count)
21}
22
23fn rows(
24  input: List(List(String)),
25  row_id: Int,
26  d: dict.Dict(#(Int, Int), Bool),
27) {
28  case input {
29    [x, ..xs] -> {
30      let d = row(x, row_id, 0, d)
31      rows(xs, row_id + 1, d)
32    }
33    [] -> d
34  }
35}
36
37fn row(
38  input: List(String),
39  row_id: Int,
40  col_id: Int,
41  d: dict.Dict(#(Int, Int), Bool),
42) -> dict.Dict(#(Int, Int), Bool) {
43  case input {
44    [x, ..xs] -> {
45      let d = case x {
46        "#" -> dict.insert(d, #(row_id, col_id), True)
47        _ -> d
48      }
49
50      row(xs, row_id, col_id + 1, d)
51    }
52    [] -> d
53  }
54}

Part 1 - Straight Squares

boxes starts with squares as big as the grid and works down a size at a time until it gets to 1. For each size it tries every top-left corner that leaves room for the square, using int.range as a fold to keep a running count, and asks has_box whether all four corners are a #. A spot that isn’t in the Dict comes back as an Error, so anything off the grid or empty just doesn’t count.

gleam
 1pub fn part1(input: String) -> String {
 2  let g =
 3    input
 4    |> parse
 5  int.to_string(boxes(g.width, g, 0))
 6}
 7
 8fn boxes(size: Int, g: Grid, count: Int) {
 9  case size {
10    1 -> count
11    s -> {
12      let new_count =
13        int.range(from: 0, to: g.width - size + 1, with: count, run: fn(acc, x) {
14          int.range(
15            from: 0,
16            to: g.height - size + 1,
17            with: acc,
18            run: fn(acc, y) { acc + has_box(y, x, g, size, 0) },
19          )
20        })
21      boxes(s - 1, g, new_count)
22    }
23  }
24}

Part 2 - Tilted Squares

My friend, who is also participating in AoC this year, sent me a GIF of a rotating square… When I got to this part, the GIF made sense.

Part 2 counts tilted squares too. A tilted square still fits inside a straight bounding box, with one corner on each side of it, so boxes_with_rotation is boxes with a third loop. rotation slides each corner the same distance along its side of the box: 0 is the straight square from part 1, and each step after that tilts it a bit more, until it would come back round to straight again.

gleam
 1pub fn part2(input: String) -> String {
 2  let g =
 3    input
 4    |> parse
 5
 6  int.to_string(boxes_with_rotation(g.width, g, 0))
 7}
 8
 9fn boxes_with_rotation(size: Int, g: Grid, count: Int) {
10  case size {
11    1 -> count
12    s -> {
13      let new_count =
14        // X
15        int.range(from: 0, to: g.width - size + 1, with: count, run: fn(acc, x) {
16          // Y
17          int.range(
18            from: 0,
19            to: g.height - size + 1,
20            with: acc,
21            run: fn(acc, y) {
22              // R
23              int.range(from: 0, to: size - 1, with: acc, run: fn(acc, r) {
24                acc + has_box(y, x, g, size, r)
25              })
26            },
27          )
28        })
29      boxes_with_rotation(s - 1, g, new_count)
30    }
31  }
32}
33
34fn has_box(y: Int, x: Int, g: Grid, size: Int, rotation: Int) -> Int {
35  let i1 = #(x + rotation, y)
36  let i2 = #(x + size - 1, y + rotation)
37  let i3 = #(x, y + size - 1 - rotation)
38  let i4 = #(x + size - 1 - rotation, y + size - 1)
39
40  let c1 = dict.get(g.data, i1)
41  let c2 = dict.get(g.data, i2)
42  let c3 = dict.get(g.data, i3)
43  let c4 = dict.get(g.data, i4)
44
45  case c1, c2, c3, c4 {
46    Ok(True), Ok(True), Ok(True), Ok(True) -> 1
47    _, _, _, _ -> 0
48  }
49}

Day 4 - The First Cheat

This was the first day I had to really cheat. I spent a lot of time trying to think of ways of building permutations, and of checking whether those strings matched the inputs. This was a dead end, at least for me in Gleam, so I got a hint that it doesn’t matter what the input is, just that an input exists. So what you’re really looking for is whether every character at the same position % n is the same.

The input is lines of rs, gs and bs, with ?s for the gaps, and parse splits it into a list of characters per line.

gleam
1fn parse(input: String) -> List(List(String)) {
2  input
3  |> string.trim()
4  |> string.split("\n")
5  |> list.map(string.to_graphemes)
6}

Part 1 - Shortest Repeat

test_length is the hint turned into code. It numbers each character, throws away the ?s, and groups the rest by their position % length. If any group has more than one different character in it, that line can’t be a pattern repeating every length characters. If none do, it can, whatever the ?s turn out to be.

For example, with a length of 2, this works:

text
11,2,3
2r,g,r

Positions 1 and 3 land in the same group, and they’re both r. Position 2 is on its own, so it’s the “same” by default. But this doesn’t:

text
11,2,3
2r,g,b

Position 2 is still alone, but the group with positions 1 and 3 now has two different values in it.

try then starts with a length of 1 and uses list.partition to split the lines into the ones that work at that length and the ones that don’t. The ones that work score length each, and the rest go round again with a length one longer, until it runs past the longest line.

gleam
 1pub fn part1(input: String) -> String {
 2  let input =
 3    input
 4    |> parse
 5
 6  let assert Ok(max) =
 7    list.map(input, fn(x) { list.length(x) }) |> list.max(int.compare)
 8  let c = try(input, 1, max)
 9  int.to_string(c)
10}
11
12fn try(input: List(List(String)), length: Int, max: Int) {
13  case length {
14    x if x > max -> 0
15    _ -> {
16      let #(g, b) = list.partition(input, fn(x) { test_length(x, length) })
17
18      { list.length(g) * length } + try(b, length + 1, max)
19    }
20  }
21}
22
23pub fn test_length(input: List(String), length: Int) -> Bool {
24  let l =
25    input
26    |> list.index_map(fn(x, i) { #(i, x) })
27    |> list.filter(fn(x) {
28      let #(_, c) = x
29      c != "?"
30    })
31    |> list.group(fn(x) {
32      let #(i, _) = x
33      i % length
34    })
35    |> dict.to_list
36    |> list.map(fn(x) {
37      let #(_, a) = x
38      list.map(a, fn(x) {
39        let #(_, c) = x
40        c
41      })
42    })
43    |> list.filter(fn(x) { list.unique(x) |> list.length > 1 })
44
45  l == []
46}

Part 2 - One Long Line

Part 2 reuses all of part 1. part2 flattens all the lines into one long line, wraps it back up in a list, and hands it to the same try. Because we had a good solution to part 1 thanks to the hint, this just work.

gleam
 1pub fn part2(input: String) -> String {
 2  let input =
 3    input
 4    |> parse
 5    |> list.flatten
 6  let input = [input]
 7
 8  let assert Ok(max) =
 9    list.map(input, fn(x) { list.length(x) }) |> list.max(int.compare)
10  let c = try(input, 1, max)
11  int.to_string(c)
12}

  1. Why Gleam? Just a new language to play with. ↩︎

  2. Which it turns out isn’t needed, as list.group doesn’t care about the order. ↩︎

  3. Indigo didn’t make the cut. ↩︎

  4. Well, int.ranges in int.ranges. ↩︎

search.exe
esc to close