指示灯何时闪烁?


10

假设您有两个灯。这些指示灯以特定速率闪烁:

Light 0: Delay 0ms and then blink every 1000ms
Light 1: Delay 500ms and then blink every 1000ms

让我们在前2000ms中模拟这些灯:

0ms:    Light 0 on
500ms:  Light 1 on
1000ms: Light 0 off
1500ms: Light 1 off
2000ms: Light 0 on

挑战

给定一个表示照明时间的有序对的列表,编写一个程序或函数以输出它们闪烁时的顺序。

输入值

输入应采用以下格式:

TimeToSimulate
Light0Delay,Light0Period
Light1Delay,Light1Period
...

以这种格式,上面的示例将是:

2000
0,1000
500,1000

输出量

输出应为一系列有序三元组:

Time,LightNum,LightStatus

如果灯打开,LightStatus是真实值,如果灯关闭,则是伪值。

上面示例的输出为:

0,0,True
500,1,True
1000,0,False
1500,1,False
2000,0,True

如果两个指示灯同时闪烁,则编号较小的指示灯应首先显示在输出中。

其他的东西

  • 输入和输出格式不严格
  • 代码不应产生任何错误
  • 解决方案不应该依赖比赛条件
  • 无标准漏洞
  • 这是,所以最短的解决方案是成功的!

测试用例

Input:

2000
0,1000
500,1000

Output:

0,0,True
500,1,True
1000,0,False
1500,1,False
2000,0,True

----

Input:

2
0,1
0,1

Output:

0,0,True
0,1,True
1,0,False
1,1,False
2,0,True
2,1,True

----

Input:

500
100,50
200,100
300,150

Output:

100,0,True
150,0,False
200,0,True
200,1,True
250,0,False
300,0,True
300,1,False
300,2,True
350,0,False
400,0,True
400,1,True
450,0,False
450,2,False
500,0,True
500,1,False

----

Input:

1000
23,345
65,98
912,12
43,365

Output:

23,0,True
43,3,True
65,1,True
163,1,False
261,1,True
359,1,False
368,0,False
408,3,False
457,1,True
555,1,False
653,1,True
713,0,True
751,1,False
773,3,True
849,1,True
912,2,True
924,2,False
936,2,True
947,1,False
948,2,False
960,2,True
972,2,False
984,2,True
996,2,False

排行榜摘要:


多少输出就足够了?
aschepler '17

@aschepler是什么意思?输入指定了一个要“模拟”的时间
Daniel M.

Answers:


3

JavaScript,98 97字节

a=>b=>[...Array(a+1)].map((_,i)=>b.map((d,j)=>d[0]--||c.push([i,j,d[d[0]=d[1]-1,2]^=1])),c=[])&&c

在线尝试

感谢Shaggy节省了一个字节 -使用currying输入语法。


用currying保存一个字节:a=>b=>
毛茸茸的

@毛茸茸。您太快了,我正在准备编辑。

经验法则:如果有2个输入,请始终咖喱!
毛茸茸的


2

果冻 26  25 字节

Ḣrm⁸ð€µ;€€"J;"J$€€ẎẎḂ0¦€Ṣ

一个二进位链接,它获取一个delay, period数字列表和一个时间范围内的数字,并返回一个time, light, action整数列表。

灯是1分度的,0代表“关闭”动作,而1代表“开启”动作。

在线尝试!

怎么样?

Ḣrm⁸ð€µ;€€"J;"J$€€ẎẎḂ0¦€Ṣ - Link: [[delay, period],...], time-frame 
    ð€                    - for €ach [delay, period]:
Ḣ                         -   head (get the delay and modify the item to [period])
 r                        -   inclusive range to time-frame = [delay,delay+1,...,time-frame]
   ⁸                      -   chain's left argument = [period]
  m                       -   modulo slice = [delay, delay+period, delay+2*period, ...]
      µ                   - monadic chain separation, call that v
           J              - range(length(v)) = [1,2,...,nLights]
          "               - zip with:
       ;€€                -   concatenate for €ach for €ach (add light indexes to times)
               $€€        - last two links as a monad for €ach for €ach:
              J           -   range (length(switch-times-for-a-light))
             "            -   zip with:
            ;             -     concatenation (i.e. append a 1-based index)
                  ẎẎ      - tighten & tighten again (flatten by 2 to a list of triples)
                      |€  - sparse application of (for €ach):
                     0    - ...to indexes: 0 (=last entry)
                    Ḃ     - ...action: modulo by 2 (even appended indexes ->0s; odds -> 1s)
                        Ṣ - sort the resulting list of triples

