-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd11.py
180 lines (147 loc) · 5.04 KB
/
d11.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
inp = """The first floor contains a thulium generator, a thulium-compatible microchip, a plutonium generator, and a strontium generator.
The second floor contains a plutonium-compatible microchip and a strontium-compatible microchip.
The third floor contains a promethium generator, a promethium-compatible microchip, a ruthenium generator, and a ruthenium-compatible microchip.
The fourth floor contains nothing relevant."""
from copy import deepcopy
class Generator:
def __init__(self,t):
self.t = t
def __repr__(self):
return 'Gen ' + self.t
def __hash__(self):
return hash('G'+self.t)
def __eq__(self, x):
return type(x) is Generator and self.t == x.t
class Microchip:
def __init__(self,t):
self.t = t
def __repr__(self):
return 'Mic ' + self.t
def __hash__(self):
return hash('M'+self.t)
def __eq__(self, x):
return type(x) is Microchip and self.t == x.t
class Floor:
contents = set()
def __init__(self,n,contents=None):
self.n = n
if contents is not None:
self.contents = contents
def is_valid(self):
for ele in self.contents:
if isinstance(ele, Microchip):
if not self.check_microchip(ele):
return False
return True
def check_microchip(self, m):
t = m.t
for ele in self.contents:
if isinstance(ele, Generator) and ele.t != t:
return self.has_generator_t(t)
return True
def has_generator_t(self, t):
for ele in self.contents:
if isinstance(ele, Generator) and ele.t == t:
return True
return False
def __deepcopy__(self, memo):
return Floor(self.n, set(self.contents))
def __str__(self):
return str(self.contents)
def __repr__(self):
return self.__str__()
def __eq__(self, x):
return len(self.contents) == len(x.contents) and self.contents == x.contents
class Floors:
def __init__(self):
self.floors = [Floor(0), Floor(1), Floor(2), Floor(3)]
def __getitem__(self, n):
return self.floors[n]
def __repr__(self):
return ' '.join([repr(fl) for fl in self.floors])
def __str__(self):
return self.__repr__()
import sys
sys.setrecursionlimit(10000)
floors = Floors()
floors[0].contents = set([Generator('thulium'), Microchip('thulium'), Generator('plutonium'), Generator('strontium')])
floors[1].contents = set([Microchip('plutonium'), Microchip('strontium')])
floors[2].contents = set([Generator('promethium'), Microchip('promethium'), Generator('ruthenium'), Microchip('ruthenium')])
#floors[0].contents = set([Microchip('hydrogen'), Microchip('lithium')])
#floors[1].contents = set([Generator('hydrogen')])
#floors[2].contents = set([Generator('lithium')])
elevator_n = 0
import itertools
UP = 5
DOWN = 4
def poss_is_valid(p):
for ele in p:
if isinstance(ele, Microchip):
if not poss_check_microchip(p, ele):
return False
return True
def poss_check_microchip(p, el):
t = el.t
for ele in p:
if isinstance(ele, Generator) and ele.t != t:
return poss_has_generator_t(p,t)
return True
def poss_has_generator_t(p, t):
for ele in p:
if isinstance(ele, Generator) and ele.t == t:
return True
return False
def determine_new_fl(n,d):
if d == UP:
return n+1
return n-1
def attempt_solution(fl_o, n, tm, d):
new_n = determine_new_fl(n,d)
if (new_n < 0) or (new_n > 3):
return (False, None, None)
fl = Floors()
for i in range(4):
fl[i].contents = fl_o[i].contents.copy()
for i in tm:
fl[new_n].contents.add(i)
fl[n].contents.remove(i)
return ((fl[n].is_valid() and fl[new_n].is_valid()), fl, new_n)
def is_complete(fl):
print(sum([len(fl[x].contents) for x in range(3)]))
return all([len(fl[x].contents) == 0 for x in range(3)])
def is_dup(res, sol, n, already):
for i in list(res):
if already >= i[2] and i[1] == n and all([i[0][floor]==sol[floor] for floor in range(4)]):
return True
return False
done = []
res = set()
def get_min():
if done:
return min(done)
return 100000000
def determine_steps(fl, n, already=0):
if already >= get_min() or already >= 50:
return
if is_complete(fl):
print("Got solution")
print(fl,n,already)
done.append(already)
poss = []
for z in range(1,min(len(fl[n].contents)+1,3)):
poss.extend(itertools.combinations(fl[n].contents, z))
for p in poss:
if not poss_is_valid(p):
continue
for d in (UP, DOWN):
r = attempt_solution(fl, n, p, d)
if r[0]:
if is_complete(fl):
print(fl,n,already)
if is_dup(res, r[1], r[2], already+1):
continue
res.add((r[1],r[2], already+1))
determine_steps(r[1], r[2], already+1)
determine_steps(floors, 0)
print(done)
print(min(done))