24
\$\begingroup\$

Who doesn't love action movies with fast noisy cars, especially those with a lot of crashes? Who doesn't love action shots in ascii art?

The scene is:

Two cars are starting at opposite sides of a straight road (with 60 spaces between). They start driving towards each other at constant speeds. The car to the left drives at 1 space per second, and the one to the right drives at 2 spaces per second.

Obviously, the cars can't pass through each other, so for n ≥ 20, the scene will be two crashed cars with bonnets up at the position where the crash occurred.

As a movie lover, I want to pause the scene now and then, just to enjoy the beauty of it.

Given an integer n (function argument or STDIN), representing the number of seconds from the start of the scene, show the scene at that moment.

This is the starting scene, with 60 spaces between the front wheels:

  __                                                                __
_/  \_                                                            _/  \_
o    o                                                            o    o

this is the scene after 11 seconds:

             __                               __
           _/  \_                           _/  \_
           o    o                           o    o

and this is what it looks like after the crash (note that the bonnets are up after the crash):

                      __    __
                    _/  \/\/  \_
                    o    oo    o

I'm only interested in watching two cars crashing, so spaces, newlines, ++ doesn't matter.

This is code golf, so the shortest code in bytes wins. Answers added later can still win if they are shorter than the current accepted answer.

\$\endgroup\$
  • 2
    \$\begingroup\$ It's implied, but is the crash (20s?) the only time the bonnets are up / \ as opposed to down _ _ ? \$\endgroup\$ – Sp3000 Aug 26 '15 at 17:34
  • 2
    \$\begingroup\$ @StewieGriffin Nice one!! \$\endgroup\$ – Luis Mendo Aug 27 '15 at 2:20

16 Answers 16

10
\$\begingroup\$

CJam, 68 66 bytes

liKe<:M"  __  _/  \_o    o"6/f{\S*1$+60M3*-S*@N}:+MK={58'/t59'\t}&

Try it online

Anybody who sees the start of the code will be sure to liKe it!

Explanation:

li      Get input n and convert to integer.
Ke<     Cap n at 20.
:M      Save in variable M for multiple use later.
"  __  _/  \_o    o"
        Car (18 characters).
6/      Split into 3 lines of 6 characters.
f{      Map lines with parameter.
  \       Swap n to top.
  S*      Create string with n spaces for left margin.
  1$      Copy one car line to top. Keep original for second car
  +       Concatenate the spaces and car.
  60M3*-  Calculate 60-3*n, which is the amount of space between cars.
  S*      Create string with 60-3*n spaces.
  N       Add a newline.
}       End of line mapping.
:+      Concatenate all output pieces into single string.
MK=     Check if capped n is equal to 20.
{       If equal, replace hoods with crashed versions.
  58'/t   '/ at position 58.
  59'\t   '\ at position 59.
}&      End of conditional block for crashed hoods.
\$\endgroup\$
  • \$\begingroup\$ Are the first four characters a subliminal message for the voters? \$\endgroup\$ – John Dvorak Aug 28 '15 at 12:36
  • \$\begingroup\$ @JanDvorak Absolutely! :) Well, it wasn't completely subliminal anymore because I pointed it out. No original intention, but I noticed it immediately when I pasted the code into the answer. \$\endgroup\$ – Reto Koradi Aug 28 '15 at 14:37
14
\$\begingroup\$

Labyrinth, 394 386 bytes

I proudly introduce...

<}74}}:23}29}59}}}}}}}:111_}}}}:::::::23_}:111
?                        @
:" }}_47}_95    3""""""""(
 _ :       }    _   }    {=}
 2 23_}29_ _    ;   :      \
 0       ; 3  +_( 3_"  60{ .{.{.
"-_95:}}"" 2  0 ) 2 "  _ _ {
""       _ :  2 _ ."(; } 3 .{
 ;_92}_47} :  _ 0    = : *  ;
           : "" 2 {.{{ . -""(
}}:59_}}:::: "";_ .    {  _ "
}             "   {.{.{.  32.
}}}_95:}}}}_20-

...my new two-dimensional esolang Labyrinth! The code above isn't incredibly well golfed (there are 161 spaces and 25 NOPs, so a better layout could shorten this a lot), but at least I managed to show that the language is usable for non-trivial tasks. :)

How it works

