Hey so I got stuck on Day 5 for a little bit because my code hung on the part 2 data and I spent a long time thinking my code was inefficient, but it was actually a weird hang!
I wrote an extension on ClosedRange<Int>
which contained the code
guard !other.contains(self) else {
print("other contains source entirely. Nothing left over.")
return []
}
Which I expected other.contains(self)
would return true if self
was fully inside or==
to other
but I didn't put it to a hard test and it worked fine on the sample data.
When I loaded the real data it just freezes entirely. Recreated here with my very verbose extension that got really long as I tried to find the problem.
//This one works fine.
let resultC = (2442...2679).crop(by:2321...2446)
print(resultC)
print(Int.max)
//This one Hangs.
let resultD = (2442763622...2679835230).crop(by:2321931404...2446354471)
//let result = (0...9).crop(by:3...8)
print(resultD)
I wrote my own fullyContains
, every thing worked fine.
I feel like there must be something obvious about those numbers or how Range.contains() works or both that makes the problem obvious and I'd love to be pointed in the right direction since I will likely be using Range
again!.
Thanks!