The challenge
The shortest code by character count to identify and mark water depressions in the ASCII representation of a land from input.
Input will be an ASCII representation of a landscape, having hills, valleys and flat lands. The program should simulate what the landscape would look like if if was flooded - filling all valleys with water (character x
).
The landscape will always start and stop with the character _
and will be at least 2 characters long, making the shortest input __
.
A hill is defined as a raise, and should not be filled with water:
__
_/ \_
A valley is defined as a depression and will be filled with water until a flatland is encountered:
_ _
\__/
Input can be assumed clean and will be composed only from the characters space (), newline (
\n
), underscore (_
), and forward and backward slashes (/
and \
). Input can be seen as a continuous line, and any input that contains ambiguous line input such as _/_
or
_ _
\_/
/ \
Is considered invalid.
Regarding underwater caves, water level should be maintained if cave level goes above water level.
Test cases
Input:
__/\__
\__
\ ___ ___________
/ / \_ \_
\_____/ \__ _/
\/
Output:
__/\__
\__
\ ___ ___________
/xxxxxx/ \xxxxxx\_
\xxxxx/ \xxxxx/
\/
Input:
__ ___
/ \_____/
/ _______
________ / \ /
_____/ \ /__ \ \_
____ / \ /__/ __/
\_ / \ ____/
\______\ /____/
Output:
__ ___
/ \xxxxx/
/ _______
________ / \ /
_____/ \xxx/__ \xxxx\_
____ / \xxxx/__/xxxxx/
\xxxxxxxx/ \xxxxxxxxx/
\xxxxxx\ /xxxx/
Input:
__ _
_ ____ ____ _____/ \ /
\ / \ __________/ \ __/ ___ /___\
\___/ \ \ \ \___/ /_
/________\ \___________\
Output:
__ _
_ ____ ____ _____/ \xxx/
\xxxxx/ \xxxxxxxxxxxxxxxxxx/ \xxxxxx/ ___ /xxx\
\xxx/ \xxxxxxx\ \xxx\___/xx/_
/xxxxxxxx\ \xxxxxxxxxxx\
Code count includes input/output (i.e full program).
C -
741621600 characters (but handles the new cases properly)Python,
702 805 794 778 758 754 710651Handles DigitalRoss's test cases, as well as large test cases such as http://pastie.org/708764.
Example run
Code
Perl, 534
545 550 566 569 567 578 594 596This handles all the test cases that I've seen. Newlines are optional and are only there for formatting.
Call it as e.g.
perl water.pl test.txt
.Here's another funny edge case (for my algorithm anyway) not in any of the previous examples:
The verbose version I'd put up earlier still fails on that.
Ruby,
794 759 769 752 715 692 663 655 626616Additional test cases: http://pastie.org/708281 and http://pastie.org/708288 and http://pastie.org/708310
Compressed except for indent: