Part of Advent of Code Golf 2021 event. See the linked meta post for details.
The story continues from AoC2018 Day 3.
But you're not Bubbler, you're Lyxal! yeah, I know.
After a long period of chaos, the Elves have finally agreed on how to cut the fabric. Unfortunately, the next day they have come up with how to utilize the leftovers - make a super long present wrapping strip. Well, if it can shrink Santa, it might be able to shrink presents as well...
The description of the strip is a string of
ODCF, which happens to be the first letters of "up down left right" in Elvish (totally not this Elvish). Divide the fabric into a grid of 1cm × 1cm square cells, select a starting cell somewhere in the middle, and then move around according to the description to claim the strip.
So if the string is
OOCOFFFDD, you would get this strip, starting at X:
OFFF CO.D .O.D .X..
... Except that the strip the Elves gave to you is self-intersecting, so it simply doesn't work (a 1cm² fabric part doesn't magically become 2cm² - well, it's physics, at least until Santa comes).
Given the string
? is where the strip self-intersects:
OFFF CO.D C?CD DX..
In order to avoid the
? cell becoming a problem, you can take its substring (a contiguous part of the given string) in two ways:
OOCOFFFDDC (cutting away the last 3 chars) and
COFFFDDCCCD (cutting away the first 2 chars) gives, respectively,
OFFF CO.D .OCD .X.. OFFF CX.D CCCD D...
Among the two and all the other options, the latter of the two above is the longest possible.
Given a nonempty string of
ODCF, determine the length of its longest substring out of which you can make a valid (non-self-intersecting) strip. Assume the fabric leftover is large enough to cover any valid substring of the input.
Standard code-golf rules apply. The shortest code in bytes wins.
C -> 1 ODD -> 2 (DD) OOCOFFFDD -> 9 (whole input) OOCOFFFDDCCCD -> 11 (whole input minus first 2) OOOFFDCCCDFFFDCOOO -> 12 (minus first 3 and last 3)