- Swift 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| Plugins | ||
| Sources | ||
| Tests | ||
| .editorconfig | ||
| .gitignore | ||
| CONTRIBUTING.md | ||
| LICENSE | ||
| Package.swift | ||
| README.md | ||
Advent of Code Swift Starter Project
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:
- Swift (provides core language edit / debug / test features)
- apple-swift-format (supports the swift-format package)
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 challengeSources/Year2015/Day00.swift: the code to solve the challengeTests/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
RegisterDaysplugin 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 isYear2026TestsunderTests/Year2026). Your layout is fine; ignore the advice and runswift 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.