Skip to content

Instantly share code, notes, and snippets.

@Q726kbXuN
Created August 8, 2022 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Q726kbXuN/a56bde5b7ea0e6fe1531584851a8b771 to your computer and use it in GitHub Desktop.
Save Q726kbXuN/a56bde5b7ea0e6fe1531584851a8b771 to your computer and use it in GitHub Desktop.
from collections import deque
grid = [3,3,1,3,2,2,0,0,2,1,2,0,0,0,0,0,1,1,3,0,3,3,1,0,3]
todo = deque([(grid, [])])
seen = set()
while True:
grid, trail = todo.pop()
if sum(grid) == -25:
print(trail)
exit(0)
for x_col in range(5):
target = grid[x_col + 20]
if target != -1:
temp = grid[:]
flood = deque([(x_col, 4)])
while len(flood):
x, y = flood.pop()
temp[x+y*5] = -1
for xo, yo in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
if 0 <= x+xo <= 4 and 0 <= y+yo <= 4 and temp[x+xo+(y+yo)*5] == target:
flood.append((x+xo,y+yo))
for x in range(5):
col = [temp[x+y*5] for y in range(4,-1,-1)]
if sum(col) != -5:
col = [i for i in col if i >= 0]
col = col + [-1] * (5 - len(col))
for i, val in enumerate(col):
temp[x + (4 - i) * 5] = val
if tuple(temp) not in seen:
seen.add(tuple(temp))
todo.appendleft((temp, trail + [x_col]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment