6
\$\begingroup\$

To celebrate the island's next Founder's Day, colored filters (fuchsia, chartreuse, and aquamarine*) have been installed in every lighthouse. All lighthouses begin operation with their fuchsia filter, and they are all on the same color cycle: FCA, FCA, ... Each lighthouse changes its color filter after every ON blink, whether long or short. In addition, each starts the cycle anew with fuchsia each time its code begins.

Other than the addition of color, lighthouse operation is identical to that in the original challenge, Blinking Lighthouses.

Challenge: Input is identical to the original challenge: the codes for each of n lighthouses in the form of strings of (only) L's and S's.

Output now consists of four lines, one line per color, plus a "totals" line. The top line has the total times that 0...n fuchsia lights were simultaneously on; the second line chartreuse; and the third line aquamarine. Thus the third number in the second line is the total seconds that exactly two chartreuse lights were simultaneously on regardless of what other colors were on during those seconds. The fourth line has the totals for 0...n lights simultaneously on regardless of color (this line is identical to the output of the original challenge).

Unfortunately, Chief of Lighthouse Operations Marie Lumenix has reported a glitch during filter testing. For some reason, if all lighthouses have simultaneously blinked aquamarine at the same time for a total of 5 seconds after the start of operation (the last item in line 3 of output), the electronic color filter system immediately fails and all lighthouses continue their schedules for the rest of the hour blinking white. This glitch will not affect the last, "totals", line of output, but may affect the first three rows, which each total the number of seconds only for the period that filters were working.**

Rules The rules are otherwise the same as in the original challenge. Shortest code in bytes wins.

Just for clarification: the first number in the last row is the total seconds that all lighthouses were off regardless of color. The last row should sum to 3600 sec as in the original challenge, or 1 hr of lighthouse operation. The sums for each row, 1 to 3, should match, whether or not they each total 3600. These three rows stop counting after a glitch.

Test Cases for the broken system

(Input -> Output)

['SLS', 'SLL', 'SSS', 'LSL'] ->
  
[1178, 360, 90, 40, 5]
[998, 470, 165, 40, 0]
[1178, 165, 270, 55, 5]
[1125, 890, 652, 590, 343]
    

['SLS', 'LLS', 'SSSL'] ->
    
[2121, 1158, 288, 33] 
[2155, 1219, 226, 0] 
[2957, 579, 64, 0] 
[1254, 1125, 577, 644] 

(The color schedules, in seconds, for the 3 lighthouses in test case #2 case are:

F-CCC-A-------  [repeat]
FFF-CCC-A------- [repeat]
F-CCC-A-FFF------- [repeat]

each lighthouse begins the FCA color cycle anew each time its code repeats.)


['SSSLLLSSS', 'LLSSSLLSSLS', 'LSLSLL', 'SSLLL', 'SLLSSSL'] ->

[1334, 1115, 548, 223, 57, 8] 
[1560, 1047, 463, 163, 44, 8] 
[1591, 950, 525, 184, 30, 5] 
[484, 653, 657, 553, 553, 700] 

*The colors of Lighthouse Island's flag, chosen by a referendum held at Aloisius' Tavern and Gift Shop.
**Marie feels absolutely terrible about this and promises to get things shipshape by Founder's Day.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ For the first three rows, the first number (no lights on of that color) covers only the time up until a glitch. Item 1 of the last row (all lights off), however continues to tally "all off" until the hour is up. For the first test case, I get a glitch occurring after 1673 seconds (the sum of the first row). All lighthouses continue to blink their code (in white) until the hour is up. Does that work? \$\endgroup\$
    – DjinTonic
    Jul 17 at 14:48
  • 1
    \$\begingroup\$ I say "the electronic color filter system immediately fails and all lighthouses continue their schedules for the rest of the hour blinking white. " and "This glitch will not affect the last, "totals", line of output." But I will add a clarification -- thanks \$\endgroup\$
    – DjinTonic
    Jul 17 at 14:57
  • \$\begingroup\$ You're right, I missed [...] which now each total the number of seconds that filters were working \$\endgroup\$
    – ovs
    Jul 17 at 14:59
  • \$\begingroup\$ No bother. My schedules match yours, but I'm getting different numbers. Could you please post in a comment your output for test case #2 (and #3 if we disagree there too). I'll work on my code. Thank you! \$\endgroup\$
    – DjinTonic
    Jul 17 at 16:14
  • 1
    \$\begingroup\$ I've edited the challenge and believe I've address the ambiguities that were raised in the comments and chat room. The 3 test cases are correct as is. Thanks! \$\endgroup\$
    – DjinTonic
    Jul 17 at 19:29