First, a quick overview of the language:

  • Labyrinth operates on two stacks, main and auxiliary, which can hold arbitrary signed integers. At the bottom of both stacks there is an infinite amount of zeroes.
  • Commands are individual characters on a 2D grid and they form a maze (that is unknown characters, particularly spaces, are walls). " is a NOP which is not a wall and can be helpful for padding certain paths in the code. As opposed to many other 2D languages, the edges do not wrap around.
  • The instruction pointer (IP) starts at the first non-wall character (in reading order) moving to the right. @ terminates the program.
  • If possible, the IP follows corridors (also around bends). If the IP has multiple cells to move to, it will generally turn left if the top of the main stack is negative, move straight ahead if it's zero, or turn right if it's positive. When the IP hits a wall it reverses direction. (There are a few more subtleties, but they shouldn't matter for this code.) This is the only way to implement control flow.
  • Apart from arithmetic and stack manipulation commands, the source code can be modified at runtime with the four commands >v<^ which will shift a row or column of the source code cyclically by one cell. Which row or column is affected depends on the top of the stack. If the IP's own row or column is shifted, it will move with the shift. This makes it possible to jump from one edge of the source code to the other.

Now for this particular challenge, here is the general idea of the algorithm:

  • Push the ends of the cars up to the bonnets (i.e. / \_o oo o) onto the auxiliary stack.
  • Read the input and determine whether to push __ or /\ next.
  • Push the remainder of the cars (i.e. __ __ _/ \ and two leading spaces) onto the auxiliary stack.
  • Clamp the input to a maximum value of 20, let's call this N.
  • Now do the following 3 times:
    • Print N spaces.
    • Print 6 stored characters.
    • Print 60 - 3*N spaces.
    • Print 6 stored characters.
    • Print a newline.

Finally, let's look at some parts of the code. The IP starts in the top left corner, on a grid shifting command. The top of the main stack is 0 (which is used as a relative index), so the first row is shifted to the left, which also moves the IP to the right end of the grid. Now the first row is simply executed from right to left, which pushes the first set of fixed characters onto the auxiliary stack:

}74}}:23}29}59}}}}}}}:111_}}}}:::::::23_}:111<

This row shifting is useful for golfing when you want to start with a large amount of linear code.

Next we read the input and push the correct bonnets:

?  
:" 
 _ 
 2 
 0       ;
"-_95:}}""
""       _
 ;_92}_47}

The bit on the left with the three NOPs sends negative results along the top branch and non-negative results along the bottom branch. At the right they are joined back together.

Now follows another large linear section (which could probably be golfed a lot with another row-shifting trick):

   }}_47}_95 
   :       } 
   23_}29_ _ 
           3 
           2 
           : 
           : 
           : 
}}:59_}}:::: 
}
}}}_95:}}}}

This pushes the remainder of the cars onto the auxiliary stack.

Next, we compute min(20, input), which is similar to first branch:

                ;
              +_(
              0 )
              2 _
              _ 0
             "" 2
             "";_
              "  
           _20-

