1
+ # UNSOLVED
2
+
3
+ from time import sleep
4
+
5
+ f = open ("29sample.in" )
6
+ walls = []
7
+ boxes = []
8
+ movements = []
9
+ bot_pos = (0 ,0 )
10
+ for y , line in enumerate (f .readlines ()):
11
+ for x , char in enumerate (line ):
12
+ if char == "#" :
13
+ walls .append ((x ,y ))
14
+ elif char == "O" :
15
+ boxes .append ((x ,y ))
16
+ elif char == "@" :
17
+ bot_pos = (x ,y )
18
+ elif char == "\n " or char == "." :
19
+ continue
20
+ else :
21
+ movements .append (char )
22
+
23
+ for i in range (len (walls )):
24
+ walls .append ((walls [i ][0 ]* 2 + 1 , walls [i ][1 ]))
25
+ walls [i ] = (walls [i ][0 ]* 2 , walls [i ][1 ])
26
+
27
+ for i in range (len (boxes )):
28
+ boxes [i ] = (boxes [i ][0 ] * 2 , boxes [i ][1 ])
29
+
30
+ bot_pos = (bot_pos [0 ] * 2 , bot_pos [1 ])
31
+
32
+ push_stack = []
33
+
34
+ def do_push (x , y , movement = None , box = False ):
35
+ global walls , boxes , movements , bot_pos , push_stack
36
+ if movement == None :
37
+ movement = movements .pop (0 )
38
+ if movement == "<" :
39
+ if (x - 2 , y ) in walls :
40
+ return False
41
+ if (x - 2 , y ) in boxes :
42
+ if do_push (x - 2 , y , movement , True ):
43
+ boxes [boxes .index ((x - 2 , y ))] = (x - 3 , y )
44
+ return True
45
+ else :
46
+ return False
47
+ else :
48
+ return True
49
+ if movement == "^" :
50
+ if not box :
51
+ if (x , y - 1 ) in walls :
52
+ return False
53
+ if (x , y - 1 ) in boxes :
54
+ if do_push (x , y - 1 , movement , True ):
55
+ boxes [boxes .index ((x , y - 1 ))] = (x , y - 2 )
56
+ return True
57
+ else :
58
+ return False
59
+ if (x - 1 , y - 1 ) in boxes :
60
+ if do_push (x - 1 , y - 1 , movement , True ):
61
+ boxes [boxes .index ((x - 1 , y - 1 ))] = (x - 1 , y - 2 )
62
+ return True
63
+ else :
64
+ return False
65
+ return True
66
+ else :
67
+ if (x , y - 1 ) in walls or (x + 1 , y - 1 ) in walls :
68
+ for item in push_stack :
69
+ boxes [boxes .index (item )] = (item [0 ], item [1 ] + 1 )
70
+ return False
71
+ if (x , y - 1 ) in boxes :
72
+ if do_push (x , y - 1 , movement , True ):
73
+ boxes [boxes .index ((x , y - 1 ))] = (x , y - 2 )
74
+ push_stack .append ((x , y - 2 ))
75
+ return True
76
+ else :
77
+ return False
78
+ pushed_left = None
79
+ pushed_right = None
80
+ if (x - 1 , y - 1 ) in boxes :
81
+ pushed_left = do_push (x , y - 1 , movement , True )
82
+ if (x + 1 , y - 1 ) in boxes :
83
+ pushed_right = do_push (x + 1 , y - 1 , movement , True )
84
+ if pushed_left and pushed_right :
85
+ boxes [boxes .index ((x - 1 , y - 1 ))] = (x - 1 , y - 2 )
86
+ push_stack .append ((x - 1 , y - 2 ))
87
+ boxes [boxes .index ((x + 1 , y - 1 ))] = (x + 1 , y - 2 )
88
+ push_stack .append ((x - 1 , y - 2 ))
89
+ return True
90
+ elif pushed_left == None and pushed_right == None :
91
+ return True
92
+ else :
93
+ return False
94
+ if movement == ">" :
95
+ if not box :
96
+ if (x + 1 , y ) in walls :
97
+ return False
98
+ if (x + 1 , y ) in boxes :
99
+ if do_push (x + 1 , y , movement , True ):
100
+ boxes [boxes .index ((x + 1 , y ))] = (x + 2 , y )
101
+ return True
102
+ else :
103
+ return False
104
+ else :
105
+ return True
106
+ else :
107
+ if (x + 2 , y ) in walls :
108
+ return False
109
+ if (x + 2 , y ) in boxes :
110
+ if do_push (x + 2 , y , movement , True ):
111
+ boxes [boxes .index ((x + 2 , y ))] = (x + 3 , y )
112
+ return True
113
+ else :
114
+ return False
115
+ else :
116
+ return True
117
+ if movement == "v" :
118
+ if not box :
119
+ if (x , y + 1 ) in walls :
120
+ return False
121
+ if (x , y + 1 ) in boxes :
122
+ if do_push (x , y + 1 , movement , True ):
123
+ boxes [boxes .index ((x , y + 1 ))] = (x , y + 2 )
124
+ return True
125
+ else :
126
+ return False
127
+ if (x - 1 , y + 1 ) in boxes :
128
+ if do_push (x - 1 , y + 1 , movement , True ):
129
+ boxes [boxes .index ((x - 1 , y + 1 ))] = (x - 1 , y + 2 )
130
+ return True
131
+ else :
132
+ return False
133
+ return True
134
+ else :
135
+ if (x , y + 1 ) in walls or (x + 1 , y + 1 ) in walls :
136
+ for item in push_stack :
137
+ boxes [boxes .index (item )] = (item [0 ], item [1 ] - 1 )
138
+ return False
139
+ if (x , y + 1 ) in boxes :
140
+ if do_push (x , y + 1 , movement , True ):
141
+ boxes [boxes .index ((x , y + 1 ))] = (x , y + 2 )
142
+ push_stack .append ((x , y + 2 ))
143
+ return True
144
+ else :
145
+ return False
146
+ pushed_left = None
147
+ pushed_right = None
148
+ if (x - 1 , y + 1 ) in boxes :
149
+ pushed_left = do_push (x , y + 1 , movement , True )
150
+ if (x + 1 , y + 1 ) in boxes :
151
+ pushed_right = do_push (x + 1 , y + 1 , movement , True )
152
+ if pushed_left and pushed_right :
153
+ boxes [boxes .index ((x - 1 , y + 1 ))] = (x - 1 , y + 2 )
154
+ push_stack .append ((x - 1 , y + 2 ))
155
+ boxes [boxes .index ((x + 1 , y + 1 ))] = (x + 1 , y + 2 )
156
+ push_stack .append ((x - 1 , y + 2 ))
157
+ return True
158
+ elif pushed_left == None and pushed_right == None :
159
+ return True
160
+ else :
161
+ return False
162
+ for i in range (len (movements )):
163
+ movement = movements [0 ]
164
+ # print(movement, bot_pos)
165
+ # for y in range(10):
166
+ # for x in range(20):
167
+ # if (x, y) in walls:
168
+ # print("#", end="")
169
+ # elif (x, y) in boxes:
170
+ # print("[]", end="")
171
+ # elif bot_pos == (x, y):
172
+ # print("@", end="")
173
+ # elif (x-1, y) not in boxes:
174
+ # print(".", end="")
175
+ # print()
176
+ # sleep(0.1)
177
+ if movement == "<" :
178
+ if do_push (bot_pos [0 ], bot_pos [1 ]):
179
+ bot_pos = (bot_pos [0 ]- 1 , bot_pos [1 ])
180
+ if movement == "^" :
181
+ if do_push (bot_pos [0 ], bot_pos [1 ]):
182
+ bot_pos = (bot_pos [0 ], bot_pos [1 ]- 1 )
183
+ if movement == ">" :
184
+ if do_push (bot_pos [0 ], bot_pos [1 ]):
185
+ bot_pos = (bot_pos [0 ]+ 1 , bot_pos [1 ])
186
+ if movement == "v" :
187
+ if do_push (bot_pos [0 ], bot_pos [1 ]):
188
+ bot_pos = (bot_pos [0 ], bot_pos [1 ]+ 1 )
189
+ push_stack .clear ()
190
+ total = 0
191
+ for box in boxes :
192
+ total += box [0 ] + box [1 ] * 100
193
+ print (total )
194
+ for y in range (10 ):
195
+ for x in range (20 ):
196
+ if (x , y ) in walls :
197
+ print ("#" , end = "" )
198
+ elif (x , y ) in boxes :
199
+ print ("[]" , end = "" )
200
+ elif bot_pos == (x , y ):
201
+ print ("@" , end = "" )
202
+ elif (x - 1 , y ) not in boxes :
203
+ print ("." , end = "" )
204
+ print ()
0 commit comments