4
\$\begingroup\$

J, 129 bytes

<@(0,#\)(<:@(#/.~@,+/))&>[:(<@(+/),~]<@({.&.|:"2)~[:{:5{.!._[:>:@I.*/@{:)1 2 3=/(3600$(6$0),~&;[:(*&.>1 2 3$~$)19<@#:@|14+3&u:)&>

Try it online!

\$\endgroup\$
0
4
\$\begingroup\$

05AB1E, 52 bytes

Based on my answer to the previous challenge.

ε€C9%Å10.ý˜7Å0«Dη_O3%>*60n∍}øÐ3QP.¥5‹Ï3LδQøs<dªεOZÝ¢

Try it online!

ε€C9%Å10.ý˜7Å0«        60n∍}   # see my previous answer
               Dη              # push all prefixes of the signal
                 _O            # count the number of 0's (how often the signal was off)
                   3%>         # modulo 3, add 1
                      *        # multiply each value in the signal list (0 or 1) with this
                               # this replaces all 1's with the correct color: (1=F, 2=C, 3=A)

øÐ                             # tranpose the list (n × 3600 -> 3600 × n) and push the result three times
  3QP                          # on the first copy, check for each second if all lighthouses are aquamarine
     .¥                        # take the sums of prefixes of this list,
                               # this keeps track of how many times all lighthouses were aquamarine so far
       5‹                      # for each second: is this value less than 5?
         Ï                     # take the lighthouse for all seconds where this was true
          3L                   # push the range [1, 2, 3]
            δQø                # for each of those numbers (colors) compare the lighthouse colors to it
               s<dª            # append a boolean matrix of activations in any color (>0)
                   ε           # for each of those 4 boolean matrices:
                    O          # sum each row / second
                     ZÝ        # get the range [0 .. maximum]
                       ¢       # and count all these values

The first half of this can be used to visualize the color pattern: Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ -1 byte by golfing the same as in your other answer; and -1 byte by replacing <d with Ā. \$\endgroup\$ Aug 24 at 9:14
4
\$\begingroup\$

Jelly, 55 bytes

ŻI»0Ä’%3‘×
Ø0œịŻṖÄ<5a
O%9ÄṬ;Øỵ¬Ñṁ⁽½c)Zċþ3Sṭ$=þ@LŻ$ṪṭÇƊ§

Try it online!

A full program taking a list of strings and printing a list of lists of integers.

The first part (converting from Ss and Ls to the sequence dir each lighthouse was derived from @JonathanAllan’s answer to the previous challenge.

Explanation

Convert a list of 0s and 1s to a list of 0s, 1s, 2s and 3s

Ż          | Prepend 0
 I         | Increments
  »0       | Max of 0 and this (i.e. replace -1 with 0)
    Ä      | Cumulative sum
     ’     | Add 1
      %3   | Mod 3
        ‘  | Subtract 1
         × | Times the input

Replace all of the values in the lists of F, C and A with 0 once the last list of the list list provided as the argument. Argument referred to as x.

Ø0œị       | x[0][0]
    Ż      | Prepend 0
     Ṗ     | Remove last item
      Ä    | Cumulative sum
       <5  | Less than 5
         a | Logical and with x (vectorises)
              )                     | For each lighthouse list:
