Part of Advent of Code Golf 2021 event. See the linked meta post for details.
The story continues from AoC2015 Day 3, Part 2. This challenge was kindly contributed by Wheat Wizard (Grain Ghost).
Santa is delivering presents to an infinite two-dimensional grid of houses. The delivery begins delivering a present to the house at an arbitrary starting location, and then moving along a predetermined path delivering a new present at every step of the path. Moves are always exactly one house to the north ^
, south v
, east >
, or west <
.
However sometimes the notes giving the path have a few mistakes. Your job is to write a program that figures out how to correct these mistakes. We can't know exactly what mistakes were made but we do know that no house should ever receive more than 1 present. So we will just correct paths so that no house is visited more than once.
To correct a path we substitute a step with a different step. For example >^<v
can be corrected to >^>v
. Since we don't want to over-correct too much we will make the minimal number of corrections we can while reaching the desired result.
Task
Given a string representing directions output the minimal number of corrections required before it visits no house more than once.
This is code-golf so answers will be scored in bytes.
Test cases
>>v>>>vvv<<<^<v : 0
>>>>>><<<<<<< : 1
^^>>vv<< : 1
><> : 1
><>< : 2
^<v>^<v> : 2
^^>>vv<^^ : 1 (suggested by Yousername)
><<^<vv>>>>^^< : 2 (suggested by tsh)
^^>>vv<^^
(1 correction). \$\endgroup\$><<^<vv>>>>^^< : 2
. It only has one duplicate cell in the original path. But it has to be corrected by 2 modifications. \$\endgroup\$