Finally, we have the loop which runs three times to print the lines. Each iteration of the loop contains two small (3x3) loops to print the spaces, as well as two sections to print 6 characters from the auxiliary stack:

                         @
                3""""""""(
                _   }    {=}
                    :      \
                  3_"  60{ .{.{.
                  2 "  _ _ {
                  ."(; } 3 .{
                     = : *  ;
                  {.{{ . -""(
                  .    {  _ "
                  {.{.{.  32.

One nifty trick I'd like to draw attention to is the .{.{. at the right edge. This is a dead end, so apart from the . at the end the code is executed twice, once forward and once backward. This gives a neat way to shorten palindromic code (the catch is that you need to make sure the IP takes the correct turn when exiting the dead end again).

\$\endgroup\$
  • \$\begingroup\$ Challenge: Write a program in Labyrinth without a monospaced font ;) \$\endgroup\$ – Beta Decay Aug 28 '15 at 11:10
  • 1
    \$\begingroup\$ @BetaDecay That sounds painful in any language. ;) \$\endgroup\$ – Martin Ender Aug 28 '15 at 11:47
7
\$\begingroup\$

Python 2.7, 167 164 159 Bytes

n=input();s,x=60-3*n,min(n,20)
for e in['  _',"_/ ","o  "]:p=e+(e[::-1],(' \_',' \/')[s<1])['/'in e];print" "*x+p+" "*s+(p[-1]+p[1:-1]+p[0]).replace("//","\/")

This takes input from stdin.
Demo here
Testing this -

$ ./cars.py
0
  __                                                                __  
_/  \_                                                            _/  \_
o    o                                                            o    o

$ ./cars.py
11
             __                               __  
           _/  \_                           _/  \_
           o    o                           o    o

$ ./cars.py
20
                      __    __  
                    _/  \/\/  \_
                    o    oo    o
\$\endgroup\$
  • 2
    \$\begingroup\$ (n,20)[n>20] is simply min(n,20). \$\endgroup\$ – orlp Aug 26 '15 at 21:17
  • \$\begingroup\$ @orlp thanks :) \$\endgroup\$ – Kamehameha Aug 26 '15 at 21:36
  • \$\begingroup\$ You can save a byte by replacing (' \_',' \/')[s<1] with ' \\\\_/'[s<1::2]. \$\endgroup\$ – kirbyfan64sos Aug 26 '15 at 22:53
  • \$\begingroup\$ Also, is l necessary? Could you just do for e in [' _',"_/ ","o "]: and remove l altogether? \$\endgroup\$ – kirbyfan64sos Aug 26 '15 at 22:55
  • \$\begingroup\$ @kirbyfan64sos Yes, l is not necessary now. I had to used it in an earlier version. Thanks :) \$\endgroup\$ – Kamehameha Aug 27 '15 at 5:08
5
\$\begingroup\$

R, 191 Bytes

About as good as I can get it now. Takes the seconds from STDIN and cats to STDOUT.

n=scan();p=paste0;formals(p)$collapse='';cat(sprintf(c('%s  __  %s  __  ','%s_/  \\_%s_/  \\_','%s_/  \\/%s\\/  \\_','%so    o%so    o')[-3+(n<21)],p(rep(' ',n)),p(rep(' ',60-n*3))),sep='\n')

Explanation

# Get the input from STDIN.  Requires a single number 0-20
n=scan();
# alias paste0 and set collapse default to ''
p=paste0;
formals(p)$collapse='';
# output
cat(
    # sprintf to format the strings
    sprintf(
        # vector of strings to format                                                      picks which bonnet to remove
        c('%s  __  %s  __  ','%s_/  \\_%s_/  \\_','%s_/  \\/%s\\/  \\_','%so    o%so    o')[-3+(n<21)]
        # move left car by 1
        ,p(rep(' ',n))
        # move right car by 2
        ,p(rep(' ',60-n*3))
    )
,sep='\n')

Tests

> n=scan();p=paste0;formals(p)$collapse='';cat(sprintf(c('%s  __  %s  __  ','%s_/  \\_%s_/  \\_','%s_/  \\/%s\\/  \\_','%so    o%so    o')[-3+(n==20)],p(rep(' ',n)),p(rep(' ',60-n*3))),sep='\n')
1: 0
2: 
Read 1 item
  __                                                                __  
_/  \_                                                            _/  \_
o    o                                                            o    o
> n=scan();p=paste0;formals(p)$collapse='';cat(sprintf(c('%s  __  %s  __  ','%s_/  \\_%s_/  \\_','%s_/  \\/%s\\/  \\_','%so    o%so    o')[-3+(n==20)],p(rep(' ',n)),p(rep(' ',60-n*3))),sep='\n')
1: 5
2: 
Read 1 item
       __                                                 __  
     _/  \_                                             _/  \_
     o    o                                             o    o
> n=scan();p=paste0;formals(p)$collapse='';cat(sprintf(c('%s  __  %s  __  ','%s_/  \\_%s_/  \\_','%s_/  \\/%s\\/  \\_','%so    o%so    o')[-3+(n==20)],p(rep(' ',n)),p(rep(' ',60-n*3))),sep='\n')
1: 20
2: 
Read 1 item
                      __    __  
                    _/  \/\/  \_
                    o    oo    o
> 
\$\endgroup\$
  • \$\begingroup\$ Nice work! And clever use of formals(). :) \$\endgroup\$ – Alex A. Aug 27 '15 at 5:14
  • \$\begingroup\$ @AlexA it only saved me one in the end, but seeing the two collapses was a bit offensive \$\endgroup\$ – MickyT Aug 27 '15 at 5:34
  • \$\begingroup\$ You and I have very different definitions of "offensive." \$\endgroup\$ – Alex A. Aug 27 '15 at 5:40
4
\$\begingroup\$

CJam, 120 bytes

q~20e<_["   "]*\[" _o"]\[" / " "_  " "_  " " \ " ]\[19>" /o"" _o"?]60 3 5$,*-["   "]*[0$," _o"" \o"?]3$[" _o"]+++++++zN*

Demo

Ungolfed:

q~        e# Read the input
20e<      e# min(20, input) (let's call it N)
_         e# Duplicate the value on the stack
["   "]   e# Push 3 blank spaces
*         e# Create N arrays of 3 blank spaces
\         e# Swap the top two stack elements
[" _o"]   e# Push the " _o" string to stack
\         e# Swap the top two stack elements
[" / " "_  " "_  " " \ " ]
\         e#
[         e# If N is greater than 20 then push the array [" /o"],
  19      e# otherwise [" _o"]
  >
  " /o"
  " _o"
  ?
]
60        e# 60 is the maximum space between the two cars
3         e# 3 is the number of blocks covered per move
5$,       e# Take the 6th element from the stack (an array of size N)
*-        e# Compute the remaining blocks (60 - 3 * N)
["   "]   e# Add an array of 3 blank spaces
*         e# Multiply it to fit the remaining blocks
[0$," _o"" \o"?] e# Add the (broken?) front part of the second car
3$        e# Copy the middle part of the car
[" _o"]   e# Append the right side of the car
+++++++   e# Concatenate all arrays
z         e# Transpose the array
N*        e# Join the array using new lines

Demo

\$\endgroup\$
  • \$\begingroup\$ You should be able to save quite a bit of code by not having separate strings for the left and right car, since they are basically the same (except for the crash case). A couple of local improvements: Variable J has value 19, K has value 20, saving a character each for those constants. If you need an array with one element, you can use the a operator to wrap the element, instead of using a pair of brackets. \$\endgroup\$ – Reto Koradi Aug 27 '15 at 5:35
  • \$\begingroup\$ Thanks for the tips and for the posted CJam code. It actually helps a lot to see many other possible improvements. \$\endgroup\$ – Razvan Aug 27 '15 at 8:27
4
\$\begingroup\$

PHP, 160 155 bytes

$f=str_repeat;echo$l=$f(' ',$s=min(max($argv[1],0),20)),"  __  ",
$m=$f(' ',$r=60-3*$s),"  __\n{$l}_/  \\",$r?_.$m._:'/\\',
"/  \\_\n{$l}o    o{$m}o    o\n";

The code is displayed here on 3 lines to fit the layout of the code box. Those newlines are not needed.

The ungolfed code:

// The number of seconds, between (and including) 0 and 20
$sec  = min(max($argv[1], 0), 20);
$rest = 60 - 3 * $sec;

$left = str_repeat(' ', $sec);      // left padding
$mid  = str_repeat(' ', $rest);     // space in the middle
$c = $rest ? '_'.$mid.'_' : '/\\';

echo($left.'  __  '.$mid."  __\n");
echo($left.'_/  \\'. $c ."/  \\_\n");
echo($left.'o    o'.$mid."o    o\n");

It gets the number of seconds from the command line (first argument):

$ php -d error_reporting=0 action.php 11
             __                               __
           _/  \_                           _/  \_
           o    o                           o    o

The PHP CLI option -d error_reporting=0 is needed to hide some notices PHP displays about undefined constants (str_repeat, _) that it converts to strings (2 bytes saved for each notice).

One additional byte can be saved on PHP 7 by squeezing the initialization of $f into its first use ($m=($f=str_repeat)(...)); it doesn't compile on PHP 5.

The test case and some of the techniques used to shrink the code can be found on github.

Update:

@ismail-miguel squeezed the initialization of $left and inlined $c into the arguments of echo saving 4 bytes (see comment below).

By swapping the order the variables $m and s are initialized I got rid of a pair of parentheses and saved 1 byte more.

\$\endgroup\$
  • \$\begingroup\$ 156 bytes: $f=str_repeat;$m=$f(' ',$r=60-3*($s=min(max($argv[1],0),20)));echo$l=$f(' ',$s)," __ $m __\n{$l}_/ \\",$r?_.$m._:'/\\',"/ \\_\n{$l}o o{$m}o o\n"; \$\endgroup\$ – Ismael Miguel Aug 27 '15 at 9:53
  • \$\begingroup\$ @IsmaelMiguel 155 bytes: $f=str_repeat;echo$l=$f(' ',$s=min(max($argv[1],0),20))," __ ",$m=$f(' ',$r=60-3*$s)," __\n{$l}_/ \\",$r?_.$m._:'/\\',"/ \\_\n{$l}o o{$m}o o\n"; \$\endgroup\$ – axiac Aug 27 '15 at 10:12
  • \$\begingroup\$ I tried a variation of your finding and I actually got longer code. You can edit your answer and update the byte count \$\endgroup\$ – Ismael Miguel Aug 27 '15 at 10:14
  • \$\begingroup\$ I also tried to extract o o into a variable but got the same length or worse. \$\endgroup\$ – axiac Aug 27 '15 at 10:24
  • \$\begingroup\$ You can replace the \n with real newlines. I forgot about it. And it counts as 1 byte each \$\endgroup\$ – Ismael Miguel Aug 27 '15 at 10:29
3
\$\begingroup\$

JavaScript (ES6), 121 bytes

Using template string, the 2 newlines inside the string are signficant and counted.

To save bytes, output with alert, even if the proportional font used in alert is not well suited for ASCII art and the result is ugly for n>=20 (crash).

Test running the snippet in FireFox

F=n=>alert(`  __  
_/  \\_
o    o`.replace(/.+/g,v=>(Z=x=>' '.repeat(x)+v)(n<20?n:n=20)+Z(60-3*n)).replace('__/','/\\/'))
<input id=I value=10><button onclick='F(I.value)'>go</button>

\$\endgroup\$
2
\$\begingroup\$

Python 2, 148 bytes

This uses ANSI escape codes to position the cursor in the right place to draw the cars. It then checks to see if the input was 20, if it was, it goes back and draws on the bonnets of the car.

Takes an int from stdin, output to stdout.

p=lambda x:"u  __u_/  \_uo    o".replace("u","\n\033[%dC")%(x,x,x)+"\033[4A";i=min(20,input());print p(i)+"\n"+p(66-i*2)+"\n\n\n\033[25C/\\"*(i==20)

Ungolfed:

def get_car(x):
    return "\n  __\n_/  \_\no    o".replace("\n","\n\033[%dC")%(x,x,x)+"\033[4A"

i=min(20,input())
print get_car(i)
print get_car(66-i*2)
if i==20:
    print"\n\n\033[25C/\\"
\$\endgroup\$
2
\$\begingroup\$

Pyth, 67 bytes

Kjbm+++*\ JhS,Q20d*\ -60*3Jdc3"  __  _/  \_o    o"?nJ20KXXK58\/59\\

Try it out here.

                                                                       Implicit: Q=eval(input())
          JhS,Q20                                                      Set J=min(Q,20)
                              "  __  _/  \_o    o"                     Concatenated car string
                            c3                                         Split into 3
   m                                                                   For d in the above
       *\ J                                                            J spaces before 1st car
                  *\ -60*3                                             60-3J spaces in between them
    +++          d         d                                           Concatenate spaces and car string
Kjb                                                                    Join on newlines, store in K
                                                  ?nJ20                If J != 20...
                                                       K               ... print K
                                                         XK58\/        ... else put / in position 58
                                                        X      59\\        and \ in position 59 (implicit print)
\$\endgroup\$
2
\$\begingroup\$

C, 180 191 168 bytes

#define S(x,y)"  __  ",#x"/  \\"#y,"o    o",
char*s[][3]={S(_,_)S(_,/)S(\\,_)};i;l;f(n){l=n>19&&(n=20);for(i=0;i<3;i++)printf("%*s%*s\n",n+6,s[l][i],60-3*n,s[l*2][i]);}

ungolfed:

// make a map of possible car parts
#define S(x, y) { "  __  ", #x "/  \\" #y, "o    o" }
char * s[4][3]= {
    S(_,_),
    S(_,/),
    S(\\,_)
};
i,l;
f(n){
    i = 0;
    l = n>19 && (n = 20); // l = 1, if crash happend
    for(; i < 3; i++) {
        // '*' means length (padding) is given as an int argument
        printf("%*s%*s\n", n + 6, s[l][i], 60 - 3 * n, s[l*2][i]);
    }
}

test program:

main() {
    f( 0);f( 5);f(10);
    f(15);f(20);f(21);
    return 0;
}

output:

  __                                                          __
_/  \_                                                      _/  \_
o    o                                                      o    o
       __                                           __
     _/  \_                                       _/  \_
     o    o                                       o    o
            __                            __
          _/  \_                        _/  \_
          o    o                        o    o
                 __             __
               _/  \_         _/  \_
               o    o         o    o
                      __    __
                    _/  \/\/  \_
                    o    oo    o
                      __    __
                    _/  \/\/  \_
                    o    oo    o

I was able to golf this one pretty hard. I think I started with almost 300 bytes.

But I don't know if this still fulfills all the requirements. As you can see after 21 seconds, the first car pushes the second car over to the right. I would need to add a few bytes if this isn't allowed.

Edit: fixed it. This should be more realistic than Sharknado ;-)

Edit: I could significantly shorten my solution by taking a second look at the printf man-page. If you use '*' you can supply the field length directly to printf, without the need to create a format-string with sprintf beforehand.

\$\endgroup\$
  • \$\begingroup\$ The faster car should compensate for the heaviness of the other car. They shouldn't move after hitting, or would maybe even move to the left a bit, since twice the speed on the right, but probably not twice the weight on the left. \$\endgroup\$ – mbomb007 Aug 27 '15 at 14:56
  • 3
    \$\begingroup\$ I agree with @mbomb007,but, Sharknado 3 has an IMDB-rating of 4.5, so even though your answer is obviously defying physics, it might still deserve some positive recognition =) \$\endgroup\$ – Stewie Griffin Aug 27 '15 at 14:57
  • \$\begingroup\$ I think the expected behavior is clearly defined in the question (emphasis added): "for any large enough n, the scene will be two crashed cars at the position where the crash occurred". The way I read this, they should stay at the same position indefinitely after the crash. \$\endgroup\$ – Reto Koradi Aug 27 '15 at 22:21
2
\$\begingroup\$

><>, 538 276 bytes

 :3*a6*$-:0)?v~~a2*0c4*1-e2*1+6pa9*2+b7p04.
  v          >04.
 >>1[>:0)  ?v~].
  ^  ^-1o" "<
