Skip to content

Instantly share code, notes, and snippets.

@christian-oudard
Created September 4, 2020 17:46
Show Gist options
  • Save christian-oudard/7eab067e1686973558d399b746a92ea0 to your computer and use it in GitHub Desktop.
Save christian-oudard/7eab067e1686973558d399b746a92ea0 to your computer and use it in GitHub Desktop.
Brute force solution to level 47 in Black, by Bart Bonte.
numbers = [4, 6, 1, 3, 4, 4, 5, 3, 2, 3, 5, 2, 2, 4, 2, 6]
size = len(numbers)
path = []
def explore(pos):
num = numbers[pos]
print('explore', pos, num)
if pos in path:
print('already visited')
return False
else:
path.append(pos)
print()
print(''.join( ('X' if pos in path else 'O') for pos in range(size) ))
print('[ ', end='')
for pos in path:
print(pos, numbers[pos], end=' | ')
print()
print()
if len(path) >= size:
print('done')
return True
num = numbers[pos]
right = (pos + num) % size
left = (pos - num) % size
if explore(right):
return True
if explore(left):
return True
print('back out')
path.pop()
return False
for start in range(size):
if explore(start):
break
for pos in path:
print(pos, numbers[pos])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment