7
\$\begingroup\$

Here in the absolutely made up country Rnccia we have a lot of good stuff, but the roads aren't good. There are lots of holes and only half of them get fixed. Even then, only half of the fixes are well done. Your task is to embrace the bad roads of this country and draw them for me.

Input

You receive two positive integers as input. One represents the length of the road, and the second represents the number of lanes in each direction. The length of the road is guaranteed to be an even number.

Output

Output or return an ASCII-art image of the road with as many holes as will fit. Read the next two sections to see how the road and holes should be generated.

Road layout

  1. Every road has a straight line of minuses(-) above and below it.
  2. Every road has the same number of lanes in both directions.
  3. Every road has a line of equal signs(=) down the middle.
  4. Lanes have dashed lines between them (excep the special middle line from rule #3). One minus, one whitespace and so on. Starting with the minus.

Examples of roads:

Road with one lane in both sides:

--------------

==============

--------------

Road with two lanes in both sides:

--------------

- - - - - - -

==============

- - - - - - - 

--------------

Hole layout

  1. Every road has 7n total holes. n is the ceiling of the number of holes that will fit on the road divided by seven.

  2. Every road has 4n unfixed holes. An unfixed hole is represented as double O (OO).

  3. Every road has 2n holes that are fixed and represented as double # (##).

  4. Every road also has n holes that are fixed well and represented as double H (HH).

  5. Holes (in any state) appear on every lane in diagonal pattern, starting at the top right and moving down-across. A new line of holes should start in every 4th lane and every 8 characters in the top lane. See the examples for clarification.

  6. The order of holes is irrelevant.

  7. If 7n holes can't fit in road of given length, you may use #6 to choose which of the holes will not be visible.

In this example we have 14 holes, 4 of which are fixed poorly, and 2 which are fixed well:

------------------
OO      HH      OO
- - - - - - - - -
  ##      OO
- - - - - - - - -
    ##      OO
==================
      OO      OO
- - - - - - - - -
OO      ##      HH
- - - - - - - - -
  ##      OO
------------------

In this example we hava 9 holes, which is not multiple of 7. So your pool of holes to use is size 14 with 8 (OO), 4(##), 2(HH). You may use any holes from it to fill road.

------------------
OO  OO  OO  OO  OO
==================
  OO  OO  OO  ##
------------------

Another corner example. Road can have only 3 holes. So pool is 7: 4(OO), 2(##), 1(HH). Again, any can be used:

------
##  
- - - 
  ##
======
    HH
- - - 

------

Other rules

  1. You may change the appearance of holes to two of any other character, except -, = and whitespace. Just mention it in your answer.
  2. It's , so make your code as compact as possible.
\$\endgroup\$
  • \$\begingroup\$ I think you probably want to do something like this: 1) calculate how many holes will fit on the road in that pattern. 2) make roughly half of these fixed holes. 3) make roughly half of the fixed holes fixed well. \$\endgroup\$ – ETHproductions Mar 23 '17 at 15:13
  • \$\begingroup\$ I've added couple more examples to clarify approximate ratio \$\endgroup\$ – Dead Possum Mar 23 '17 at 15:28
  • \$\begingroup\$ Is there some reason for using the 4:2:1 ratio instead of 4:2:2? The latter can be simplified to 2:1:1 and is simpler by all measures. \$\endgroup\$ – ETHproductions Mar 23 '17 at 15:38
  • \$\begingroup\$ @ETHproductions No big reason, powers of 2 seems more appealing to me \$\endgroup\$ – Dead Possum Mar 23 '17 at 15:42
  • \$\begingroup\$ starting at the top right in the description, but at the top left in the examples. \$\endgroup\$ – Titus Mar 24 '17 at 2:07
1
\$\begingroup\$

JavaScript (ES6), 158 bytes

f=
(w,l)=>[...Array(l*4+1)].map((_,i)=>[...Array(w)].map((_,j)=>i%2?(i>>1)-j&3?`  `:s[n%7]+s[n++%7]:i/2%l?`- `:i-l-l?`--`:`==`).join``,n=0,s=`O#OHO#O`).join`\n`
<div oninput=o.textContent=f(+w.value,+l.value)><input id=w type=number min=1 value=1><input id=l type=number min=1 value=1><pre id=o>--
OO
==

--

\$\endgroup\$
  • \$\begingroup\$ Hooray for the winner! \$\endgroup\$ – Dead Possum May 17 '17 at 8:26
6
\$\begingroup\$

Python 2, 229 213 204 200 188 bytes

lambda n,l:"\n".join(["-"*l]+[i%2and"".join(j-i/2&1+2*(n>1)and"  "or x.append(1)or"H0000##"[len(x)%7]*2for j in range(l/2))or((i-n*2and"- "or"=")*l)[:l]for i in range(1,n*4)]+["-"*l])
x=[]

Try it online!


My longest answer so far

\$\endgroup\$
  • \$\begingroup\$ Can't check right now, but wouldn't it be shorter to add a line at the beginning 'r=range'? and replace all range with r? \$\endgroup\$ – Matias K Mar 24 '17 at 1:01
  • \$\begingroup\$ @Matias it would be exactly the same length \$\endgroup\$ – ovs Mar 24 '17 at 5:56
2
\$\begingroup\$

PHP, 160 186 178 173 170 168 163 bytes

for(;$y<=2*$h=2*$argv[2];$y+=print"
")for($x=0;$x++<$w=$argv[1];)echo$y%2?($x+$y+$h*$y)%($h+2)>1|$x>($w&~1)?" ":UBUGUBU[$n++%14/2]:" -="[$y%(2*$h)?$y-$h?$x&1:2:1];

takes input from command line; Run with -nr. The holes are Good, Bad and Ugly.

breakdown

for(;$y<=2*$h=2*$argv[2];$y+=print"\n") # loop throug lines and lanes, post-print newline
    for($x=0;$x++<$w=$argv[1];)echo         # loop from left to right and print ...
        $y%2
        ?                                       # lanes:
            ($x+$y+$h*$y)%($h+2)>1                  # if not the position for a hole
            |$x>($w&~1)                             # or too close to the end
            ?" "                                    # then space
            :UBUGUBU[$n++%14/2]                     # else hole
    :" -="[                                     # or lines:
        $y%(2*$h)                               # if neither top nor bottom line
            ?   $y-$h                               # if not middle line
                ?   $x&1                                # then "- "
                :   2                                   # else "=="
            :   1                                   # else "--"
    ]
    ;
  • ($x-$y)+$y*($h+2) --> ($x+$y+$h*$y)
  • $w<$x+1&&$w&1 --> $x>$w-($w&1) --> $x>($w&~1)
  • ($y%(2*$h)?$y-$h?" -"[$x&1]:"=":"-") -->
    ($y%(2*$h)?" -="[$y-$h?$x&1:2]:"-") (thanks @Christoph) -->
    " -="[$y%(2*$h)?$y-$h?$x&1:2:1]
\$\endgroup\$
  • \$\begingroup\$ $y-$h?" -"[$x&1]:"=" to " -="[$y-$h?$x&1:2] for 1 byte. \$\endgroup\$ – Christoph Mar 24 '17 at 7:39
  • \$\begingroup\$ Uhm you might want to check that: $y<=2*$h=2*$argv[2] to $y<=2*$h (+2, first iteration starts even if h == 0), for($x=0;$x++<$w=$argv[1];) to for($x=0;$x++<$w=$argv[1];$h=2*$argv[2]) (+0, basically just moved), ($x+$y+$h*$y)%($h+2)>1 to ($x+$y*++$h)%++$h>1 (-3). Adding up to -1 bytes but I haven't tested it. \$\endgroup\$ – Christoph Mar 24 '17 at 8:08
  • \$\begingroup\$ @Christoph division by zero at %(2*$h) \$\endgroup\$ – Titus Mar 24 '17 at 8:15
1
\$\begingroup\$

Batch, 380 bytes

@echo off
set h=O#OHO#O
set r=
for /l %%i in (1,1,%1)do call set r=--%%r%%
set/an=%2*2-1
for /l %%j in (0,1,%n%)do call:c %1 %2 %%j
echo %r%
exit/b
:c
if %3==0 (echo %r%)else if %3==%2 (echo %r:-==%)else echo %r:--=- %
set s=
for /l %%i in (1,1,%1)do set/an=%3-%%i^&3&call:l
echo(%s%
exit/b
:l
if %n%==3 (set s=%s%%h:~,1%%h:~,1%&set h=%h:~1%%h:~,1%)else set s=%s%  
\$\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.