\$:&94&12." ":oo"_":oo" ":oo
\$:&95&12." ":oo"_":oo" ":ooao
\$:&96&12."_"o"/"o" ":oo"\"o"_"o
\$:&97&12."_"o"/"o" ":oo"\"o"_"oao
\$:&98&12."o"o" ":::oooo"o"o
\$:&99&12."o"o" ":::oooo"o"o;

I've dropped the size a LOT, I'm amazed that I managed to drop the size by half. The old one is below. This one is not as efficient performance wise due to the width of the grid, mostly from the very first line.

You can test it out here. Put the amount of time passed in the "Initial Stack", not "Input"!

Here's the old version.

:3*a6*$-:0)  ?v~~a2*0c4*1-c3*1-4p^                        
v~v?)0:  <[1:$/$:1[ >:0)  ?v~]" ":oo"_":oo" ":ooaov       
] >" "o1-^    ^    <^-1o" "/"/"o"_"<]~v?)0:  <[1:$<       
>" ":oo"_":oo" ":oo^       \o" ":oo"\"\" "o1-^            
/o"\"oo:" "o"/"o"_"]~v?)0:  <[1:$o"_"o/                   
\"_"oaov   hi there  >" "o1-^                             
       >$:1[ >:0)  ?v~]"o"o" ":::oooo"o"o>$:1[ >:0)  ?v~]v
       ^    <^-1o" "<                    ^    <^-1o" "<   
              v      p4-1*29+2*9a<    ;o"o"oooo:::" "o"o"<
