Given a rectangular matrix of elevations, draw its contours.
Task
Two elements x
and y
are on the same contour level if floor(x/10) == floor(y/10)
. For example, 52
and 58
are on the same contour level, but 58
and 64
are not.
The act of drawing contours is defined as follows:
For every element e
, replace it with a two-char string chosen as follows:
- the first character is
" "
if the element belowe
is on the same contour level ase
or if there is no element to the belowe
and"_"
otherwise - the second character is
" "
if the element to the right ofe
is on the same contour level ase
or there is no element to the right ofe
and"|"
otherwise
The elements within rows are joined together, then the rows are joined together with newlines.
Example
Let's say the input is [[5,20],[3,6]]
, visualized as
5 20
3 6
We first look at 5
. Since 3
is on the same contour level as 5
, the first character is " "
. Since 20
is not on the same contour level as 5
, the second character is "|"
.
Now we look at 20
. Since 6
is not on the same contour level as 20
, the first character is "_"
. Since there is no element to the right of 20
, the second character is " "
.
Now we look at 3
. Since there is no element below 3
, the first character is " "
. Since 6
is on the same contour level as 3
, the second character is " "
.
Now we look at 6
. Since there is no element below 6
, the first character is " "
. Since there is no element to the right of 6
, the second character is " "
.
Based on these two-char strings, we make replacements to get [[" |","_ "],[" "," "]]
. Joining these together, we get an output of
|_
Rules
- The input matrix will always be rectangular and composed of positive integers.
- Trailing spaces or newlines may be in any amount (including 0) and do not have to be consistent in any way.
- You do not have to follow the same algorithm as long as you produce the same results.
- Your program or function may output a newline-separated string, list of strings, or equivalent.
- This is code-golf, so shortest code in bytes wins.
Test Cases
input
output
[[1,5,8,9],[3,11,13,8],[7,14,10,9],[4,8,7,6]]
_ _
| |
|_ _|
[[0,10,20,30,40,50,60,70,80,90],[0,0,10,10,20,20,30,30,40,40],[0,0,0,10,10,10,20,20,20,30],[0,0,0,0,10,10,10,10,20,20],[0,0,0,0,0,10,10,10,10,10],[0,0,0,0,0,0,10,10,10,10],[0,0,0,0,0,0,0,10,10,10],[0,0,0,0,0,0,0,0,10,10],[0,0,0,0,0,0,0,0,0,10],[0,0,0,0,0,0,0,0,0,0]]
|_|_|_|_|_|_|_|_|_
|_ |_ _|_ _|_ _
|_ |_ _ |_
|_ |_ _
|_
|_
|_
|_
|_
[[5,5,5,5,5,5,5,5,5,5,5],[5,10,10,10,10,10,10,10,10,10,5],[5,10,15,15,15,15,15,15,15,10,5],[5,10,15,20,20,20,20,20,15,10,5],[5,10,15,20,25,25,25,20,15,10,5],[5,10,15,20,25,30,25,20,15,10,5],[5,10,15,20,25,25,25,20,15,10,5],[5,10,15,20,20,20,20,20,15,10,5],[5,10,15,15,15,15,15,15,15,10,5],[5,10,10,10,10,10,10,10,10,10,5],[5,5,5,5,5,5,5,5,5,5,5]]
_ _ _ _ _ _ _ _ _
| |
| _ _ _ _ _ |
| | | |
| | _ | |
| | |_| | |
| | | |
| |_ _ _ _ _| |
| |
|_ _ _ _ _ _ _ _ _|
[[35,32,29,26,25,25,25,26,29,32,35],[32,28,25,22,20,20,20,22,25,28,32],[29,25,21,18,15,15,15,18,21,25,29],[26,22,18,14,11,10,11,14,18,22,26],[25,20,15,11,7,5,7,11,15,20,25],[25,20,15,10,5,0,5,10,15,20,25],[25,20,15,11,7,5,7,11,15,20,25],[26,22,18,14,11,10,11,14,18,22,26],[29,25,21,18,15,15,15,18,21,25,29],[32,28,25,22,20,20,20,22,25,28,32],[35,32,29,26,25,25,25,26,29,32,35]]
_| |_
_| _ _ _ _ _ |_
_| |_
| _ _ _ |
| | | |
| | | |
| |_ _ _| |
|_ _|
_ |_ _ _ _ _| _
|_ _|
| |