I make the 1000 x 1000 array and fill it every slot with a *.
First time a claim is added, it gets a #
If a claim overlaps with another, those cells get marked with an X.
Part 1: Filter matrix for all X values.
Part 2: Check every coordinate for each claim. If every coordinate is a #, then that claim is the answer.
I did my part 1 similarly: I wrote the ID to each cell in it’s rectangle, unless the cell wasn’t zero, in which case I wrote -1. At the end, I counted the -1 values.
The second step was a little different. First of all, I added the ID to a set of IDs that were OK. Then I went through each cell of its rectangle. If the cell is zero, then update it to the current row’s ID. If it had a number in it, then that’s the ID of the most recent rectangle to overlap that cell; so remove the found ID and the current ID from the set of good IDs. Then set all the cells in the entire rectangle to its ID. At the end, the set of good IDs has a single element.
It sounds tricky, but it’s literally just a couple of lines of code
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.
Node.js
Reused my work for part 1 in part 2.
I make the 1000 x 1000 array and fill it every slot with a
*
.First time a claim is added, it gets a
#
If a claim overlaps with another, those cells get marked with an
X
.Part 1: Filter matrix for all
X
values.Part 2: Check every coordinate for each claim. If every coordinate is a
#
, then that claim is the answer.Main.js:
I did my part 1 similarly: I wrote the ID to each cell in it’s rectangle, unless the cell wasn’t zero, in which case I wrote -1. At the end, I counted the -1 values.
The second step was a little different. First of all, I added the ID to a set of IDs that were OK. Then I went through each cell of its rectangle. If the cell is zero, then update it to the current row’s ID. If it had a number in it, then that’s the ID of the most recent rectangle to overlap that cell; so remove the found ID and the current ID from the set of good IDs. Then set all the cells in the entire rectangle to its ID. At the end, the set of good IDs has a single element.
It sounds tricky, but it’s literally just a couple of lines of code