\$\endgroup\$
2
\$\begingroup\$

Java, 258 Chars

class M{public static void main(String[]a){String l="",m=l,r="  __  ",w="o    o",x="_/  \\_";int p=Integer.parseInt(a[0]),i=20;p=p>i?i:p;for(i=-1;++i<p;)l+=" ";for(;++i<21;)m+="   ";System.out.print(l+r+m+r+"\n"+l+(x+m+x).replace("__","/\\")+"\n"+l+w+m+w);}}

Un-Golfed

  class M {
     public static void main(String[] a) {
        String l = "", m = l, r = "  __  ", w = "o    o", x = "_/  \\_";
        int p = Integer.parseInt(a[0]), i = 20;
        p = p > i ? i : p;
        for (i = -1; ++i < p;)
           l += " ";
        for (; ++i < 21;)
           m += "   ";
        System.out.print(l + r + m + r + "\n"
              + l + (x + m + x).replace("__", "/\\") + "\n"
              + l + w + m + w);
     }
  }

Results

0
  __                                                                __  
_/  \_                                                            _/  \_
o    o                                                            o    o

1
   __                                                             __  
 _/  \_                                                         _/  \_
 o    o                                                         o    o

...

19
                     __       __  
                   _/  \_   _/  \_
                   o    o   o    o

