-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.py
118 lines (86 loc) · 2.8 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
#!/usr/bin/python3.5
import sys
import re
import copy
import time
from pprint import pprint
from itertools import combinations, repeat
from sortedcontainers import SortedList
#from operator import itemgetter
from collections import deque
locations = [
[0,1], #,'Polonium'],
[0,1], #,'Promethium'],
[0,0], #,'Thulium'],
[0,0], #,'Ruthenium'],
[0,0], #,'Cobalt']
[0,0], #,'Elerium']
[0,0], #,'Dilithium']
]
seen_states = SortedList()
bad_states = SortedList()
efloor = 0;
def state_valid(state):
# unprotected_chips = set([i[1] for i in state if i[0]!=i[1]])
generators = set([i[0] for i in state])
for i in state:
if i[0] != i[1] and i[1] in generators:
return False
return True
def floor_items(state,floor):
items = []
for i, val in enumerate(state):
if val[0] == floor:
items.append((i,0))
if val[1] == floor:
items.append((i,1))
return items
def new_state(state, nf, items):
s = copy.deepcopy(state)
for i in items:
s[i[0]][i[1]] = nf
return s
def state_final(state):
return all([ i[0] == 3 and i[1] == 3 for i in state])
def next_states(floor, cur_state, num_moves):
time_start = time.time()
checked = 0
new_states = deque([(floor, cur_state, num_moves)])
while new_states:
(floor, cur_state, num_moves) = new_states.popleft()
if floor == 0 :
possible_floors = [1]
elif floor == 3 :
possible_floors = [2]
else:
possible_floors = [floor-1, floor+1]
items = floor_items(cur_state, floor)
item_pairs = [[i] for i in items] + list(combinations(items, 2))
for f in possible_floors:
for its in item_pairs:
ns = sorted(new_state(cur_state, f, its))
checked += 1
if not checked%10000:
print(checked, num_moves, f, ns, len(new_states),'/',len(seen_states), '*', len(bad_states), time.time() - time_start)
if ns not in bad_states:
if (ns, f) not in seen_states:
seen_states.add((ns,f))
if state_valid(ns):
if state_final(ns):
print('final state:' , ns)
print('number of moves:', num_moves + 1)
return (num_moves + 1)
else:
new_states.append((f,ns, num_moves + 1))
else:
bad_states.add(ns)
print(next_states(efloor, locations, 0))
exit(0)
while True:
try:
line = sys.stdin.readline().rstrip()
print(line)
except KeyboardInterrupt:
break
if not line:
break