O                                   | - Convert each character to UTF8 codepoints
 %9                                 | - Mod 9
   Ä                                | - Cumulative sum
    Ṭ                               | - Untruthy
     ;Øỵ                            | - Append "aeiouy"
        ¬                           | - Logical not
         Ñ                          | - Call helper link 1
          ṁ⁽½c                      | - Mould to 3600
               Z                    | Transpose
                ċþ3                 | Outer function using count and the numbers [1,2,3]
                     $              | Following as a monad:
                   S                | - Sum (because of vectorisation, will add the F, C and A values together)
                    ṭ               | - Tag onto the end of the F,C,A list
                      =þ@LŻ$        | Outer function using equals (with arguments reversed) and using [0,1,..,n] as the right argument
                               Ɗ    | Following as a monad:
                            Ṫ       | - Tail
                             ṭ      | - Tag onto:
                              Ç     |   - Result of calling helper link 2 on the remaining lists
                                §   | Sum of innermost lists
\$\endgroup\$
2
\$\begingroup\$

Python 2, 203 200 bytes

E=enumerate
l=input()
t=eval(`[[0]*-~len(l)]*4`)
for i in range(3600):
 for y,q in E(t):q[sum(y==((sum([~ord(w)%5*[j%3]+[3]for j,w in E(c)],[])+[3]*6)*450)[i]for c in l)^0-y/3]+=y*y|5>t[2][-1]
print t

Try it online!

Building the pattern with strings instead of lists of digits comes out at the same length and is a bit faster: Try it online!

\$\endgroup\$
2
\$\begingroup\$

Jelly, 51 bytes

Ẓ&/Ä<5S‘x3ɓBSZḊḣ"
O%9Ṭ€;Øỵ¬a"“µ¦¤‘ṁ$+⁴Fṁ⁽½c)ÇċⱮ€LŻ$

A monadic Link that accepts the list of patterns and yields the list of lists of counts.

Try it online!

How?

Ẓ&/Ä<5S‘x3ɓBSZḊḣ" - Link 1: list of lists of colours at each second, L
                            where F=25, C=21, A=19, and off=16
Ẓ                 - is prime? (i.e. is Aquamarine?)
  /               - reduce using:
 &                -   bitwise AND -> 1 if all Aquamarine else 0
   Ä              - cumulative sums
    <5            - less than five?
      S           - sum
       ‘          - increment -> second, s, when filtering breaks (or 3601)
        x3        - repeat (that) three times -> [s,s,s]
          ɓ       - new dyadic chain, f(L, [s,s,s]):
           B      -   convert (L) to binary i.e. each number -> [1, F?, C?, A?, on?]
            S     -   sum columns
             Z    -   transpose
              Ḋ   -   dequeue (discard the leading-bit result) 
                " -   (that) zip ([s,s,s]) with:
               ḣ  -     head to index  (this leaves the fourth list intact)

O%9Ṭ€;Øỵ¬a"“µ¦¤‘ṁ$+⁴Fṁ⁽½c)ÇċⱮ€LŻ$ - Main Link: list of patterns
                         )        - for each pattern:
O%9Ṭ€                             -   convert 'S' to [0,1] and 'L' t [0,0,0,1]
     ;Øỵ                          -   concatenate "aeiouy"
        ¬                         -   logical NOT -> unflattend on/off seconds, U
                 $                -   last two links as a monad, f(pattern):
           “µ¦¤‘                  -     [9,5,3]
                ṁ                 -     mould like (patten) - e.g. 'SLSS' -> [9,5,3,9]
          "                       -   (U) zip (that) with:
         a                        -     logical AND -> 1s become 9 (F), 5 (C), or 3 (A)
                  +⁴              -   add 16 -> 25 (F), 21 (C), 19 (A), or 16 (off)
                    F             -   flatten
                     ṁ⁽½c         -   mould like [1..3600] 
                          Ç       - call last Link (Link 1) as a monad, f(that)
                                $ - last two links as a monad, f(patterns):
                              L   -   length
                               Ż  -   zero-range -> [0..length(patterns)]
                           ċⱮ€    - for each map with count occurrences
\$\endgroup\$
2
\$\begingroup\$

Python 3, 282 276 241 bytes

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

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 1if q[0]>'L'else 3 can be shortened to q[0]>'L'or 3 as 1 and True are basically the same. \$\endgroup\$
    – ovs
    Jul 26 at 15:50
  • \$\begingroup\$ Thank you! Even if I'm still far away from your score! \$\endgroup\$
    – SevC_10
    Jul 26 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.