It's that time of year when some of us have a little Swift coding challenge fun!
Every December, Eric Wastl publishes Advent of Code, a fun set of coding exercises. One challenge is posted each day from December 1st to December 25th. The challenges start out easy and increase in difficulty. Here’s the first exercise from last year for example.
You can use whatever programming language and techniques you like to complete the exercises, but of course, we use Swift! Participating is a great way to sharpen your Swift skills and have a little fun with the community.
Clone the template (optional)
a) If you’re using Xcode, you can open this template by selecting File > Open and navigating to its base directory.
b) If you’re using the Swift CLI, change directory to this template and type swift run to run your code or swift test to run your tests.
Create an account at Advent of Code (required to use the leaderboard)
Every day (or as often as you prefer), attempt the challenge using Swift!
The leaderboard will automatically update to show who has completed the challenge and provide a score based on how long it took you. You can always ignore the score of course - it’s just for fun!
We'd encourage you to include a link to your GitHub account where you can post your solution. It’s really neat to see how everyone attempts each day’s challenge: we learn new things about how others use the Swift language, and you can pick up some great tips and techniques from studying others' work.
I’d recommend using the "Use this template" button on GitHub instead of cloning the template directly. It’s just a small tip for anyone who might be new to working with project templates on GitHub!
If anyone feels lazy, I whipped up a quick script using Swift shell scripting to download and create all the necessary files for a given day.
It's not super robust, and I know there are other options written in other languages that are far better. You can find it here (this is in my AoC repo, so don't go snooping if you don't want spoilers).
You can copy the file and chmod +x it to get started.
Just call ./GetDay <day_num>.
It will prompt you for a cookie (use a browser inspector and copy the session data from AoC when logged in).
It then creates all the necessary files for this template (Source/Data/Day00.txt, Source/Day00.swift, Tests/Day00.swift), downloading that day's input file from the website.
Have you wrapped up today’s Advent of Code?
If you’re up for a fun challenge, check out Golfcoder, an open-source project where you can see how compact your Swift solution is for today’s puzzle.
Since Swift is a very concise language, you might have a good chance being top spot with a Swift solution. Feedback is welcome!
Darnit, you got me to spend way too much time on this. Cool app, though. Very smooth.
For anyone trying this, I don't believe it uses Swift 6 for compilation, so count(where:) won't work. Just something to remember. Don't ask how I figured that one out.
Thanks, It’s a great helper! I just tweaked it a little because I dislike having to fill in the browser session each time. Now it’s saved in the .advent-of-code file.
Can you share the "far better" scripts you were talking about?
Glad you found it useful!
I did check their automation docs and changed the code a little. The creator wants the User-Agent of automated requests to include contact info and a link (the full rules are here), but you can find the updated script in my main branch.
I’ve also been thinking about saving the cookie somewhere instead of digging it up from my clipboard history, so you beat me to it!
I just had a chance to do this: GetDay now adds to your all challenges array (and respects whether you like trailing commas, by accident), and it can save and load your session cookie from the .advent-of-code file you already made. Just remember to add that .advent-of-code file to your .gitignore.