l=W.length
k=max(0,i-W[i])
L=[1...n]
g(n)=L[L0+n>0]0
f(H,W)=0^{0^{[max(∑_{i=1}^ljoin(g(k),g(W[i]+i-1-k)+1,g(l))[1...l]0^{(h-H[i])^2})forh=H[1...]].max-1}}
My attempt at solving this challenge without looking at the other answers lolol. You can see how terribly that came out just by how long the answer is.
The function f
takes in the height list first and the width list second, and returns 0
for no collisions, 1
if there is a collision.
Also the H[1...]
can't be golfed to H
(I checked), because apparently H
is recognized as a number, not a list, so the list comprehension errors out. H[1...]
circumvents this error by forcing Desmos to recognize H
as a list.
Try It On Desmos!
Try It On Desmos! - Prettified
I have added an explanation of my code below because I'm bored :P.
Explanation
The general rundown of my strategy is for each height, construct lists of 1
's and 0
's which correspond to the spaces that each mushroom takes up for that particular height. Then, all these lists are summed together. If there is a collision at that height, then there will be a number \$>1\$ in the list.
Now, to explain each part of the code.
L=[1...n]
g(n)=L[L0+n>0]0
g
is a function that will return a list of n
zeroes.
f(H,W)=0^{[max(∑_{i=1}^ljoin(g(k),g(W[i]+i-1-k)+1,g(l))[1...l]0^{(h-H[i])^2})forh=H[1...]].max-1}
f
is the function that returns the answer. Let's break it down:
... [ ... forh=H[1...]]
For h
in list H
(looping over all heights)...
... ∑_{i=1}^l ...
Take the summation from i=1
to the length of list W
of (looping over indices of the width list)...
join(g(k),g(W[i]+i-1-k)+1,g(l))[1...l]
A list of 1
's and 0
's, representing the spaces that the mushroom at index i
in the width list takes up, with 1
representing the space taken up by the mushroom. This list is constructed by joining three individual lists into one list: g(k)
, g(W[i]+i-1-k)+1
, and g(l)
.
The first list, g(k)
, consists of the 0
's before the range of 1
's which represent the mushroom. The number of 0
's needed can be calculated by subtracting the width of the mushroom W[i]
from its index i
(The mushroom extends W[i]-1
spaces to the left from the center space, so i-W[i]
is essentially calculating the index of the last 0
right before the mushroom.). The issue with this is that some mushrooms extend past the front of the list, which will make i-W[i]
negative. While this is no problem for g
(it returns an empty list for negative inputs), it may cause issues for length calculations later on (i.e. the list of 1
's right after), so instead we store the length of the list as k=max(0,i-W[i])
, and call g(k)
to construct the list of 0
's.
The second list, g(W[i]+i-1-k)+1
, constructs a list of 1
's which represent the space that the mushroom takes up. g
returns a list of 0
's, so we convert that to a list of 1
's by adding 1 to the list. At first, it might be obvious to do g(2W[i]-1)+1
as each mushroom takes up \$2w-1\$ spaces. But this runs into the problem of width 0, as g(-1)+1
is just an empty list, but what we actually wanted was the list [0]
. Also, the bigger problem is that it doesn't account for mushrooms extending past the front of the list, and will have too many 1
's for those mushrooms. To counteract this problem, note that the length of the list up to the end of the mushroom has to be i+W[i]-1
(the mushroom extends W[i]-1
spaces to the right of its center, i
, so the index of the last 1
in the mushroom is i+W[i]-1
). The first list already accounted for k
of the elements, so the second list will have a length of i+W[i]-1-k
to account for the rest of the elements.
The last list, g(l)
, is simply to pad the end of the list with enough 0
's to make the list have a length of at least l=W.length
for the slice (explained below).
[1...l]
is a slice that takes the first l
elements of the list. This is to ensure that all the lists being summed have the same length, as Desmos gives an error if they aren't all the same length.
0^{(h-H[i])^2}
This checks if the height of the mushroom at index i
is the same as the current height h
(if we don't have this code, then the function would detect for collisions as if they were all on the same height). (h-H[i])^2
returns 0
if the current height h
is the same as the mushroom's height H[i]
. Otherwise, it returns a positive integer. Then, 0^{(h-H[i])^2}
returns 1
if (h-H[i])^2
is 0
, and 0
if (h-H[i])^2
is a positive integer.
Putting that all together, 0^{(h-H[i])^2}
returns 1
if h
equals H[i]
, and returns 0
otherwise. When multiplying this value with the list, if it is 1
, then nothing happens to the list, but if it is 0
, then all the entries in the list become 0
, which essentially means that the list won't contribute to the summation at all. This causes any mushroom that isn't at the current height of the loop to be nullified, leaving only the mushrooms which are at the current height to contribute to the summation.
0^{0^{[max( ... ) ... ].max-1}}
Recall that if the summed list contains any number \$>1\$, then that means there is a collision. If there are any numbers \$>1\$, then the max
of the summed list will be \$>1\$. The list comprehension will create a list of maximums for each height in H
. By taking the maximum of the list comprehension, we can then detect if there were any collisions in any of the heights if the maximum is \$>1\$. We can do this by first subtracting one from the maximum, and then wrapping 0^{0^{ ... }}
around it. 0^{0^n}
for any integer n
returns 1
if n
is positive, and 0
if n
is 0
or negative. This means if the maximum is 0
or 1
, the function will return 0
, because both 0^{0^{-1}}
and 0^{0^0}
return 0
. If the maximum is 2
or above, then the function will return 1
.