Solutions to the 2021 edition of Advent of Code in Swift.
Spent some time figuring out a good way to do this in Swift without having to manage a bunch of Xcode projects/targets and still have the ability to use external dependencies using the Swift Package Manager.
I chose the excellent swift-sh to make it easy to run/compile standalone scripts with external dependencies (such as swift-algorithms).
Used a couple of helpful functions from swift-algorithms to massage and process the data without writing a bunch of boilerplate.
Fairly straightforward, started with a quick 'n dirty solution, later changed it to use a bit more functional approach using map/reduce and avoiding local state/side effects.
A broken dishwasher limited the time I had availble to spend on this, hence the copiuous duplication in part 2.
Trickiest part was to figure out the correct halting condition in part 2. Much love for Swift's typealias
so I don't
have to mentally deal with types like [[[Int]]]
.
TIL: signum()
is awesome. In the end part 2 is actually cleaner than part 1. I could have just used that with the
additional constraint of filtering out diagonals for part 1.
Did the first part the naive way, which of course didn't work for part 2. My new favorite Swift toy is for sure
myDict[myKey, default: 0] += 1
where you don't have to worry about dealing with nil
s.
For part 1 the median sufficed, but for part 2 I (initially) used the average, but the rounding was off such that the sample input would fail, but the actual puzzle input would pass. Because of the limited input size in the end I calculated the fuel cost for all position between the first and the last crab, which ran quick enough. TIL: Triangular numbers.
Good fun, I was already expecting part 2 to require decoding the numbers, but did solve part 1 the simple
way, by simply counting string lengths. Part 2 required writing down the patterns, but then it was fairly
easy by process of elimination. Not too happy with the inout
mutable state and force unwraps, but it works.
Nice to see a MS Paint style floodfill in action.
Push and pop all the way.
Part 2 surprisingly easy after part 1. Only difference is not halting after 100 steps, but when the grid is all zeros.
Did not enjoy this, mainly because I find recursion problems not really interesting. Implemented a DFS for this.
Bit of repetition, but couldn't be bothered to clean it up.
reduce(into:_:)
all the things! (in part 2)