-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29.py
96 lines (93 loc) · 2.72 KB
/
29.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
from time import sleep
f = open("29.in")
walls = []
boxes = []
movements = []
bot_pos = (0,0)
for y, line in enumerate(f.readlines()):
for x, char in enumerate(line):
if char == "#":
walls.append((x,y))
elif char == "O":
boxes.append((x,y))
elif char == "@":
bot_pos = (x,y)
elif char == "\n" or char == ".":
continue
else:
movements.append(char)
def do_push(x, y, movement=None):
global walls, boxes, movements, bot_pos
if movement == None:
movement = movements.pop(0)
if movement == "<":
if (x-1, y) in walls:
return False
if (x-1, y) in boxes:
if do_push(x-1, y, movement):
boxes[boxes.index((x-1, y))] = (x-2, y)
return True
else:
return False
return True
if movement == "^":
if (x, y-1) in walls:
return False
if (x, y-1) in boxes:
if do_push(x, y-1, movement):
boxes[boxes.index((x, y-1))] = (x, y-2)
return True
else:
return False
return True
if movement == ">":
if (x+1, y) in walls:
return False
if (x+1, y) in boxes:
if do_push(x+1, y, movement):
boxes[boxes.index((x+1, y))] = (x+2, y)
return True
else:
return False
return True
if movement == "v":
if (x, y+1) in walls:
return False
if (x, y+1) in boxes:
if do_push(x, y+1, movement):
boxes[boxes.index((x, y+1))] = (x, y+2)
return True
else:
return False
return True
for i in range(len(movements)):
movement = movements[0]
# print(movement)
# for y in range(8):
# for x in range(8):
# if (x, y) in walls:
# print("#", end="")
# elif (x, y) in boxes:
# print("O", end="")
# elif bot_pos == (x, y):
# print("@", end="")
# else:
# print(".", end="")
# print()
# sleep(1)
if movement == "<":
if do_push(bot_pos[0], bot_pos[1]):
bot_pos = (bot_pos[0]-1, bot_pos[1])
if movement == "^":
if do_push(bot_pos[0], bot_pos[1]):
bot_pos = (bot_pos[0], bot_pos[1]-1)
if movement == ">":
if do_push(bot_pos[0], bot_pos[1]):
bot_pos = (bot_pos[0]+1, bot_pos[1])
if movement == "v":
if do_push(bot_pos[0], bot_pos[1]):
bot_pos = (bot_pos[0], bot_pos[1]+1)
total = 0
for box in boxes:
total += box[0] + box[1] * 100
print(total)