Last active
July 17, 2018 05:53
-
-
Save crvdgc/0c2f97be2e05a8b222b75bf83f16723a to your computer and use it in GitHub Desktop.
To the Moon, in-game memento solver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
def solve_memento(): | |
rowNum = int(input('Row number: ')) | |
colNum = int(input('Col number: ')) | |
best = int(input('Best move: ')) | |
print("Input table, 1 for solved, 0 for unsolved") | |
table = [list(map(lambda x: True if int(x) == 1 else False, input().split())) for i in range(rowNum)] | |
def check(): | |
return all([all(row) for row in table]) | |
def flip(i, j): | |
table[i][j] = False if table[i][j] else True | |
def apply_solution(solution): | |
for r in range(rowNum): | |
if solution[r]: | |
for c in range(colNum): | |
flip(r, c) | |
for c in range(colNum): | |
if solution[rowNum+c]: | |
for r in range(rowNum): | |
flip(r, c) | |
if solution[-1]: | |
# diagnol, from left-bottom | |
for i in range(min(rowNum, colNum)): | |
flip(rowNum-1-i, i) | |
def print_solution(solution): | |
for r in range(rowNum): | |
if solution[r]: | |
print('r%s' % r) | |
for c in range(colNum): | |
if solution[rowNum+c]: | |
print('c%s' % c) | |
if solution[-1]: | |
print('d') | |
for solution in itertools.product([False, True], repeat=rowNum+colNum+1): | |
if solution.count(True) > best: | |
continue | |
apply_solution(solution) | |
if check(): | |
print_solution(solution) | |
break | |
else: | |
apply_solution(solution) | |
else: | |
print('No answer for best move %s' % best) | |
solve_memento() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment