Advertisement
Baidicoot

hex

Jun 17th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. str = """
  2.     L I P M O C
  3.    E R P O R Y A
  4.   X A O B J A C T
  5.  R E L I P M O C N
  6. S I E R R O R E T Y
  7. C Y P M O R Y J X O S
  8. S N Y G O G B L E R
  9.  U T R R C O M P U
  10.   N A R E R R O W
  11.    M X G I C P U
  12.     S J N X A L
  13.  
  14. PROGRAM
  15. COMPILER
  16. ERROR
  17. SYNTAX
  18. OBJECT
  19. """
  20.  
  21. def load_grid(str):
  22.     str = str.strip()
  23.     out = []
  24.     find = []
  25.     lines = iter(str.splitlines())
  26.     for l in lines:
  27.         l = l.strip()
  28.         if l == "":
  29.             break
  30.         else:
  31.             out.append(l.replace(" ", ""))
  32.     for l in lines:
  33.         find.append(l)
  34.     if len(out) % 2 == 0:
  35.         print("HEX MUST BE ODD")
  36.         return
  37.     sradius = (len(out)-1) // 2
  38.     d = len(out)
  39.     for i, l in enumerate(out):
  40.         if len(l) != d - abs(i-sradius):
  41.             print("LINE", i, "HAS INVALID LENGTH")
  42.     return out, sradius, d, find
  43.  
  44. grid, sradius, d, words = load_grid(str)
  45.  
  46. def out_of_bounds(x, y):
  47.     if (x < 0 or y < 0):
  48.         return True
  49.     elif (y >= len(grid)):
  50.         return True
  51.     elif (x >= len(grid[y])):
  52.         return True
  53.     else:
  54.         return False
  55.  
  56. def solve(x, y, word, nextfn):
  57.     if (out_of_bounds(x, y)):
  58.         return []
  59.     if (len(word) == 0):
  60.         return []
  61.     if (grid[y][x] == word[0]):
  62.         return [(x, y)] + solve(nextfn(x, y)[0], nextfn(x, y)[1], word[1:], nextfn)
  63.     else:
  64.         return []
  65.  
  66. e = lambda x, y : (x-1, y)
  67. w = lambda x, y : (x+1, y)
  68. ne = lambda x, y : (x if y <= sradius else x+1, y-1)
  69. se = lambda x, y : (x if y >= sradius else x+1, y+1)
  70. nw = lambda x, y : (x if y <= sradius else x-1, y-1)
  71. sw = lambda x, y : (x if y >= sradius else x-1, y+1)
  72.  
  73. def find(word, dirs=[e,w,ne,se,nw,sw]):
  74.     for x in range(d):
  75.         for y in range(d):
  76.             for direc in dirs:
  77.                 z = solve(x, y, word, direc)
  78.                 if (len(z) == len(word)):
  79.                     return z
  80.     return []
  81.  
  82. solutions = [find(word) for word in words]
  83. keep = [x for y in solutions for x in y]
  84.  
  85. out = []
  86.  
  87. for y, ys in enumerate(grid):
  88.     row = []
  89.     for x, ch in enumerate(ys):
  90.         if (x, y) in keep:
  91.             row.append(ch)
  92.         else:
  93.             row.append(".")
  94.     out.append(row)
  95.  
  96. for y, row in enumerate(out):
  97.     print(" "*abs(y-sradius), end="")
  98.     for ch in row:
  99.         print(ch+" ", end="")
  100.     print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement