Part of Advent of Code Golf 2021 event. See the linked meta post for details.
Related to AoC2016 Day 1, Part 1.
You're airdropped near Easter Bunny Headquarters in a city somewhere. "Near", unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further.
The Document indicates that you should start at the given coordinates (where you just landed) and face North East. Then, follow the provided sequence: turn right 60 degrees, then walk forward the given number of blocks, ending at a new intersection.
"What, 60 degrees?" You think, and you look around. Then you realize the city is not quite like what you imagine; the streets in this city form a triangular grid.
Anyway, there's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the street grid of the city, how far is the shortest path to the destination?
For example, if the instructions are [1, 1, 0, 2]
, it means to:
- Turn right 60 degrees and move 1 block forward
- Turn right 60 degrees and move 1 block forward
- Turn right 60 degrees and stay in place
- Turn right 60 degrees and move 2 blocks forward
In the end, you end up at just 1 block west of the start.
Input: A (possibly empty) list of non-negative integers. If you encounter a 0, it means to turn right 60 degrees but stay in place.
Output: A non-negative integer indicating how far away the destination is from the current position, measured in triangular taxicab distance.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
[] -> 0
[1, 1, 1] -> 2
[1, 1, 0, 2] -> 1
[1, 2, 0, 0, 0, 2, 1] -> 4
[1, 2, 3, 4] -> 6
[1, 1] -> 2
, my previous answer returns1
. \$\endgroup\$