Blogmark

Taskfile for Advent of Code

via [email protected]

I wasn't planning on doing any Advent of Code this year. However, with so much of my workday being abstracted to prompting code instead of writing it, I had the itch to solve some problems with hand-chiseled code.

I did start with a prompt though.

Can you create a Taskfile for this project that has a primary invocation mode where you pass it a number (it will be 1 through 6, but no need to validate that) which will then run a series of commands to validate the Advent of Code solutions for that day.

That day number will correspond to a file like 0N, so for 1, it will be 01.

The prerequisites when it runs is that there should be a sample_input.txt file and a part_one.py file. Those are needed at a minimum to run. In which case, it will run part one with that sample file as the first argument/input. It will report the result formatted with the run context (e.g. Part One -> Sample Data or however you want to structure it to look nice). If input.txt is also available, then it should also run part one against that main input, outputting the result. Lastly, if part_two.py is present, then it should run part_two against the sample and main inputs, reporting the results of each.

If there is anything underspecified or you have a clarifying question, let me know.

And it generated the following Taskfile.yml. What is interesting to me is writing some Python to solve Elf Problems, not to write boilerplate task runner code. For that reason, it was nice to hand this part off to an LLM.

version: '3'

tasks:
  default:
    desc: Usage hint
    silent: true
    cmds:
      - 'echo "usage: task <day>  (e.g. task 1)"'

  '*':
    desc: Run solutions for a day, e.g. `task 1`
    silent: true
    env:
      DAY: '{{index .MATCH 0}}'
    cmds:
      - |
        dir=$(printf "%02d" $DAY)
        sample="$dir/sample_input.txt"
        input="$dir/input.txt"
        p1="$dir/part_one.py"
        p2="$dir/part_two.py"

        for f in "$p1" "$sample"; do
          [ -f "$f" ] || { echo "missing required file: $f" >&2; exit 1; }
        done

        run() {
          label="$1"; script="$2"; data="$3"
          result=$(uv run "$script" "$data") || { echo "  ✗ $label failed" >&2; exit 1; }
          printf "  %-22s %s\n" "$label" "$result"
        }

        echo "── Day $dir ──"
        run "Part One · Sample" "$p1" "$sample"
        [ -f "$input" ] && run "Part One · Input" "$p1" "$input"
        if [ -f "$p2" ]; then
          run "Part Two · Sample" "$p2" "$sample"
          [ -f "$input" ] && run "Part Two · Input" "$p2" "$input"
        fi
        true

I can then run this for my Day 1 solutions to get nicely formatted output:

❯ task 01
Alias tip: t 01
── Day 01 ──
  Part One · Sample      186276
  Part One · Input       51261734
  Part Two · Sample      193050
  Part Two · Input       317759715

I also got to learn about using wildcards with Taskfile by browsing through what Claude came up with.