Oh SO MUCH BETTER than yesterday!
Map, Filter, Reduce for the win! ;)
code/spoilers under the cut.
Most of the challenge today was in correctly parsing the file-system (or at least as much as your input shows you!), and building a data structure to store it, plus "do you remember your recursion unit in your Algorithms class?" pop quiz. :)
The most interesting bit, for me, was getting "sly" with my filtering and sorting. I tried to get reduce to work, but honestly, it was 10:30 PM, and I was too tired to work out wtf arguments it was handing me / expecting back.
I'm pretty proud of my part 2 solution:
func dirsBiggerThan(_ targetSize: Int) -> [DiskObj] {
var bigEnoughDirs = Array(containedObjects.filter { (_, obj) in obj.dir == true }.filter { (_, obj) in obj.size >= targetSize }.values)
for file in self.containedObjects {
bigEnoughDirs.append(contentsOf: file.1.dirsBiggerThan(targetSize))
}
return bigEnoughDirs
}
// returns the size of that directory
func smallestDirLargerThan(_ targetSize: Int) -> Int {
return self.dirsBiggerThan(targetSize).sorted { (tuple1, tuple2) in return tuple1.size < tuple2.size }[0].size
}
I feel dirty about using the tuple reference numbers (see file.1 which grabs the 2nd item in the file tuple (the actual DiskObj), but I'll get over it.
The gotcha for me was that when you're mapping and filtering over a dictionary, instead of passing you a value at a time, it passes you a tuple (key, value), which in retrospect is perfectly logical, but I was not expecting at first, so the type checking system spent a bit of time shaking it's finger at me.
..I think, actually, that's probably what led me astray with reduce, that it was handing me a tuple and not a DiskObj, and I wasn't understanding that. heh. oops!
(https://github.com/nothes/AdventOfCode/blob/main/AoC/AoC/Day%207/day7.swift)