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.
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 - 741 621 600 characters (but handles the new cases properly)
$ gcc water.c && ./a.out < test6.txt
__ ___
/ \xxxxx/
/ _______
________ / \ /
_____/ \xxx/__ \xxxx\_
____ / \xxxx/__/xxxxx/
\xxxxxxxx/ \xxxxxxxxx/
\xxxxxx\ /xxxx/
#include<stdio.h>
char d[99][99],*p,*e,*z,*s=d,c,S=' ',D='-',O='.',U='_';n,w,x,N=99,i;
g(y){for(i=0;!i;p+=N,e+=N){i=*p==D;for(z=p;z!=e;z+=y){if(*z!=O&&*z!=
D)break;*z=*z==O?S:U;}}}f(char*n,int r){if(*n==O||*n==D){*n=r>0?'x':
S;int k;for(k=0;k<9;k++)f(n+k/3*N-N+k%3-1,r+k/3-1);}}main(){for(p=s;
gets(p);p+=N,n++){x=strlen(p)-1;w=x>w?x:w;}for(p=s,e=d[N];p<s+N;p++)
{for(i=1,z=p;z<e;z+=N)c=*z,c==0?*z=c=S:0,i?c==S?*z=O:c==U?*z=D:0:0,(
c=='/'&&z[1]!=U)||(c=='\\'&&z[-1]!=D)||c==U?i=1-i:0;}p=s;e=s+w;g(1);
p=s+w;e=s;g(-1);for(p=s;p<s+w;p++){for(z=p;*z==S;z+=N);f(z,1);}for(i
=0;i<n;i++)printf("%.*s\n",w+1,d[i]);}