This repository was archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.py
212 lines (174 loc) · 5.01 KB
/
11.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from itertools import combinations
INPUT = [
['M2', 'M1'],
['G2'],
['G1'],
[]
]
# INPUT = [
# ['G1', 'M1'],
# ['G2', 'G3', 'G4', 'G5'],
# ['M2', 'M3', 'M4', 'M5'],
# []
# ]
# INPUT = [
# ['G1', 'M1', 'G6', 'M6', 'G7', 'M7'],
# ['G2', 'G3', 'G4', 'G5'],
# ['M2', 'M3', 'M4', 'M5'],
# []
# ]
NR_ITEMS = max(int(item[1:]) for row in INPUT for item in row)
SINGLE_COMBINATIONS = map(lambda x: 1 << x, range(NR_ITEMS*2))
print SINGLE_COMBINATIONS
DOUBLE_COMBINATIONS = map(sum, combinations(SINGLE_COMBINATIONS, 2))
print DOUBLE_COMBINATIONS
ALL_COMBINATIONS = DOUBLE_COMBINATIONS + SINGLE_COMBINATIONS
M_MASKS = [1 << (x*2 ) for x in range(NR_ITEMS)]
G_MASKS = [1 << (x*2 + 1) for x in range(NR_ITEMS)]
M_MASK = sum(M_MASKS)
G_MASK = sum(G_MASKS)
FLOOR_MASK = 1 << (NR_ITEMS*2)
def convertToBin(item):
t = item[0]
n = int(item[1:]) - 1
return 1 << (n*2 + 1) if t == 'G' else 1 << (n*2)
START = [sum(map(convertToBin, row)) for row in INPUT]
START[0] += FLOOR_MASK
START = tuple(START)
def isValidOptionRow(row):
gs = row & G_MASK
if gs == 0:
return True
ms = row & M_MASK
return notLogicalImplication((ms << 1), gs) == 0
def generateup(floor, option, movedItems):
cf = option[floor] - movedItems - FLOOR_MASK
if not isValidOptionRow(cf):
return None
uf = option[floor + 1] + movedItems
if not isValidOptionRow(uf):
return None
if floor == 0:
return (
cf,
uf + FLOOR_MASK,
option[2],
option[3]
)
if floor == 1:
return (
option[0],
cf,
uf + FLOOR_MASK,
option[3]
)
if floor == 2:
return (
option[0],
option[1],
cf,
uf + FLOOR_MASK
)
def generatedown(floor, option, movedItems):
cf = option[floor] - movedItems - FLOOR_MASK
if not isValidOptionRow(cf):
return None
df = option[floor - 1] + movedItems
if not isValidOptionRow(df):
return None
if floor == 1:
return (
df + FLOOR_MASK,
cf,
option[2],
option[3]
)
if floor == 2:
return (
option[0],
df + FLOOR_MASK,
cf,
option[3]
)
if floor == 3:
return (
option[0],
option[1],
df + FLOOR_MASK,
cf
)
def getFloor(option):
return next(i for i,v in enumerate(option) if v & FLOOR_MASK)
def notLogicalImplication(p, q):
return p & ~q
cache = set()
GOAL = FLOOR_MASK + FLOOR_MASK - 1
RESULT = None
def checkLocationAndReturnLocation(it, o):
global RESULT
RESULT = o if o[3] == GOAL else None
return RESULT
def flattenListIgnoreCache(l):
global cache
r = {item for sublist in l for item in sublist if item not in cache}
cache |= r
return r
graph = {}
def possibleOptions(option):
floor = getFloor(option)
row = option[floor]
possible_combinations = [x for x in ALL_COMBINATIONS if notLogicalImplication(x, row) == 0]
ups = [] if floor == 3 else filter(None, map(lambda x: generateup (floor, option, x), possible_combinations))
downs = [] if floor == 0 else filter(None, map(lambda x: generatedown(floor, option, x), possible_combinations))
# global graph
# rr = ups + downs
# for x in rr:
# if x not in graph:
# graph[x] = option
return ups + downs
def prettyPrint(option):
for i, o in enumerate(reversed(option)):
e, g5, m5, g4, m4, g3, m3, g2, m2, g1, m1 = '{0:{fill}11b}'.format(o, fill='0')
rs = 'F' + str(4 - i)
rs += ' E' if e != '0' else ' '
rs += ' G5' if g5 != '0' else ' '
rs += ' M5' if m5 != '0' else ' '
rs += ' G4' if g4 != '0' else ' '
rs += ' M4' if m4 != '0' else ' '
rs += ' G3' if g3 != '0' else ' '
rs += ' M3' if m3 != '0' else ' '
rs += ' G2' if g2 != '0' else ' '
rs += ' M2' if m2 != '0' else ' '
rs += ' G1' if g1 != '0' else ' '
rs += ' M1' if m1 != '0' else ' '
print rs
# print '{0:{fill}11b}'.format(o, fill='0').replace('0', ' ').replace('1', '#')
print
# options = [START]
# graph[START] = None
# it = 0
# while not any(checkLocationAndReturnLocation(it, o) for o in options):
# # noptions = []
# # for asdf in options:
# # po = possibleOptions(asdf)
# # for x in po:
# # if x not in graph:
# # graph[x] = asdf
# # noptions.append(po)
# # noptions = flattenListIgnoreCache(noptions)
# options = flattenListIgnoreCache(map(possibleOptions, options))
# # for x in noptions:
# # if x in graph:
# # graph[x] = options
# # options = noptions
# it += 1
# print it
# print RESULT
# print prettyPrint(RESULT)
# print prettyPrint(graph[RESULT])
# e = RESULT
# while e:
# prettyPrint(e)
# e = graph[e]
# print
# print it