Advent of Code 2024

Hi Swift Community!

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.

For those interested, we have a starter template to help you get going.

To participate with Swift:

  1. 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.
  2. Create an account at Advent of Code (required to use the leaderboard)
  3. Join the Swift community leaderboard to participate with others. Use ID 3315857-05237f97. Up to 200 players can join the leaderboard.
  4. 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.

Look forward to seeing you online!

24 Likes

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! :blush:

2 Likes

What's the correct etiquette ... Should you keep the GitHub repo you created using the template private or public during the Advent of Code event?

No, it's fine to keep the repo public. People share their solution code all the time.

However, you should not publish your puzzle inputs, the template mentioned above has those files in .gitignore.

2 Likes

However, you should not publish your puzzle inputs, the template mentioned above has those files in .gitignore .

I always forget that part. :sweat_smile:

Anyway, I joined as well. Have fun everyone getting those :star: :star:

My repo: GitHub - maartene/AoC2024: Advent of Code 2024

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.

1 Like

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.

Yay, AoC 2024! :tada:

My repo of Swift solutions: GitHub - tevelee/AdventOfCode

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!

In terms of other CLIs, I think I’ve seen others, but this one is the one that sticks in my mind.
https://github.com/scarvalhojr/aoc-cli

1 Like

Adding a new day item to the ⁠allChallenges array in the ⁠AdventOfCode.swift file would be a nice improvement as well.

1 Like

I've never done Advent of Code, but have always been curious. Seems a good opportunity to get more experienced with Swift.

I will also be joining, looking forward to another year :partying_face:. I have done all previous years as well using Swift.

Solutions for 2024 will be pushed to github and previous years can be found in the same repo.

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.