Can Pac-Man Eat This String?
Retina, 55 38 bytes
i`^(([elp])|[^ghos]|(?<-2>.))*
$.&$*_<
Try it online! (The first line just allows running several test cases at once.)
Explanation
The problem is essentially to find the longest prefix that doesn't have an unmatched closing parenthesis. Except that we can use either e
, l
or p
in place of (
and either g
, h
, o
or s
in place of )
.
Hence, this solution is almost a textbook example of balancing groups. I won't go into too much detail about how they work, as this code is essentially the same as the standard example you can read up on in my SO answer on balancing groups.
The entire program is therefore a single regex substitution. The i
activates case-insensitivity. Then we either match a pellet with [elp]
and increment the depth counter (in the form of the capture stack of group 2
), or we match something that isn't a ghost with [ghos]
or we match a ghost with .
and decrement the depth counter by popping from stack 2
. Of course, in principle this allows matching a pellet with the [^ghos]
section or a non-ghost with the .
section, but thanks to greedy matching and the way the regex is backtracked, these possibilities are never attempted by the regex engine.
The substitution then uses two Retina specific features: $*
repeats the character to its right as many times as specified by the token on its left. That token is $.&
which is the length of the entire match. This just means that we replace each character in the match with a _
. And then we also append a <
to those underscores. The part of the input that isn't eaten simply remains unaffected by the substitution.
Jelly, 34 33 bytes
Œl“ʋʋ“ṁḍ»ċ€Ð€IF+\‘0ṭi0ð’”_×;”<;ṫ@
Try it online!
I think I'm finally starting to understand Jelly. Feels a bit scary.
Python 2, 114 113 108 bytes
s=raw_input()
p=i=0
for c in s:
p+=(c in'plePLE')-(c in'ghosGHOS')
if p<0:break
i+=1
print'_'*i+'<'+s[i:]