3 ms·
I was playing along with Advent of Code and decided to do some of the puzzles, mainly ones where having a graphical view of what's going on would be really nice
by Mister_Snuggles 8y ago
I was playing along with Advent of Code and decided to do some of the puzzles, mainly ones where having a graphical view of what's going on would be really nice, in Swift. For reference, the other ones were done in D.
My input parsing code was inevitably a mess of stuff like:
let parts = l.components(separatedBy: ",")
let first = parts[0].components(separatedBy: "=")
let second = parts[1].trimmingCharacters(in: .whitespaces).components(separatedBy: "=")
When I did puzzles in D my input parsing was just creating a regex, matching on it, and pulling out the results.
I started to look at how to do regex in Swift and it seemed easier (!) to just use .components(separatedBy:) as required. Google came up with a number of people who have created libraries/extensions that make this easier, but Swift really needs some better regex handling built in.
- meggar 8y agoThere's also split() let parts = l.split(separator: ",").map{ $0.split(separator: "=") }, first = parts[0], second = parts[1]
- msangi 8y agoWhile I agree that String parsing in Swift is quite annoying, when I started using Scanner my Advent of Code parsing experience improved https://developer.apple.com/documentation/foundation/scanner https://developer.apple.com/documentation/foundation/scanner