Skip to main content
shortened code, details of byte removed, improved explanation
Source Link
SevC_10
  • 469
  • 3
  • 8

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

Try it online!Try it online!

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 list m 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 status n; 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.

Python 3, 282 276 bytes

Edit:
-6 bytes thanks to @ovs: rewritten ternary if ... else

def f(h):
 m,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)
 for i in range(3600):
  for n in'0123'[3*(t[2][-1]>4):]:t[int(n)][[c[i]for c in m].count(n)]+=1
 print(t[:3],t[3][::-1])

Try it online!

Original code explanation (at least I tried)

  • 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 list m 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+='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 status n; the count corresponds to the index in the list of the corresponding status; and increment the counter.

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

Python 3, 282 276 241 bytes

Edit:
-6 bytes thanks to @ovs: rewritten ternary if ... else.
-10 bytes: m.append replaced by +=[], and including the x+=. range(4) replaced by a list of 4 elemensts.
-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):
 t=[[0]*-~len(h)for i 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 zip(w,'012'*len(w)))+'3'*6)*450 for w in h]].count(n)]+=1
 print(t[:3],t[3][::-1])

Try it online!

Original 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 list m 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 status n; 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.

modified code, removed 6 bytes
Source Link
SevC_10
  • 469
  • 3
  • 8

Python 3, 282282 276 bytes

Edit:
-6 bytes thanks to @ovs: rewritten ternary if ... else

def f(h):
 m,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]*(1if q[0]>'L'elseq[0]>'L'or 3)+'3'
  x+='3'*6;m.append(x*450)
 for i in range(3600):
  for n in'0123'[3*(t[2][-1]>4):]:t[int(n)][[c[i]for c in m].count(n)]+=1
 print(t[:3],t[3][::-1])

Try it online!Try it online!

CodeOriginal code explanation (at least I tried)

Python 3, 282 bytes

def f(h):
 m,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]*(1if q[0]>'L'else 3)+'3'
  x+='3'*6;m.append(x*450)
 for i in range(3600):
  for n in'0123'[3*(t[2][-1]>4):]:t[int(n)][[c[i]for c in m].count(n)]+=1
 print(t[:3],t[3][::-1])

Try it online!

Code explanation (at least I tried)

Python 3, 282 276 bytes

Edit:
-6 bytes thanks to @ovs: rewritten ternary if ... else

def f(h):
 m,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)
 for i in range(3600):
  for n in'0123'[3*(t[2][-1]>4):]:t[int(n)][[c[i]for c in m].count(n)]+=1
 print(t[:3],t[3][::-1])

Try it online!

Original code explanation (at least I tried)

Source Link
SevC_10
  • 469
  • 3
  • 8

Python 3, 282 bytes

I managed to get the byte count under 300, I'm quite happy with that :).

def f(h):
 m,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]*(1if q[0]>'L'else 3)+'3'
  x+='3'*6;m.append(x*450)
 for i in range(3600):
  for n in'0123'[3*(t[2][-1]>4):]:t[int(n)][[c[i]for c in m].count(n)]+=1
 print(t[:3],t[3][::-1])

Try it online!


Code explanation (at least I tried)

  • 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 list m 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+='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 status n; the count corresponds to the index in the list of the corresponding status; and increment the counter.

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