Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- str = """
- L I P M O C
- E R P O R Y A
- X A O B J A C T
- R E L I P M O C N
- S I E R R O R E T Y
- C Y P M O R Y J X O S
- S N Y G O G B L E R
- U T R R C O M P U
- N A R E R R O W
- M X G I C P U
- S J N X A L
- PROGRAM
- COMPILER
- ERROR
- SYNTAX
- OBJECT
- """
- def load_grid(str):
- str = str.strip()
- out = []
- find = []
- lines = iter(str.splitlines())
- for l in lines:
- l = l.strip()
- if l == "":
- break
- else:
- out.append(l.replace(" ", ""))
- for l in lines:
- find.append(l)
- if len(out) % 2 == 0:
- print("HEX MUST BE ODD")
- return
- sradius = (len(out)-1) // 2
- d = len(out)
- for i, l in enumerate(out):
- if len(l) != d - abs(i-sradius):
- print("LINE", i, "HAS INVALID LENGTH")
- return out, sradius, d, find
- grid, sradius, d, words = load_grid(str)
- def out_of_bounds(x, y):
- if (x < 0 or y < 0):
- return True
- elif (y >= len(grid)):
- return True
- elif (x >= len(grid[y])):
- return True
- else:
- return False
- def solve(x, y, word, nextfn):
- if (out_of_bounds(x, y)):
- return []
- if (len(word) == 0):
- return []
- if (grid[y][x] == word[0]):
- return [(x, y)] + solve(nextfn(x, y)[0], nextfn(x, y)[1], word[1:], nextfn)
- else:
- return []
- e = lambda x, y : (x-1, y)
- w = lambda x, y : (x+1, y)
- ne = lambda x, y : (x if y <= sradius else x+1, y-1)
- se = lambda x, y : (x if y >= sradius else x+1, y+1)
- nw = lambda x, y : (x if y <= sradius else x-1, y-1)
- sw = lambda x, y : (x if y >= sradius else x-1, y+1)
- def find(word, dirs=[e,w,ne,se,nw,sw]):
- for x in range(d):
- for y in range(d):
- for direc in dirs:
- z = solve(x, y, word, direc)
- if (len(z) == len(word)):
- return z
- return []
- solutions = [find(word) for word in words]
- keep = [x for y in solutions for x in y]
- out = []
- for y, ys in enumerate(grid):
- row = []
- for x, ch in enumerate(ys):
- if (x, y) in keep:
- row.append(ch)
- else:
- row.append(".")
- out.append(row)
- for y, row in enumerate(out):
- print(" "*abs(y-sradius), end="")
- for ch in row:
- print(ch+" ", end="")
- print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement