In a fictional 2D world, a set of 2D printing instructions for an object can be represented by a list of integers as follows:
1 4 2 1 1 2 5 3 4
Each number represents the height of the object at that particular point. The above list translates to the following object when printed:
#
# # #
# ###
## ####
#########
We then fill it with as much water as we can, resulting in this:
#
#~~~~#~#
#~~~~###
##~~####
#########
We define the capacity of the object to be the units of water the object can hold when completely full; in this case, 11.
Strictly speaking, a unit of water (~
) can exist at a location if and only if it is surrounded by two solid blocks (#
) in the same row.
Challenge
Take a list of positive integers as input (in any format), and output the capacity of the object printed when the list is used as instructions.
You can assume the list contains at least one element and all elements are between 1 and 255.
Test Cases
+-----------------+--------+
| Input | Output |
+-----------------+--------+
| 1 | 0 |
| 1 3 255 1 | 0 |
| 6 2 1 1 2 6 | 18 |
| 2 1 3 1 5 1 7 1 | 7 |
| 2 1 3 1 7 1 7 1 | 9 |
| 5 2 1 3 1 2 5 | 16 |
| 80 80 67 71 | 4 |
+-----------------+--------+