2

Python 2中206个 214字节

  • 添加了八个字节以符合规则(通过stdin输入)。
Q=input();D,T=Q[0],[map(int,q.split(","))for q in Q[1:]];O,l=[],len(T)
for j in range(l):
	t,b=T[j][0],9>8
	while t<=int(D):O+="%0*d,%0*d,%s"%(len(D),t,len(str(l)),j,b),;b=not b;t+=T[j][1]
print"\n".join(sorted(O))

在线尝试!

该代码生成一个无序列表,其中包含每个灯的切换时间,填充这些时间和灯标识符,对所述列表进行排序并输出。


根据标准规则,您必须接受输入,但不能期望它预先存在于变量中。您可能会发现,using input()也可以使它们减少字节计数(因为Python 2 input()是,所以不需要字符串解析eval(raw_input())):)。
乔纳森·艾伦,

...同样,如果您使用数字而不是字符串进行O排序,这也可能会减少字节数,
Jonathan Allan

@JonathanAllan感谢您注意到规则差异;我不会采纳您的建议,因为到目前为止,Python 2的答案要短得多。
乔纳森·弗雷希

1

Perl 5,106 +1(-n)= 107字节

($a[$i],$b[$i++])=eval for<>;for$i(0..$_){for(0..$#a){$a[$_]+=$b[$_],say"$i,$_,".($s[$_]^=1)if$i==$a[$_]}}

在线尝试!


1

Haskell,121个字节

import Data.List
t!l=sort$(zip[0..]l)>>=takeWhile(\(a,_,_)->a<=t).(\(i,(d,w))->iterate(\(t,i,s)->(t+w,i,not s))(d,i,2>1))

在线尝试。

这是我开始的程序:

import Data.List

type LightId = Int
type Time = Int
type State = Bool
type LightEvent = (Time, LightId, State)

lightSimulation :: Time -> Time -> [(Time, State)]
lightSimulation delay interval = iterate step (delay, True)
  where step (time, state) = (time+interval, not state)

addId :: LightId -> (Time, State) -> LightEvent
addId id (t, s) = (t, id, s)

simulate :: Time -> [(Time, Time)] -> [LightEvent]
simulate timeLimit lights = sort $ concatMap lightSim (zip [0..] lights)
  where withinTimeLimit = ((<=timeLimit) . fst)
        lightSims (id, (delay, interval)) = map (addId id) $ takeWhile withinTimeLimit (lightSimulation delay interval)

在最后打高尔夫球之前,我将其缩短为:

import Data.List

light (id,(delay,interval)) = iterate step (delay, id, True)
  where step (time, id, state) = (time+interval, id, not state)

simulate timeLimit lights = sort $ concatMap lightSims (zip [0..] lights)
  where lightSims l = takeWhile(\(a,b,c)->a<=timeLimit)$light l

1

Röda105 87 85字节

{|t|enum|[([_+_]*(t-_1[0]+1))()|enum|(_+_)]|{[[_+_4,_3,_4//_2%2=0]]if[_4%_2=0]}|sort}

在线尝试!

说明:

{|t| /* Declare a lambda with one parameter */
/* The input stream contains arrays */
enum| /* For each array in the input, push an ascending number after it */
/* [1] (for stream content in this point, see below) */
[ /* For each array-number pair in the stream: */
    (
        [_+_] /* Create a copy of the array with the number as the last element */
        *(t-_1[0]+1) /* Create copies of the array for every ms simulated */
    )()| /* Push all copies to the stream */
    enum| /* After each copy, push an ascending number to the stream */
    (_+_) /* Append the number to each array before */
]|
/* [2] (for stream content in this point, see below) */
{
    /* Push an on or off event to the stream: */
    [[
        _+_4,      /* delay + time = actual time */
        _3,        /* light-id */
        _4//_2%2=0 /* does the light go on or off? */
    ]] 
    if[_4%_2=0] /* if the light goes on or off (time%period=0) */
}|
/* [3] (for stream content in this point, see below) */
sort /* Sort the events */
}

流包含[1]以下顺序的点值:

[delay, period], light-id
 _1[0]  _1[1]    _2

流包含[2]以下顺序的点值:

delay, period, light-id, time
_1     _2      _3        _4

流包含[3]具有以下结构的点数组:

[time, light-id, on_or_off]
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.