Weathervane Walk
HardOnline Assessment
Track a courier who walks a ring of villages, where each village's weathervane decides his next direction, for up to 10^15 days.
A ring road has n villages numbered 0 to n - 1; village n - 1 is next to village 0. Every village has a weathervane pointing either clockwise (C, toward the next higher number, wrapping from n - 1 to 0) or anticlockwise (A, toward the next lower number, wrapping from 0 to n - 1).
The courier starts in village start. Each day, exactly this happens:
- He reads the weathervane of the village he is in and walks to the neighbouring village it points toward.
- Then the weathervane of the village he just left flips (
CbecomesA,AbecomesC). The weathervane of the village he arrived in is not touched.
For example, on a ring of 3 villages with vanes CCA, starting in village 0: day 1 he walks to village 1 and vane 0 flips (ACA); day 2 he walks to village 2 and vane 1 flips (AAA); day 3 he reads A at village 2, walks to village 1, and vane 2 flips (AAC).
After days days, report where he is, what every weathervane shows, and how many times he has arrived in each village during those days. The starting position at day 0 is not an arrival.
Input: vanes, a string of n characters C and A (the vane of village i is vanes[i]); start, the starting village; days, the number of days.
Output: a list [village, vanes, arrivals]: his final village (an integer), the final weathervane string in the same format as the input, and a list of n integers where entry i is the number of arrivals in village i.
Example 1
- Input:
- ["CCA",0,4]
- Output:
- [0,"ACC",[1,2,1]]
- Explanation
- Three villages, vanes
CCA, start 0, 4 days. Day 1: 0 to 1, vane 0 flips (ACA). Day 2: 1 to 2, vane 1 flips (AAA). Day 3: the vane at village 2 readsA, so 2 to 1, and vane 2 flips (AAC). Day 4: the vane at village 1 now readsA, so 1 to 0, and vane 1 flips (ACC). He is in village 0; arrivals are 1 at village 0, 2 at village 1 (days 1 and 3), 1 at village 2.
Example 2
- Input:
- ["ACAAC",3,0]
- Output:
- [3,"ACAAC",[0,0,0,0,0]]
- Explanation
- With
days = 0nothing happens: he stays in the start village, the vanes are unchanged, and there are no arrivals (the start is not an arrival).
Example 3
- Input:
- ["CCCC",0,8]
- Output:
- [0,"CCCC",[2,2,2,2]]
- Explanation
- Starting in village 0 of a ring of 4 with every vane
C, he walks all the way around in 4 days, flipping each vane toAas he leaves it (AAAA, one arrival everywhere). In the next 4 days he walks back the other way, flipping every vane back toC. After 8 days he is in village 0 again withCCCCand two arrivals in every village. From here the whole walk repeats every 8 days, which is the pattern to look for whendaysis huge.
Constraints
- 3 ≤ n = vanes.length ≤ 120; vanes contains only the characters C and A
- 0 ≤ start < n
- 0 ≤ days ≤ 1015