20
                      __    __  
                    _/  \/\/  \_
                    o    oo    o

21
                      __    __  
                    _/  \/\/  \_
                    o    oo    o      
\$\endgroup\$
2
\$\begingroup\$

Python 2, 102 bytes

n=input()
for r in"  __  ","_/  \_","o    o":print((n*' ')[:20]+r+(60-3*n)*' '+r).replace('__/','/\/')

Pretty straightforward. For each row of the car, we print n spaces, that row, 60-3*n spaces, and the row again. To stop the cars, rather than doing min(n,20), it was one char shorter to limit the first run of spaces with [:20], and the second is fine because a negative times a string is the empty string.

To move up the fenders, we just do a replace. Since __ also appears on the roof, we need a bit of context to identify the fenders, so we check for a / following.

\$\endgroup\$
1
\$\begingroup\$

Java, 270 267 bytes

Pretty sure there's a better/shorter way of doing this, but my brain isn't properly engaged.

class C{public static void main(String[]a){String l="",m="",r="  __  ",w="o    o";int p=Math.min(Integer.parseInt(a[0]),20),i;for(i=0;++i<p;)l+=" ";for(i=0;++i<60-3*p;)m+=" ";System.out.print(l+r+m+r+"\n"+l+"_/  \\"+(p==20?"/"+m+"\\":"_"+m+"_")+"/  \\_\n"+l+w+m+w);}}

