ไฟกระพริบเมื่อไหร่?


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

@aschepler คุณหมายถึงอะไร อินพุตระบุระยะเวลาในการ "จำลอง"
Daniel M.

คำตอบ:


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=>นี้:
Shaggy

@Shaggy คุณเร็วมากฉันกำลังเตรียมการแก้ไข

Rule of thumb: ถ้ามี 2 อินพุตให้ใส่แกงกะหรี่เสมอ!
ขนดก


2

เจลลี่ ,  26  25 ไบต์

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

ลิงก์ dyadic รับรายการ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))

ลองออนไลน์!

รหัสนี้จะสร้างรายการที่ไม่เรียงลำดับซึ่งมีเวลาเปลี่ยนของแสงแต่ละแผ่นเวลาเหล่านั้นและตัวระบุแสงเรียงลำดับรายการดังกล่าวและส่งออกมัน


ตามกฎมาตรฐานคุณต้องป้อนข้อมูลคุณไม่สามารถคาดหวังว่ามันจะมีอยู่แล้วในตัวแปร คุณอาจจะพบว่าการใช้input()จะช่วยให้คุณสามารถตัดพวกเขาไบต์นับถอยหลังเกินไป (ไม่แยกสตริงจะต้องตั้งแต่หลาม 2 input()เป็นeval(raw_input())) :)
Jonathan Allan

... เช่นกันถ้าคุณใช้ตัวเลขมากกว่าสตริงในOพวกเขาจะเรียงลำดับซึ่งอาจจะเป็นการลดจำนวนไบต์ด้วย
Jonathan Allan

@JanathanAllan ขอบคุณสำหรับการสังเกตความแตกต่างของกฎ; ฉันจะไม่รวมข้อเสนอแนะของคุณเนื่องจากมีคำตอบ Python 2 ที่สั้นกว่าอย่างมากในขณะนี้
Jonathan Frech


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öda , 105 87 85 bytes

{|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]ค่าat point ตามลำดับต่อไปนี้:

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

สตรีมมี[2]ค่าat point ตามลำดับต่อไปนี้:

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

สตรีมที่มี[3]อาร์เรย์ที่จุดที่มีโครงสร้างดังต่อไปนี้:

[time, light-id, on_or_off]
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.