I really wanted to do Part 1 geometrically by splitting and merging claims into non-overlapping regions. It would be O(N²) however, and I was short of actual time to build it so went for the big map of positions like many others:
data classClaim(valid:Int,valx:IntRange,valy:IntRange){constructor(id:Int,origin:Origin,size:Size):this(id,(origin.left..origin.left+size.width-1),(origin.top..origin.top+size.height-1))funpositions():Sequence<Pos>=x.asSequence().flatMap{x->y.asSequence().map{y->Pos(x,y)}}}
Part 2
Fairly simple extension. The tricky bit was remembering to remove both overlapping claims from the candidate result set:
funpart2(claims:List<Claim>):Set<Int>{valmaterial=mutableMapOf<Pos,Int>()valnonOverlapping:MutableSet<Int>=claims.map{c->c.id}.toMutableSet()claims.forEach{claim->claim.positions().forEach{pos->valclaimed=material[pos]if(claimed==null){// unclaimed position, now claimedmaterial[pos]=claim.id}else{// overlapping position, remove both claims from result setnonOverlapping-=setOf(claimed,claim.id)}}}returnnonOverlapping}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Ah, regexes and string splitting. The One True Way to parse text is with parser combinators.
Part 1
I really wanted to do Part 1 geometrically by splitting and merging claims into non-overlapping regions. It would be O(N²) however, and I was short of actual time to build it so went for the big map of positions like many others:
That uses a modified
Claim
class as follows:Part 2
Fairly simple extension. The tricky bit was remembering to remove both overlapping claims from the candidate result set: