11
\$\begingroup\$

The Scream™ Winter Sale is on and you just nabbed yourself an intense minimalist action game, Super Square. Upon playing it, you come to the realisation that either the game's just ridiculously hard or you're just really bad at it. In particular, there's this "rain" pattern that seems to get you every time...

enter image description here

Frustrated, you decide to give yourself a different challenge: Draw the rain pattern in ASCII art!

Input

Input is a single positive integer indicating the size of the pattern n, given via STDIN or function argument.

Output

Output is the rain pattern at the specified size, returned as a string or printed via STDOUT. Leading or trailing whitespace before or after the entire image is okay. Additionally, the image need not be flush to the left of the screen, but must be clearly discernible.

Here's n = 10:

 ...................
| ----------------- |
|  ...............  |
| | ------------- | |
| |  ...........  | |
| | | --------- | | |
| | |  .......  | | |
| | | | ----- | | | |
| | | |  ...  | | | |
| | | | | - | | | | |
| | | | |   | | | | |
| | | | | - | | | | |
| | | |  ...  | | | |
| | | | ----- | | | |
| | |  .......  | | |
| | | --------- | | |
| |  ...........  | |
| | ------------- | |
|  ...............  |
| ----------------- |
 ...................

Here's n = 5:

 --------- 
  .......  
 | ----- | 
 |  ...  | 
 | | - | | 
 | |   | | 
 | | - | | 
 |  ...  | 
 | ----- | 
  .......  
 --------- 

And finally, here's n = 1 (just the innermost two walls):

 - 

 - 

Construction (for extra clarity)

The playing area is divided into two pairs of quadrants like so:

 AAAAAAAAA
B AAAAAAA B
BB AAAAA BB
BBB AAA BBB
BBBB A BBBB
BBBBB BBBBB
BBBB A BBBB
BBB AAA BBB
BB AAAAA BB
B AAAAAAA B
 AAAAAAAAA

The upper/lower quadrants should alternate between horizontal walls represented by hyphens -, and gaps shaded with dots .. The left/right quadrants should alternate between spaces and vertical walls represented by pipes |. The main diagonals are empty, and should always be filled with spaces.

The rain pattern of size n has 2n walls, with walls from the upper/lower quadrants closest to the centre and walls alternating between quadrants as we move away from the centre.

Scoring

This is code-golf, so the code in the fewest bytes wins.

\$\endgroup\$
  • 4
    \$\begingroup\$ This game... I know what you are talking about. It stole like 40 hours of my life until I beat it. ^^ \$\endgroup\$ – ThreeFx Dec 24 '14 at 19:36
3
\$\begingroup\$

CJam, 93 87 78 61 59 bytes

ri:K_+){K" |"*KKI-z:I-I2%:L+<SL>\+_W%L'-'.?I2*Ig-*@I0=>N}fI

Takes value n via STDIN

Few examples:

1
 - 

 - 
2
 ... 
| - |
|   |
| - |
 ... 
5
 --------- 
  .......  
 | ----- | 
 |  ...  | 
 | | - | | 
 | |   | | 
 | | - | | 
 |  ...  | 
 | ----- | 
  .......  
 --------- 
10
 ................... 
| ----------------- |
|  ...............  |
| | ------------- | |
| |  ...........  | |
| | | --------- | | |
| | |  .......  | | |
| | | | ----- | | | |
| | | |  ...  | | | |
| | | | | - | | | | |
| | | | |   | | | | |
| | | | | - | | | | |
| | | |  ...  | | | |
| | | | ----- | | | |
| | |  .......  | | |
| | | --------- | | |
| |  ...........  | |
| | ------------- | |
|  ...............  |
| ----------------- |
 ................... 

This can be golfed a lot, which I will do first thing tomorrow.

Try it online here

\$\endgroup\$
5
\$\begingroup\$

Haskell 150 bytes

I realize it's not going to win, just wanted to post my first codegolf :D

q n=putStr$unlines$iterate(\l->let[a,b]=if l!!0!!1=='-'then"|."else" -";c=[a:s++[a]|s<-l];t=' ':[b|x<-l!!0]++" "in t:c++[t])[" - ","   "," - "]!!(n-1)

Use by loading into GHCi and calling q n where n is the size.

Some examples:

*Main> q 1
 - 

 - 
*Main> q 2
 ... 
| - |
|   |
| - |
 ... 
*Main> q 5
 --------- 
  .......  
 | ----- | 
 |  ...  | 
 | | - | | 
 | |   | | 
 | | - | | 
 |  ...  | 
 | ----- | 
  .......  
 --------- 
*Main> q 10
 ................... 
| ----------------- |
|  ...............  |
| | ------------- | |
| |  ...........  | |
| | | --------- | | |
| | |  .......  | | |
| | | | ----- | | | |
| | | |  ...  | | | |
| | | | | - | | | | |
| | | | |   | | | | |
| | | | | - | | | | |
| | | |  ...  | | | |
| | | | ----- | | | |
| | |  .......  | | |
| | | --------- | | |
| |  ...........  | |
| | ------------- | |
|  ...............  |
| ----------------- |
 ................... 

Someone can probably do better, I'm fairly new to Haskell.

\$\endgroup\$
  • 6
    \$\begingroup\$ Don't worry about winning. ;) Winning is for CJam and Pyth. Golfing in other languages is about beating submissions in the same "weight class", and learning some new obscure features of your language. Welcome to PPCG! \$\endgroup\$ – Martin Ender Dec 24 '14 at 23:43
3
\$\begingroup\$

Python, 204,198,191 bytes

r=lambda a,b,d=' ':d.join((a,b,a[::-1]))
def f(s,i,n):d=[r(s[:i],'.-'[(n-i)%2]*((n-i)*2-1))];return i==n and[r(s,' '*(2*(i%2)+1),'')]or d+f(s,i+1,n)+d
g=lambda n:'\n'.join(f('| '*(n/2),0,n))

"r" is a utility function that writes "b" surrounded by reflected "a", with an optional delimiter, (yes lambda parameters can have defaults). "f" is recursive, generating sides and middle parts for each level "g" is the rain function, which can be called with an integer to return the text requested.

\$\endgroup\$
  • \$\begingroup\$ I just realised I forgot to allow functions which return strings, which I usually permit (have edited that in). You can still save on a few chars by removing whitespace and putting lines 4, 5 on one line :) \$\endgroup\$ – Sp3000 Dec 25 '14 at 3:40
  • \$\begingroup\$ With a bit of short circuit evaluation you can get this down to 190 :) link \$\endgroup\$ – Sp3000 Dec 25 '14 at 4:06
  • \$\begingroup\$ Sorry, I downvoted you by an accident. I can cancel it if you edit. \$\endgroup\$ – nutki Dec 25 '14 at 18:41
1
\$\begingroup\$

Perl 5: 74 bytes (73 code + -p)

#!perl -p
s/.*/ /;$a=qw(- .)[$|--]x
s/.+/$"$&$"/g,s/^|\z/ $a 
/g,$"^="\\"for($_)x$&

Takes parameter on the input (end of line character required for proper function):

$ perl rain.pl <<<"3"
 ----- 
  ...  
 | - | 
 |   | 
 | - | 
  ...  
 ----- 

Ungolfed:

                         # Read the input line into $_ (-p)
s/.*/ /;                 # Replace the input with a space (plus the original eol), saves the parameter in $&
for(($_)x$&) {           # Iterate $& times without affecting $_
  $c=s/.+/$"$&$"/g;      # Add $" (initially space) at the start and the end of each line, stores number of lines in $c
  $a=("-",".")[$|--]x$c; # Set $a to $c times minus or dot using magic $| (which iterates over 1 and 0 on decrement)
  s/^|\z/ $a \n/g;       # Equivalent to $_=" $a \n$_ $a \n"
  $"^="\\";              # Alternate $" between space and bar using the string xor 
}
                         # Print $_ (-p)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.