# Advent of Code 2024

**URL:** <https://forums.swift.org/t/advent-of-code-2024/76301>\
**Category:** Community Showcase\
**Tags:** adventofcode\
**Created:** [November 30, 2024, 11:46pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301 "2024-11-30T23:46:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![timsneath](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/timsneath/32/31180_2.png) [@timsneath](https://forums.swift.org/u/timsneath)\
**Post date:** [November 30, 2024, 11:46pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/1 "2024-11-30T23:46:35Z")

</div>

Hi Swift Community!

It's that time of year when some of us have a little _ **Swift coding challenge fun** _!

Every December, [Eric Wastl](http://was.tl) publishes [Advent of Code](https://adventofcode.com/), 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](https://adventofcode.com/2023/day/1) 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](https://github.com/apple/swift-aoc-starter-example).

To participate with Swift:

1. Clone [the template](https://github.com/apple/swift-aoc-starter-example) (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](https://adventofcode.com/2024/auth/login) at Advent of Code (required to use the leaderboard)
3. [Join the Swift community leaderboard](https://adventofcode.com/2024/leaderboard/private) 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!

---

<div class="post-metadata">

**Author:** ![tomassliz](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tomassliz/32/45668_2.png) [@tomassliz](https://forums.swift.org/u/tomassliz)\
**Post date:** [December 1, 2024, 1:08am UTC](https://forums.swift.org/t/advent-of-code-2024/76301/2 "2024-12-01T01:08:54Z")

</div>

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! 😊

---

<div class="post-metadata">

**Author:** ![softinio](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/softinio/32/20989_2.png) [@softinio](https://forums.swift.org/u/softinio)\
**Post date:** [December 1, 2024, 3:47am UTC](https://forums.swift.org/t/advent-of-code-2024/76301/3 "2024-12-01T03:47:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![gereons](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gereons/32/38393_2.png) [@gereons](https://forums.swift.org/u/gereons)\
**Post date:** [December 1, 2024, 7:36am UTC](https://forums.swift.org/t/advent-of-code-2024/76301/4 "2024-12-01T07:36:11Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![maartene](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/maartene/32/42076_2.png) [@maartene](https://forums.swift.org/u/maartene)\
**Post date:** [December 1, 2024, 10:40am UTC](https://forums.swift.org/t/advent-of-code-2024/76301/5 "2024-12-01T10:40:14Z")

</div>

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

I always forget that part. 😅

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

My repo: [GitHub - maartene/AoC2024: Advent of Code 2024](https://github.com/maartene/AoC2024/)

---

<div class="post-metadata">

**Author:** ![BruceMcRooster](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/brucemcrooster/32/37408_2.png) [@BruceMcRooster](https://forums.swift.org/u/BruceMcRooster)\
**Post date:** [December 1, 2024, 3:51pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/6 "2024-12-01T15:51:49Z")

</div>

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](https://github.com/BruceMcRooster/aoc-2024/blob/64e0c79c38891cf9c181ae385ced21f68271c154/GetDay) (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.

---

<div class="post-metadata">

**Author:** ![vonox7](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vonox7/32/33363_2.png) [@vonox7](https://forums.swift.org/u/vonox7)\
**Post date:** [December 1, 2024, 4:30pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/7 "2024-12-01T16:30:04Z")

</div>

Have you wrapped up today’s Advent of Code?  
If you’re up for a fun challenge, check out [Golfcoder](https://golfcoder.org/2024), 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!

---

<div class="post-metadata">

**Author:** ![BruceMcRooster](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/brucemcrooster/32/37408_2.png) [@BruceMcRooster](https://forums.swift.org/u/BruceMcRooster)\
**Post date:** [December 1, 2024, 7:45pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/8 "2024-12-01T19:45:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tevelee](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tevelee/32/9775_2.png) [@tevelee](https://forums.swift.org/u/tevelee)\
**Post date:** [December 2, 2024, 10:27am UTC](https://forums.swift.org/t/advent-of-code-2024/76301/9 "2024-12-02T10:27:50Z")

</div>

Yay, AoC 2024! 🎉

My repo of Swift solutions: [GitHub - tevelee/AdventOfCode](https://github.com/tevelee/AdventOfCode)

---

<div class="post-metadata">

**Author:** ![tomassliz](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tomassliz/32/45668_2.png) [@tomassliz](https://forums.swift.org/u/tomassliz)\
**Post date:** [December 2, 2024, 10:50pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/10 "2024-12-02T22:50:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BruceMcRooster](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/brucemcrooster/32/37408_2.png) [@BruceMcRooster](https://forums.swift.org/u/BruceMcRooster)\
**Post date:** [December 2, 2024, 10:58pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/11 "2024-12-02T22:58:45Z")

</div>

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](https://www.reddit.com/r/adventofcode/wiki/faqs/automation)), but you can find the updated script in my [main branch](https://github.com/BruceMcRooster/aoc-2024/blob/main/GetDay).  
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](https://github.com/scarvalhojr/aoc-cli)

---

<div class="post-metadata">

**Author:** ![tomassliz](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tomassliz/32/45668_2.png) [@tomassliz](https://forums.swift.org/u/tomassliz)\
**Post date:** [December 3, 2024, 12:39am UTC](https://forums.swift.org/t/advent-of-code-2024/76301/12 "2024-12-03T00:39:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BlueSparrow](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bluesparrow/32/38500_2.png) [@BlueSparrow](https://forums.swift.org/u/BlueSparrow)\
**Post date:** [December 3, 2024, 12:43am UTC](https://forums.swift.org/t/advent-of-code-2024/76301/13 "2024-12-03T00:43:26Z")

</div>

> **[GitHub - bhelyer/AOC2024: Advent of Code 2024](https://github.com/bhelyer/AOC2024)**
>
> Advent of Code 2024

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

---

<div class="post-metadata">

**Author:** ![rbruinier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rbruinier/32/19556_2.png) [@rbruinier](https://forums.swift.org/u/rbruinier)\
**Post date:** [December 3, 2024, 8:31am UTC](https://forums.swift.org/t/advent-of-code-2024/76301/14 "2024-12-03T08:31:39Z")

</div>

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

Solutions for 2024 will be pushed to [github](https://github.com/rbruinier/AdventOfCode/tree/main/Sources/Solutions/2024/Days) and previous years can be found in the same repo.

---

<div class="post-metadata">

**Author:** ![BruceMcRooster](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/brucemcrooster/32/37408_2.png) [@BruceMcRooster](https://forums.swift.org/u/BruceMcRooster)\
**Post date:** [December 3, 2024, 1:23pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/15 "2024-12-03T13:23:22Z")

</div>

I just had a chance to do this: [GetDay](https://github.com/BruceMcRooster/aoc-2024/blob/main/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`.

---

<div class="post-metadata">

**Author:** ![Andropov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andropov/32/35955_2.png) [@Andropov](https://forums.swift.org/u/Andropov)\
**Post date:** [December 3, 2024, 4:04pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/16 "2024-12-03T16:04:20Z")

</div>

Here's mine: [GitHub - Androp0v/AdventOfCode2024: Advent of Code 2024](https://github.com/Androp0v/AdventOfCode2024)

First year doing Advent of Code, let's see if I can keep it up more than 3 days 😅

---

<div class="post-metadata">

**Author:** ![carlynorama](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlynorama/32/27072_2.png) [@carlynorama](https://forums.swift.org/u/carlynorama)\
**Post date:** [December 3, 2024, 6:34pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/17 "2024-12-03T18:34:41Z")

</div>

Thanks for organizing this!

I have a 2025 task of understanding build systems in the Swift ecosystem better, so I'm using AoC to play around with that!

Each day is its own compilable folder, and the build scripts/set up will get fancier as the needs of each day get harder? That's the idea anyway. Ha! Used pkl for the "config"! All very wip. Keeps the data files very very separate. Still using an actual bash file for the compile script for now.

The CLI which uses package manager like a sensible person: [GitHub - carlynorama/AoCScaffold](https://github.com/carlynorama/AoCScaffold)  
Public folders: [https://github.com/carlynorama/AdventOfCode/2024](https://github.com/carlynorama/AdventOfCode/2024)

---

<div class="post-metadata">

**Author:** ![BlueSparrow](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bluesparrow/32/38500_2.png) [@BlueSparrow](https://forums.swift.org/u/BlueSparrow)\
**Post date:** [December 4, 2024, 7:35pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/18 "2024-12-04T19:35:44Z")

</div>

Day 4 was fun. My first lesson: I need to bone up on generics. I made an extension to count the number of matches ([my day4 code](https://github.com/bhelyer/AOC2024/blob/main/Sources/Programs/Day4.swift)) from a regex over a `String` or `Substring`.

I couldn't quite figure out how to get it to play nice with `StringProtocol` so I ended up cave-manning it:

```swift
extension Regex<Substring> {
    func count(in string: Substring) -> Int {
        // ...snip...
    }
    // Ctrl+C, Ctrl+V...
    func count(in string: String) -> Int {
        // ...snip...
    }
}

```

In retrospect even if I didn't have time to go down the 'really understanding `any` and `some`' rabbit hole, I should've just made the horizontal list a `[String]` too.

---

<div class="post-metadata">

**Author:** ![Andropov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andropov/32/35955_2.png) [@Andropov](https://forums.swift.org/u/Andropov)\
**Post date:** [December 4, 2024, 8:13pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/19 "2024-12-04T20:13:39Z")

</div>

I started off with a terrible approach for Day 4 part 1, which ended up involving complicated bound checks and repetitive code which had terrible performance.

On the flip side, I was able to throw in a few quick `async let` that made the code ~5x faster with zero effort. Worked great in a pinch. Basically this:

```swift
func part1() async -> Any {
     async let horizontal = searchHorizontal(backwards: false)
     async let horizontalB = searchHorizontal(backwards: true)
     async let vertical = searchVertical(backwards: false)
     // ...
    return await horizontal
            + horizontalB
            + vertical
            // ...
}

```

Not a substitute for a better algorithm, but hey, I'm happy with how easy parallelizing this code was!

* * *

Later I was able to code the part 2 solution in just a couple lines of code, which is why I think there was a much easier solution for part 1 than what I went with 😅

> **In case you're curious about my part 2**
>
> ```swift
> func part2() -> Any {
> var matchCount = 0
> for j in 0..<(numberOfLines - 2) {
> for i in 0..<(lineLength - 2) {
> let diagonalA = String(lines[j][i]) + String(lines[j + 1][i + 1]) + String(lines[j + 2][i + 2])
> let diagonalB = String(lines[j][i + 2]) + String(lines[j + 1][i + 1]) + String(lines[j + 2][i])
> guard diagonalA == "MAS" || diagonalA == "SAM" else {
> continue
> }
> guard diagonalB == "MAS" || diagonalB == "SAM" else {
> continue
> }
> matchCount += 1
> }
> }
> return matchCount
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [December 4, 2024, 8:13pm UTC](https://forums.swift.org/t/advent-of-code-2024/76301/20 "2024-12-04T20:13:44Z")

</div>

FYI, if you don't need to allow for overlapping matches of a pattern you can use `stringOrSubstring.matches(of: pattern).count`.

[Next page](https://forums.swift.org/t/advent-of-code-2024/76301.md?page=2)
