# Introduction

I have some ASCII cars that have velocity and direction. Their velocity is represented by their number. If a car is <> then it has stopped. For example:

<>
1>
2>
3>


After one second, I get

<>
1>
2>
3>


After two, I get

<>
1>
2>
3>


If two cars are too close, they crash.

1> <1
1> <2


After a second, this becomes

 ###
##


If two cars intersect, they become hashtags were they would be.

If one car is fast enough to 'hop' over the other, it does not result in a crash.

3><1   2><1   4><>


becomes

 <13>   ###     <>4>


If a car goes left off-screen, it disappears (unless there's a crash). There is no way for a car to go right off-screen.

 <11>
<1  1>
1    1>
1>


# Challenge

Based on the given car physics, you must create a program that can time-step one second into the future. The input will be cars with spaces and a maximum velocity of 5 (matching regex (<[1-5]|[1-5]>|<>| )+). The simulation will happen on one line, however that line has no fixed size.

# Test cases

<> 1> 2> 3> 4> 5>
<>  1>  2>  3>  4>  5>

1><1   1> <1   1>  <1
##     ###     1><1

2><2   2> <2   2>  <2   2>   <2   2>    <2
<22>    ###      ##       ###       2><2

<22>  <1 3>   <2
###     ##

<><>     1><>     2><>     3><>     4><>     5><>
<><>      ###       ##       ###      <>4>     <> 5>

<><1 <2 <3 <4 <5
###<2<3<4<5


# Scoring

This is , so code with the smallest number of bytes wins!

• <22> <1 3> <2 2 ### ## 2 should not be there - there are other problemts with the outputs – DanielIndie Mar 31 '18 at 7:19
• i would suggest adding "<><1 <2 <3 <4 <5" "###<2<3<4<5" to the testcases - case were a crush happens but and other cars "touching" but not part of it – DanielIndie Mar 31 '18 at 8:06
• @DanielIndie That's an interesting one. I went ahead and edited the challenge. – Arnauld Mar 31 '18 at 8:12
• It costs me some time to know <> mean one of velocity 0 – l4m2 Mar 31 '18 at 13:00
• Sorry! I added that in after writing everything so I forgot to explain it. – Nathan Wood Mar 31 '18 at 13:02

# JavaScript (ES6), 140 bytes

s=>[...s.replace(/\S./g,([a,b],i)=>r[r[i+=+b?-b:~~a]=r[i]?C:a,++i]=r[i]?C:b,r=[],C='#')&&r].map((c,i)=>c?r[i-1]==C|r[i+1]==C?C:c:' ').join


Try it online!

### Commented

s =>                      // given the input string s
[ ...s.replace(         // search in s ...
/\S./g,               //   ... all substrings consisting of 2 non-whitespace characters
([a, b], i) =>        //   let a be the 1st character, b the 2nd one and i the position
r[                  //   update r[]:
r[i +=            //     apply the car velocity to i:
+b ? -b         //       if b is a digit, then move b cells backwards
: ~~a   //       else: use a to move forwards (or don't move at all)
] = r[i] ? C : a, //     if r[i] is set, overwrite it with '#'; otherwise, insert a
++i               //     increment i for the 2nd character
] = r[i] ? C : b,   //     if r[i] is set, overwrite it with '#'; otherwise, insert b
r = [],             //   initialize r[] to an empty array
C = '#'             //   define C as the 'crash' character
) && r ]                // end of replace(); return a fully iterable copy of r[]
.map((c, i) =>          // for each entry c at position i in this array:
c ?                   //   if c is defined:
r[i - 1] == C |     //     if either the previous
r[i + 1] == C ?     //     or the next cell is '#' (in the original array):
C                 //       replace the current cell with '#'
:                   //     else:
c                 //       let c unchanged
:                     //   else:
' '                 //     insert a space
).join                // end of map(); join the result


# JavaScript (Node.js), 259 bytes

254 to 259 because i added test case which wasnt in the testcases that complicated my result regex finder

s=>{
c=[]
n=[S=""]
s.replace(/<?\d>?|<>/g,([d,D],i)=>d>0?g(i+ +d,d)+g(i-~d,D):g(i-~~D,d)+g(i-~~D+1,D))
for(i of n)S+=i||" "
return S.replace(/1?[2-9]*1?/g,(d,i)=>d>"1".repeat(l=d.length)?"#".repeat(l):c.slice(i,i+l).join)}
g=(x,p)=>x<0||(n[x]=-~n[x],c[x]=p)


Try it online!

# Retina, 178 bytes

^\d([^>])
$1 T5-1d<. *(<(\d)|((\d)>|<>))$2* $#3*5*$4* $.*$&¶
Td5-1<.
m^.{0,5}

G.
N^$$.&
{+m\A( *)  (.*)¶\1(..?)1$3$2
m^\A( *)( *)..(.*)¶\1(..?)1##$.2*#$3


Try it online! Link includes test cases. Explanation:

^\d([^>])
$1  Handle the case of a car moving off the left. T5-1d<.  Temporarily complement the digits of cars moving to the left.  *(<(\d)|((\d)>|<>))$2* $#3*5*$4* $.*$&¶


Put each car on its own line ($.*$&¶) and add some indent depending on the speed of the car; left-moving cars get the complemented speed, while non-moving cars get 5 more than the speed.

Td5-1<.


Uncomplement the left-moving car digits.

m^.{0,5}


Move all of the cars 5 to the left. This fixes the indent for all cars.

G.


Delete all cars that have moved off the left.

N^$$.&


Sort the remaining cars in reverse horizontal order.

{


Repeat the remaining stages until all the cars have been processed.

+m\A( *)  (.*)¶\1(..?)1$3$2


As long as the next car does not crash, add it to the result.

m^\A( *)( *)..(.*)¶\1(..?)1##$.2*#$3
`

Add a crashing car to the result.