For n=19:

                    __      __  
                  _/  \_  _/  \_
                  o    o  o    o

For n=20:

                     __    __  
                   _/  \/\/  \_
                   o    oo    o

Ungolfed

public class Crash { public static void main(String[] args) { String left="", mid="", r=" __ ", w="o o"; int pos = Math.min(Integer.parseInt(args[0]),20),i; for (i=0; ++i<pos;){ left+=" "; } for (i=0; ++i<60-3*pos;){ mid+=" "; } System.out.print( left + r + mid + r + "\n" + left + "_/ \\" + (pos==20 ? "/" + mid + "\\" : "_" + mid + "_") + "/ \\_\n" + left + w + mid + w); } }
\$\endgroup\$
  • 1
    \$\begingroup\$ The results of this are slightly incorrect. Your cars start only 59 chars apart. My solution fixes this and Golfs yours a bit harder :) \$\endgroup\$ – Minimal Jan 13 '16 at 10:59
  • \$\begingroup\$ Good catch & well done :) \$\endgroup\$ – Denham Coote Jan 13 '16 at 11:16
1
\$\begingroup\$

PHP 7, 140 bytes

<?$s=$argv[1];$r=($f=str_repeat)(~ß,60-3*$s);echo$l=$f(~ß,min(20,$s)),"  __  $r  __  
${l}_/  \\",$r?_.$r._:~У,"/  \_
$l",$o=~ßßßß,$r,$o;

Usage:

Save as ANSI in file.php (there should be zero-width characters in $o) and run:

php -derror_reporting=~E_NOTICE -dshort_open_tag=1 file.php x

with x as the number of seconds.

And a version that works without changing the error reporting (148 bytes):

<?$s=$argv[1];$r=($f=@str_repeat)(' ',60-3*$s);echo$l=$f(' ',min(20,$s)),"  __  $r  __  
${l}_/  \\",$r?"_${r}_":"/\\","/  \_
$l",$o="o    o",$r,$o;
\$\endgroup\$
1
\$\begingroup\$

Javascript, 193 bytes

It's not a winner, but it's something

http://jsfiddle.net/yb703y0p/2/

function f(n){
c = ["  __  A", "_/  \\_A", "o    oA"]
for(i=0;i<3;i++)c[i]=c[i].replace('A',' '.repeat(n))+c[i].replace('A','')
if(n==0)c[1]=c[1].replace('__','/\\')
console.log(c.join("\n"))
}
\$\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.