No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-05 23:43:46 -05:00
Plugins 2015/03 2026-11-05 23:43:46 -05:00
Sources 2015/03 2026-11-05 23:43:46 -05:00
Tests 2015/03 2026-11-05 23:43:46 -05:00
.editorconfig Update for Swift6, Swift testing and add editor config (#6) 2024-11-30 15:56:38 -08:00
.gitignore Per-year modules with automatic day registration 2026-11-02 20:29:30 -05:00
CONTRIBUTING.md Advent of Code starter repo 2023-11-30 17:08:26 -08:00
LICENSE Advent of Code starter repo 2023-11-30 17:08:26 -08:00
Package.swift Make each Tests/YearNNNN directory its own test module 2026-11-03 19:16:23 -05:00
README.md Make each Tests/YearNNNN directory its own test module 2026-11-03 19:16:23 -05:00

Advent of Code Swift Starter Project

Language

Daily programming puzzles at Advent of Code, by Eric Wastl. This is a small example starter project for building Advent of Code solutions.

Usage

Swift comes with Xcode, or you can install it on a supported macOS, Linux, or Windows platform.

If you're using Xcode, you can open this project by choosing File / Open and select the parent directory.

If you prefer the command line, you can run the test suite with swift test, and run the output with swift run.

If you're using Visual Studio Code to edit, you might find these Swift extensions useful:

Challenges

Each year is its own module in Sources/YearNNNN, with its tests in the matching test module Tests/YearNNNN. A challenge is three files (replace 2015 and 00 with the year and day):

  • Sources/AOCCore/Data/2015/Day00.txt: the input data provided for the challenge
  • Sources/Year2015/Day00.swift: the code to solve the challenge
  • Tests/Year2015/Day00.swift: any unit tests that you want to include

To start a new day, copy the template Sources/Year2015/Day00.swift to Sources/YearNNNN/DayNN.swift and rename the type to DayNN. It must stay public, because the command line tool lives in another module. That is the whole step: on every build the RegisterDays plugin finds each Sources/YearNNNN/DayNN.swift (days 01 to 25) and registers its type. There is no list to edit. If the file does not declare a matching public struct DayNN, the build fails and the error names the file.

The test half needs no plugin at all: swift-testing discovers the @Test functions in a target's files by itself. Copy Tests/Year2015/Day00.swift to Tests/YearNNNN/DayNN.swift, rename the type to DayNNTests, and swift test picks it up. To run one year only: swift test --filter Year2015Tests.

To start a new year, create Sources/YearNNNN/ and Tests/YearNNNN/ with their first day files, then run swift package purge-cache before building. The same applies when you remove or rename a year directory. Adding, removing, or renaming day files never needs it.

Why year directories need purge-cache

Package.swift turns every Sources/YearNNNN directory into a module, and every Tests/YearNNNN directory into a test module. SwiftPM caches the evaluated manifest, keyed on the text of Package.swift, your environment variables, and the toolchain. A directory change is not part of that key, so the cached target list goes stale until you purge it. Touching Package.swift does not help, because its modification time is not part of the key either. Because the environment is part of the key, a different terminal or shell can be holding a different cached copy, which makes the problem look intermittent. It is not. Nushell, for example, passes LAST_EXIT_CODE to swift, so the previous command's exit status alone changes which copy is used.

If you forget to purge, one of two errors tells you:

  • After adding a year, the RegisterDays plugin stops the build:

    error: Sources/Year2026, Tests/Year2026 is not a target yet because SwiftPM
    is using a cached Package.swift. Run `swift package purge-cache` and build
    again.
    
  • After removing or renaming a year, SwiftPM fails before any plugin runs, with an error suggesting a path: setting (for a test directory, the target is Year2026Tests under Tests/Year2026). Your layout is fine; ignore the advice and run swift package purge-cache:

    error: 'swift-aoc': Source files for target Year2026 should be located under
    'Sources/Year2026', or a custom sources path can be set with the 'path'
    property in Package.swift
    

Inputs are personal, so Sources/AOCCore/Data/*/Day*.txt is gitignored. A day without its input file is skipped with a message rather than crashing.

Running

By default swift run runs the most recent day. Some examples:

$ swift run AdventOfCode 3                  # day 3 of the latest year that has it
$ swift run AdventOfCode 3 --year 2015      # day 3 of 2015
$ swift run AdventOfCode --all --year 2015  # every 2015 day
$ swift run AdventOfCode --all              # every day
$ swift run AdventOfCode --missing          # days without a solution, per year
$ swift run -c release AdventOfCode --benchmark 3

--missing knows that Advent of Code has had 12 days a year since 2025.

Linting and Formatting

Challenge source code can be linted and formatted automatically using the included dependency on swift-format.

Lint source code with the following command:

$ swift package lint-source-code

Format source code with the following command:

$ swift package format-source-code
Plugin ‘Format Source Code’ wants permission to write to the package directory.
Stated reason: “This command formats the Swift source files”.
Allow this plugin to write to the package directory? (yes/no)

To avoid the interactive prompt when formatting source code, use the --allow-writing-to-package-directory flag.

$ swift package format-source-code --allow-writing-to-package-directory

swift-format will use the built-in default style to lint and format code. A .swift-format configuration file can be used to customize the style used, see Configuration for more details.