Python 3, 282 276276 241 bytes
Edit:Edit:
-6 bytes thanks to @ovs: rewritten ternary if if ... else
.
-10 bytes: m.append
replaced by +=[]
, and including the x+=
. range(4)
replaced by a list of 4 elemensts. else
-15 bytes: shortened the creation of list m
of translated codes: m
built with list comprehension, string built with join
.
-10 bytes: the creation of translated list m
is moved directly into the loop of counter. Used 2 variables to unpack the zip
.
def f(h):
m,t=[],[[0]*t=[[0]*-~len(h)for i in range(4)]
for w in h:
x=''
for q in zip(w,'012'*len(w)):x+=q[1]*(q[0]>'L'or 3)+'3'
x+='3'*6;m.append(x*450)in[0]*4]
for i in range(3600):
for n in'0123'[3*(t[2][-1]>4):]:t[int(n)][[c[i]for c in[(''.join(q*(p>'L'or 3)+'3'for p,q in m]zip(w,'012'*len(w)))+'3'*6)*450 for w in h]].count(n)]+=1
print(t[:3],t[3][::-1])
Original code explanationOriginal code explanation (at least I tried to explain)
def f(h):
Function definition, takes as input the list with the lighthouses' codes.m,t=[],[[0]*-~len(h)for i in range(4)]
Initialize the empty listm
that will contain the translated codes.
Initialize the list of counters: 4 lists (1 for each color and 1 for the total), each with n+1 lighthouses elements.for w in h:
Loop on the code of each lighthouse to translate into color codes.x=''
Initialize empty string for the translated code.for q in zip(w,'012'*len(w)):x+=q[1]*(1if q[0]>'L'else 3)+'3'
Translate the'SL'
sequences into color codes and the corresponding duration.
zip(w,'012'*len(w))
zips the the'SL'
code with the corresponding repeated color sequence ('0'=F, '1'=C, '2'=A).
For each'S'
,'L'
: replace with the color code (repeated for the seconds of short or long duration) and the off separation period ('3'=OFF).;x += lenght of color + 1s off
x+='3'*6;m.append(x*450)
Add the long off period (6s since 1s is already present) and extend the translated sequence for at least 1h.for i in range(3600):
Loop for 1h: for each second...for n in'0123'[3*(t[2][-1]>4):]:t[int(n)][[c[i]for c in m].count(n)]+=1
For each color and for the off (in '0123'
)...
'0123'[3*(t[2][-1]>4):]
count only the off periods (indexed last item of the string) if we get 5s of color A on all lighthouses at the same time.
[c[i]for c in m]
status of each lighthouse at second i.
count(n)
gives how many lighthouses have that statusn
; the count corresponds to the index in the list of the corresponding status; and increment the counter.
To summarize:t[index of color][index of how many are ON] += 1
print(t[:3],t[3][::-1])
Print the counter for the colors and the total ON period: this list must be reversed since in the previous step we counted the OFF periods.