-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.py
168 lines (134 loc) · 4.83 KB
/
solve.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
class Card:
def __init__(self, colors, dirs):
self.top, self.right, self.bot, self.left = colors
self.top_dir, self.right_dir, self.bot_dir, self.left_dir = dirs
def rotate(self):
# top -> right (clockwise)
self.right, self.bot, self.left, self.top = (
self.top, self.right, self.bot, self.left)
self.right_dir, self.bot_dir, self.left_dir, self.top_dir = (
self.top_dir, self.right_dir, self.bot_dir, self.left_dir)
def __str__(self):
colors = [self.top, self.right, self.bot, self.left]
dirs = [self.top_dir, self.right_dir, self.bot_dir, self.left_dir]
#____________
#| ^ |
#| |
#| < < |
#| |
#|_____v_____|
#
#bl = 34
#br = 35
#or = 33
#gr = 32 vv replace 32 in thing
#print("\033[1;32;40m Bright Green \n")
colormap = {'bl':'34', 'br':'35', 'or':'33', 'gr':'32'}
dirmap = {'top_dir': {'i':'v','o':'^'},
'right_dir':{'i':'<','o':'>'},
'bot_dir': {'i':'^','o':'v'},
'left_dir': {'i':'>','o':'<'}}
topsym = '\033[1;' + colormap[self.top] + ';49m' + str(dirmap['top_dir'][self.top_dir])+'\033[1;39;49m'
rightsym = '\033[1;' + colormap[self.right] + ';49m' + str(dirmap['right_dir'][self.right_dir])+'\033[1;39;49m'
botsym = '\033[1;' + colormap[self.bot] + ';49m' + str(dirmap['bot_dir'][self.bot_dir])+'\033[1;39;49m'
leftsym = '\033[1;' + colormap[self.left] + ';49m' + str(dirmap['left_dir'][self.left_dir])+'\033[1;39;49m'
return '____________ \n'+\
'| '+topsym+' |\n'+\
'| |\n'+\
'| '+leftsym+' '+rightsym+' |\n'+\
'| |\n'+\
'|_____'+botsym+'_____|'
#return " ".join(c+"-"+d for c, d in zip(colors, dirs))
WIDTH = 3
HEIGHT = 3
EMPTY_BOARD = [None for _ in range(WIDTH*HEIGHT)]
def solve(cards, board = EMPTY_BOARD, pos = 0):
if len(cards) == 0:
return True, board
for i, c in enumerate(cards):
# place card
for _ in range(4):
if place(board, c, pos):
board[pos] = c
remaining_cards = cards[:i] + cards[i+1:]
# recurse
solved, board = solve(remaining_cards, board, pos + 1)
if solved:
# if success, return
return True, board
c.rotate()
# if we're here, we failed, undo, do next
board[pos] = None
# None worked, we failed
board[pos] = None
return False, board
counter = 0
def place(board, card, pos):
global counter
counter += 1
row, col = pos_to_rc(pos)
fits = True
if row > 0:
above_pos = rc_to_pos(row-1, col)
above = board[above_pos]
if above.bot != card.top or \
above.bot_dir == card.top_dir:
fits = False
if col > 0:
# check
left_pos = rc_to_pos(row, col-1)
left = board[left_pos]
if left.right != card.left or \
left.right_dir == card.left_dir:
fits = False
return fits
def rc_to_pos(row, col):
return WIDTH*row + col
def pos_to_rc(pos):
row = pos // WIDTH
col = pos % WIDTH
return (row, col)
def photorealism(board):
boardstr = ''
for row in range(HEIGHT):
rowstr = ''
for col in range(WIDTH):
pos = rc_to_pos(row, col)
card = str(board[pos])
if rowstr == '':
rowstr = card
else:
rowstr_lines = rowstr.split('\n')
card_lines = card.split('\n')
rowstr = ''
for row_line, card_line in zip(rowstr_lines, card_lines):
rowstr += row_line + card_line + "\n"
boardstr += rowstr
return boardstr
colors = [
['bl', 'br', 'bl', 'or'],
['br', 'bl', 'br', 'gr'],
['or', 'br', 'br', 'bl'],
['or', 'or', 'bl', 'gr'],
['br', 'or', 'bl', 'br'],
['or', 'bl', 'gr', 'or'],
['br', 'gr', 'bl', 'gr'],
['or', 'gr', 'gr', 'or'],
['bl', 'gr', 'gr', 'bl'],
]
dirs = [
['o', 'i', 'i', 'o'],
['i', 'i', 'o', 'o'],
['i', 'o', 'o', 'i'],
['i', 'o', 'o', 'i'],
['i', 'i', 'o', 'o'],
['i', 'o', 'o', 'i'],
['o', 'o', 'i', 'i'],
['i', 'i', 'o', 'o'],
['o', 'o', 'i', 'i'],
]
cards = [Card(cols, dirs) for cols, dirs in zip(colors, dirs)]
board = EMPTY_BOARD
s, board = solve(cards, board)
print(counter)
print(photorealism(board))