रोशनी कब झपकती है?


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

लाइटस्टैटस एक सत्य मूल्य है यदि प्रकाश चालू होता है और यदि प्रकाश बंद हो जाता है तो एक मिथ्या मूल्य।

उपरोक्त उदाहरण से आउटपुट होगा:

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

लीडरबोर्ड स्निपेट:


कितना आउटपुट पर्याप्त है?
22

@aschepler क्या मतलब है आपका? इनपुट "अनुकरण" के लिए समय की एक राशि निर्दिष्ट करता है
डैनियल एम।

जवाबों:


3

जावास्क्रिप्ट, 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 के लिए एक बाइट धन्यवाद सहेजा गया - करी इनपुट सिंटैक्स का उपयोग करें।


एक बाइट को करीने से सहेजें a=>b=>:।
झबरा

@Shaggy। आप बहुत तेज़ हैं, मैं एक संपादन तैयार कर रहा था।

अंगूठे का नियम: यदि 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

पायथन 2 , 206 214 बाइट्स

  • नियमों का पालन करने के लिए आठ बाइट्स जोड़े गए (स्टड के माध्यम से इनपुट लेते हुए)।
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())) :)।
जोनाथन एलन

... यह भी कि अगर आप संख्याओं का उपयोग करते हैं, तो Oवे क्रमबद्ध रूप से बाइट काउंट में कटौती करेंगे,
जोनाथन एलन

@JonathanAllan नियम की विसंगति को नोटिस करने के लिए धन्यवाद; मैं आपके सुझावों को शामिल नहीं करूंगा क्योंकि अभी तक काफी कम अजगर 2 का जवाब है।
जोनाथन फ्रीच


1

हास्केल, 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 बाइट्स

{|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]
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.