-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathsolve-16.py
79 lines (64 loc) · 1.96 KB
/
solve-16.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import json
import itertools
from icecream import ic
import time
class Board:
def __init__(self):
self.b = [[i * 4 + j for j in range(4)] for i in range(4)]
def _blkpos(self):
for i in range(4):
for j in range(4):
if self.b[i][j] == 15:
return (i, j)
def reset(self):
for i in range(4):
for j in range(4):
self.b[i][j] = i * 4 + j
def move(self, moves):
for m in moves:
i, j = self._blkpos()
if m == 'L':
self.b[i][j] = self.b[i][j - 1]
self.b[i][j - 1] = 15
elif m == 'R':
self.b[i][j] = self.b[i][j + 1]
self.b[i][j + 1] = 15
elif m == 'U':
self.b[i][j] = self.b[i - 1][j]
self.b[i - 1][j] = 15
else:
self.b[i][j] = self.b[i + 1][j]
self.b[i + 1][j] = 15
def __bool__(self):
for i in range(4):
for j in range(4):
if self.b[i][j] != i * 4 + j:
return True
return False
def run(bitlength, inputString):
board = Board()
filename = f'chals/b{bitlength}.json'
with open(filename) as f:
branches = json.load(f)
assert len(branches) == bitlength**2
for i in range(len(branches)):
if inputString[branches[i][0]] == '1':
board.move(branches[i][1])
else:
board.move(branches[i][2])
return board
startTime = time.time()
def main():
bitlength = 16
# generate 0000, ..., 1111
allPossibleInputs = [''.join(x) for x in itertools.product('01', repeat=bitlength)]
count = 0
for inputString in allPossibleInputs:
board = run(bitlength, inputString)
if board:
ic(inputString)
count += 1
duration = time.time() - startTime
if count % 1000 == 0:
ic(count, duration)
main()