Lighthouse light pattern
EasyOnline Assessment
Turn a lighthouse's chart abbreviation into the second-by-second pattern of its lamp.
Charts describe a light as TYPE, then an optional group list in parentheses, then an optional colour letter, then the repeat period, for example Fl(2+1) W 12s. The pattern covers exactly one period, one character per second: "1" when the lamp is lit, "0" when dark.
Fl(flash): every flash is 1 lit second.LFl(long flash): every flash is 3 lit seconds.- Every flash is followed by 1 dark second. When the group list has several groups, such as
(2+1), there is one extra dark second between consecutive groups. With no group list there is a single flash. After the last flash the lamp stays dark until the period ends. Oc(occulting): take the pattern the same group list would give forFl, then swap every"1"and"0".Iso(isophase): never has a group list. The lamp is lit for the first half of the period and dark for the second half, so the period must be even.- The colour letter (
W,R,GorY) never changes the pattern.
If the flashes (each together with its dark second) and the extra between-group seconds need more seconds than the period provides, or if an Iso period is odd, the characteristic is impossible: return the empty string. A pattern that fits the period exactly is fine.
Input: characteristic, a string such as "Fl(3) R 15s", "LFl 10s", "Oc(2+2) 20s" or "Iso 8s". Parts are separated by single spaces, except that the group list follows the type directly.
Output: a string of "1" and "0" characters whose length equals the period, or "" if the characteristic is impossible.
Example 1
- Input:
- ["Fl(2+1) W 12s"]
- Output:
- "101001000000"
- Explanation
- Two groups, so flashes are
1 0 1 0(first group), one extra dark second, then1 0(second group). That is 7 seconds, and the remaining 5 seconds of the 12-second period are dark. The colour letterWis ignored.
Example 2
- Input:
- ["Oc(2) 6s"]
- Output:
- "010111"
- Explanation
Oc(2) 6sstarts from the flash pattern ofFl(2) 6s, which is101000, and swaps every lit second with dark and vice versa:010111.
Example 3
- Input:
- ["Iso 7s"]
- Output:
- ""
- Explanation
- An isophase light needs equal lit and dark halves, which is impossible when the period is 7 seconds, so the answer is the empty string.
Constraints
characteristicis well-formed: a type, an optional group list, an optional single-letter colour, then the period1 <= period <= 120- each group size is between
1and9, and there are at most4groups