Solution (27 bytes): 10&Yb"@gUdtZStd0=A*w|4<*Avs
Note: the online demo is slightly modified since the online interpreter cannot accept input from a file for security reasons
The solution accepts the input as a file (defin which is the default input).
We then parse each line looking for “safe” levels one at a time based on the
rules of the challenge.
The detailed explanation is below:
% Implicitly load input as a file from 'defin'
10&Yb % Split the input on newlines (ASCII 10)
" % For each line of the input
% STACK: { '76421' }
@gU % Convert the line to a numeric array
% STACK: { [7, 6, 4, 2, 1] }
d % Compute the difference between successive elements
% STACK: { [-1, -2, -2, -1] }
t % Duplicate the top of the stack
% STACK: { [-1, -2, -2, -1], [-1, -2, -2, -1] }
ZS % Determine the sign of each element (-1 is negative, 1 is positive, 0 is 0)
% STACK: { [-1, -2, -2, -1], [-1, -1, -1, -1] }
t % Duplicate the top of the stack
% STACK: { [-1, -2, -2, -1], [-1, -1, -1, -1], [-1, -1, -1, -1] }
d % Compute the difference between successive elements
% STACK: { [-1, -2, -2, -1], [-1, -1, -1, -1], [0, 0, 0, 0] }
0=A % Check if all elements are equal to zero (they were all the same sign)
% STACK: { [-1, -2, -2, -1], [-1, -1, -1, -1], [1] }
* % Element-wise multiplication of the top two stack elements
% STACK: { [-1, -2, -2, -1], [-1, -1, -1, -1] }
w % Swap top two stack elements
% STACK: { [-1, -1, -1, -1], [-1, -2, -2, -1] }
|4< % Check if the absolute value of the differences are < 4
% STACK: { [-1, -1, -1, -1], [1, 1, 1, 1] }
*A % Element-wise product to check if both arrays are all non-zeros
% STACK: { [1] } (will be 0 if unsafe)
s % Vertically concatenate the entire stack and sum
% Implicit end of the FOR loop
% Implicitly display the result
Solution (43 bytes): 10&Yb"@gUttnqXNZ{h"@gdtZStd0=A*w|4<*?l.]]vs
Note: the online demo is slightly modified since the online interpreter cannot accept input from a file for security reasons
This solution is similar to the first solution except that we add an outer for
loop that creates all permutations of each row of the input with one element
missing, appends the original row and then applies the same rule check as in
part 1 in an inner for loop
% Implicitly load input as a file from 'defin'
10&Yb % Split the input on newlines (ASCII 10)
" % For each line of the input
@gU % Convert the line to a numeric array
tt % Make two copies of this line
nq % Determine the number of elements minus 1
XN % Determine all combinations of the input with 1 item missing
Z{h % Combine these combinations with the original line with all elements
" % For each combination...
@g % Treat the combination as an array
d % Compute the difference between successive elements
t % Duplicate the top of the stack
ZS % Determine the sign of each element (-1 is negative, 1 is positive, 0 is 0)
t % Duplicate the top of the stack
d0=A % Determine if all elements have the same sign
* % Multiply the result with the original diff (removes zero diffs)
w % Swap top two elements of the stack
|4< % Determine if all diffs are < 4
* % Multiply the results
? % If all values were non-zero in both checks
l % Push a 1 to the stack
. % Break out of the inner for loop (do not evaluate more combinations)
] % Explicit end of conditional
] % Explicit end of inner for loop
vs % Vertically concatenate the entire stack and sum to count safe reports so far
% Implicit end of outer for loops
% Implicitly print the result