Ved Thiru

Advent of Code Day 11 with awk

published 2025-07-09

flatmap but with stdin/stdout

Today I was reminded that Advent of Code exists and that I hadn’t completed many of the puzzles1, so I decided to work through more. (You can see my previous solutions too.)

I decided to use awk this time.


A summary of the transformation expected in the AoC problem:

  • 0s turns into 1s
  • even-length numbers get split into two numbers
  • odd-length numbers are multiplied by 2024

We can write this in awk!

$1 == 0 { print 1; next }

length($1)%2 { print $1 * 2024; next }

{
  m = length($1)/2
  print substr($1, 0, m) + 0
  print substr($1, m + 1) + 0
}

awk is a good tool for this sort of thing. Rules are run when their condition matches. The nexts move awk to the next line of input. That + 0 after the substr just ensures that the value is converted to a number before printing, since otherwise it prints trailing zeros, e.g. 000 when using the sample input.

This only does one “blink”, though. I wrote a quick shell script to pipe our input through this program 25 times. (It also splits out the input into separate lines, since the provided input is all on one line, space-separated.)

#!/bin/sh

input=$(sed 's/ /\n/g' < $1)

i=$2
while test "$i" -ne 0; do
  input=$(echo "$input" | awk -f blink.awk)
  i=$((i - 1))
done

echo "$input" | wc -l

Part 2 wants us to blink 75 times. Since the size of $input grows exponentially in this solution, it takes too long to compute after around blink 34. I won’t blog about my Part 2 solution for today, since it’s a run-of-the-mill, boring memoized depth-first search.


  1. AoC overlaps with final exams…
Tags: