Originally from caird coinheringaahing's idea, I (Bubbler) am hosting Advent of Code Golf 2021.
On each day from today (Dec 1) until Christmas (Dec 25), a challenge will be posted at UTC midnight, just like an Advent calendar. It is a free-for-all and just-have-fun-by-participation event, no leaderboards and no prizes for solving them fast or solving them in the shortest code. More details can be found in the link above.
For this year's event, the challenge ideas were drawn from the previous AoC events (2015 - 2020).
The story continues from AoC2015 Day 3, Part 2.
Santa is delivering presents to an infinite two-dimensional grid of houses. He begins by delivering a present to the house at the starting location, and then follows the commands written as a sequence of moves ^>v<
(moving to the neighboring house to the north, east, south, or west respectively), delivering a present at the house after each move.
Next year, Santa brings up a Robo-Santa for faster delivery. Santa and Robo-Santa start at the same location (giving two presents to the same house), and take turns to move based on the commands.
Assume the command is ^v^v^v^v^v
. Santa alone would deliver a bunch of presents to only two houses (the start and its north neighbor), but with a Robo-Santa, they would deliver presents to 11 different houses (Santa moving north 5 times and Robo-Santa moving south 5 times).
Now Santa wonders: Given a sequence of instructions, how many Santas (Santa and any Robo-Santas) are needed to deliver presents to the maximum number of houses? If two or more Robo-Santas are present, Santa and the Robo-Santas will take turns cyclically to follow the commands, everyone stopping when the commands run out (the number of commands does not have to be divisible by the number of Santas). If multiple options are available, choose the smallest number of Robo-Santas (it takes time and money to build them).
Input: A nonempty string of ^>v<
instructions.
Output: The minimum number of Santas in total for maximum delivery.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
>>>>> -> 1
(everyone moving to the same house is surely a loss)
^v^v^v^v^v -> 2
(one additional Robo-Santa will deliver to 11 different houses, the possible maximum)
v>^>v<^< -> 5
(5 Santas will deliver to 7 distinct houses;
1, 3, 4, or 7 Santas will deliver to 6 distinct houses
and other choices to fewer houses)
vv>^<^^<v> -> 1
(Any of 1, 4, 6, or 7 Santas will deliver to 8 distinct houses,
but 1 is the best choice out of them)