Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear and Simple (and fast too!) solution in Clear category for Hypercube by BrianMcleod
from collections import defaultdict
def hypercube(grid):
"""
Detect wgether there is a path that spells Hypercube in the grid
Since True or false is the output, only the tails of successful paths need to be retained between
iterative tests. No tests for used letters is needed because all of the letters in 'Hypercube' are
unique except 'e' occting at positions 3 and 8. Since the two occurances are in odd and even positions
and movement os limited yo up/down/lwft/right, the two 'e's cannot be the same.
Simply check for all matches with the current letter in "Hypercube" and retain only those that are
adjacent to at least one of the tails. If at the end, there are any tails left, the result is True.
"""
word = "hypercube"
cells = defaultdict(list)
for x, row in enumerate(grid):
for y, c in enumerate(row):
cells[c.lower()].append((x,y))
tails = cells[word[0]]
for c in word[1:]:
tails = [cell for cell in cells[c] if any([sum([abs(a-b) for a,b in zip(cell,pt)])==1 for pt in tails])]
return bool(tails)
Sept. 27, 2018