Simulating Crashing Cars

9

0

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!

Nathan Wood

Posted 2018-03-31T03:13:09.240

Reputation: 679

1<22> <1 3> <2 2 ### ## 2 should not be there - there are other problemts with the outputs – DanielIndie – 2018-03-31T07:19:29.557

1i 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 – 2018-03-31T08:06:21.730

@DanielIndie That's an interesting one. I went ahead and edited the challenge. – Arnauld – 2018-03-31T08:12:28.217

It costs me some time to know <> mean one of velocity 0 – l4m2 – 2018-03-31T13:00:41.540

Sorry! I added that in after writing everything so I forgot to explain it. – Nathan Wood – 2018-03-31T13:02:28.547

Is it possible that more than two cars crash together? – l4m2 – 2018-04-17T01:34:50.900

@l4m2. Yes. If they intersect, then they all become hashes. – Nathan Wood – 2018-04-17T23:45:37.373

Answers

3

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

Arnauld

Posted 2018-03-31T03:13:09.240

Reputation: 111 334

0

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!

DanielIndie

Posted 2018-03-31T03:13:09.240

Reputation: 1 220

0

Retina, 178 bytes

^\d([^>])
 $1
T`5-1`d`<.
 *(<(\d)|((\d)>|<>))
$2* $#3*5* $4* $.`* $&¶
T`d`5-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.

T`5-1`d`<.

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.

T`d`5-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.

Neil

Posted 2018-03-31T03:13:09.240

Reputation: 95 035