模拟撞车


9

介绍

我有一些具有速度和方向的ASCII汽车。它们的速度由其数量表示。如果有汽车,<>那么它已经停止。例如:

<>
1>
2>
3>

一秒钟后,我得到

<>
 1>
  2>
   3>

两点之后,我得到

<>
  1>
    2>
      3>

如果两辆车距离太近,它们会崩溃。

1> <1
1> <2

一秒钟后,这变成

 ###
 ##

如果两辆汽车相交,它们将成为它们的标签。

如果一辆车的速度足以使其“跳上”另一辆车,则不会导致撞车。

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

变成

 <13>   ###     <>4>

如果汽车离开屏幕,它将消失(除非发生碰撞)。汽车无法在屏幕外行驶。

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

挑战

根据给定的汽车物理特性,您必须创建一个程序,该程序可以将时间推迟一秒钟。输入将是具有空间且最大速度为5(匹配正则表达式(<[1-5]|[1-5]>|<>| )+)的汽车。模拟将在一行上进行,但是该行没有固定的大小。

测试用例

<> 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

计分

这是 ,因此字节数最少的代码将获胜!


1
<22> <1 3> <2 2 ### ## 2不应该在此处-输出还有其他问题
DanielIndie

1
我建议在测试用例中添加“ <> <1 <2 <3 <4 <5”“ ### <2 <3 <4 <5”其中
DanielIndie

@DanielIndie真是有趣。我继续编辑挑战。
阿诺尔德

我花了一些时间才知道<>速度0的平均值
l4m2

抱歉! 我在写完所有内容后补充了这一点,所以我忘记了解释。
内森·伍德

Answers:


3

JavaScript(ES6),140个字节

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``

在线尝试!

已评论

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

0

JavaScript(Node.js),259字节

254到259,因为我添加了测试用例,而这在使我的结果正则表达式查找器复杂化的测试用例中是不存在的

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)

在线尝试!


0

视网膜,178字节

^\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

在线尝试!链接包括测试用例。说明:

^\d([^>])
 $1

处理汽车向左移动的情况。

T`5-1`d`<.

暂时补充向左移动的汽车的位数。

 *(<(\d)|((\d)>|<>))
$2* $#3*5* $4* $.`* $&¶

将每辆车放在自己的行($.`* $&¶)上,并根据车速添加一些缩进;向左行驶的汽车获得补充速度,而向左行驶的汽车获得比速度高5倍的速度。

T`d`5-1`<.

取消补左行汽车数字。

m`^.{0,5}

将所有汽车5向左移动。这样可以解决所有汽车的缩进问题。

G`.

删除所有离开左侧的汽车。

N^$`
$.&

按相反的水平顺序排序其余的汽车。

{

重复其余阶段,直到所有汽车都已处理完毕。

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

只要下一车不撞车,就将其添加到结果中。

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

将撞车事故添加到结果中。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.