Day 13 does not exist. we do not speak of it. (at some point I'll circle back around, but that point is not now. if you want a nice answer, check out my friend Dave's solution here: (https://github.com/davedelong/AOC/blob/main/Sources/AOC2022/Day%2013/2022-Day13.swift)
Day 14, however, DOES exist, and I blasted through it in just slightly under 1:10! :) I did hit an interesting swift-specific thing, which I'll share below the fold, along w/ the bits I thought were most interesting/relevant/etc. It was a very long day, and I am very tired for it being 10 PM so, forgive me in advance.
Ok, so, I'm writing/coding/working on an M1 Macbook Air running Monterey. (aka macOS 12.6). (this is relevant)
So, I went to write my parsing code, and the inputs are sets of coordinates, separated by the string " -> ", and I wanted to pull those out to get just the coordinates. This is pretty basic input parsing, and thankfully there's a method on that! it's actually on StringProtocol, and it looks like this:
func split(separator: Self.Element, maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true) -> [Self.SubSequence]
and I have been using it pretty consistently all month. Today, however, I went to use it like so:
line.split(separator: " -> ")
and the nice compiler informed me that that api required macOS 13, and I was set up to support 12.6, so I needed to gate it with an #available or similar. So, I looked at the API again. And again. and it kept saying "requires macOS 10.10+".
I asked the dev chat I frequent if anyone had any ideas, and my day-job-boss suggested a completely different api (which I have feelings about the divergent names, but another day), components(separatedBy:), but the argument for that is a CharacterSet, ie: please split on any of these single characters.
And that's what triggered my brain to actually READ that method declaration again. It's defined on StringProtocol, like I said, so it doesn't talk about what a Self is or a Self.Element, but on String, Self is obviously the String struct, and Element is the type of thing they're built out of... Characters. Which, fuuuck. -> is absolutely NOT a Character.
Turns out, there's a NEW split api in macOS 13 that takes a String as the separator, and that's the one I was invoking. Oops!
My vengeful solution:
var coords = line.split(separator: " ")
coords.removeAll { substr in
substr == "->"
}
The actual solution was pretty simple. I'm proud of my Room class to wrap it all together, and my re-use and extension of Coord was pretty slick:
extension Coord {
func coordBelow() -> Coord {
return Coord(x: x, y: y + 1)
}
func coordDownLeft() -> Coord {
return Coord(x: x - 1, y: y + 1)
}
func coordDownRight() -> Coord {
return Coord(x: x + 1, y: y + 1)
}
}
The one gotcha I found was that I assumed my coordinate pairs were in increasing order, and that was Not True, so I had to check to make sure my ClosedRanges were increasing, as the Swift Gods decree. :)
you can see the rest here: (https://github.com/nothes/AdventOfCode/blob/main/AoC/AoC/Day%2014/day14.swift)