ShareMyScreen/AdventOfCode/2022/04/CampCleanup
I will read in the values line by line, but this time there is no need to box them, since they all have 4 numbers. To simplify processing I will change the '-' characters to ',' and execute the resulting sentences.
] lines =. ".;._2 '-,' stringreplace LF ,~ wd 'clippaste' 2 4 6 8 2 3 4 5 5 7 7 9 2 8 3 7 6 6 4 6 2 6 4 8
One interval contains the other if its low bound is the lower of the two low bounds and its high bound is the higher of the two high bounds. I want to check that on a few examples. I will write the verb to operate on a shape-(2,2) array containing 2 intervals:
1 7 <.`>."0 (3 5) NB. (1 <. 3) , (7 >. 5) () required to separate numeric words 1 7 <.`>."0/ 2 2 $ 1 7 3 5 1 7 <.`>."0/ 2 2 $ 1 5 3 7 1 7
<.`>."0 says 'apply <. between the first pair of atoms and >. between the second pair'; that is create (smaller low bound,larger high bound). / says 'apply that verb between the two items of the argument'.
hullisitem =. (e.~ <.`>."0/)"2 NB. (x e.~ <.`>."0/ y), applied on 2-cells hullisitem 2 2 $ 1 7 3 5 1 hullisitem 2 2 $ 1 5 3 7 0
answering the question 'is the (low,high) combination one of the items of the argument?' As a matter of good form I add "2 to indicate that the answer will make sense only when the argument is a table.
Now all I have to do is organize the data into an nx2x2 brick, which I do by reshaping each line into a 2x2 table. Then I count how many times the hullisitem condition holds:
+/ hullisitem 2 2 $"1 lines 2
This took a lot longer to explain than to write. J uses the Iverson bracket, that is, boolean true is the same as the number 1, allowing arithmetic like the above.
Part 2 looks for simple overlap rather than inclusion. I've had that problem before! Order the intervals by their starting value, and if the ending value of the first to start is not lower than the starting value of the second to start, they overlap. Applied to a 2x2 table this is
overlap =. ([: >:/ (0 1;1 0) { /:~)"2 overlap 2 2 $ 1 5 3 7 1 overlap 2 2 $ 5 7 1 3 0 overlap 2 2 $ 4 6 1 4 1
Reading right to left, I ordered the intervals, then extracted (TR,BL) and finally compared TR >: BL which indicates overlap.
+/ overlap 2 2 $"1 lines 4
Revision
I could have written overlap as
overlap =: (>:/) @: ((0 1;1 0)&{) @: (/:~)"2
([: u v) is equivalent to u@:v but it doesn't need as many parentheses. The choice is a matter of taste.