-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.py
216 lines (182 loc) · 4.51 KB
/
day15.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
213
214
215
216
from HelperFunctions import readInputFile
from HelperFunctions import readExampleInput
from HelperFunctions import convertToInt
import copy
def printMap(map):
maxWidth = max([x for (x,y) in map.keys()])
maxHeight = max([y for (x,y) in map.keys()])
printmap = [['.' for x in range(maxWidth + 1)] for y in range(maxHeight + 1)]
for key,value in map.items():
x,y = key
printmap[y][x] = value
for line in printmap:
print("".join(line))
def createMap1(mapInput):
map = {}
for y,line in enumerate(mapInput.split('\n')):
for x,value in enumerate(line):
map[(x,y)] = value
return map
def createMap2(mapInput):
mapInput = mapInput.replace('#', '##')
mapInput = mapInput.replace('.', '..')
mapInput = mapInput.replace('@', '@.')
mapInput = mapInput.replace('O', '[]')
return createMap1(mapInput)
def createMoves(moveInput):
moves = []
for line in moveInput.split('\n'):
for value in line:
moves.append(value)
return moves
def push(boxPos, direction, map):
xbox,ybox = boxPos
match direction:
case '^':
pushPos = (xbox,ybox - 1)
case 'v':
pushPos = (xbox,ybox + 1)
case '<':
pushPos = (xbox - 1,ybox)
case '>':
pushPos = (xbox + 1,ybox)
match map[pushPos]:
case '#':
return False
case '.':
map[pushPos] = 'O'
map[boxPos] = '.'
return True
case 'O':
if push(pushPos, direction, map):
map[pushPos] = 'O'
map[boxPos] = '.'
return True
else:
return False
def pushBigBox(boxPositions, direction, map):
boxPos1,boxPos2 = boxPositions
x1,y1 = boxPos1
x2,y2 = boxPos2
match direction:
case '^':
newPos = [(x1,y1-1),(x2,y2-1)]
case 'v':
newPos = [(x1,y1+1), (x2,y2+1)]
case '<':
newPos = [(x1-1,y1)]
case '>':
newPos = [(x2+1,y2)]
movingPossible = True
for pos in newPos:
match map[pos]:
case '.':
continue
case '#':
movingPossible = False
case '[':
a,b = pos
if movingPossible:
movingPossible = pushBigBox([(a,b),(a+1,b)], direction, map)
case ']':
a,b = pos
if movingPossible:
movingPossible = pushBigBox([(a-1,b),(a,b)], direction, map)
if movingPossible:
match direction:
case '^':
map[(x1,y1-1)] = '['
map[(x2,y2-1)] = ']'
map[(x1,y1)] = '.'
map[(x2,y2)] = '.'
case 'v':
map[(x1,y1+1)] = '['
map[(x2,y2+1)] = ']'
map[(x1,y1)] = '.'
map[(x2,y2)] = '.'
case '<':
map[(x1-1,y1)] = '['
map[(x2-1,y2)] = ']'
map[(x2,y2)] = '.'
case '>':
map[(x1+1,y1)] = '['
map[(x2+1,y2)] = ']'
map[(x1,y1)] = '.'
return True
else:
return False
def do1(map, moves):
currentPos = [x for x in map.keys() if map[x] == '@'][0]
for move in moves:
x,y = currentPos
match move:
case '^':
possibleNewPos = (x, y-1)
case 'v':
possibleNewPos = (x, y+1)
case '<':
possibleNewPos = (x-1, y)
case '>':
possibleNewPos = (x+1, y)
match map[possibleNewPos]:
case '#':
continue
case '.':
map[currentPos] = '.'
currentPos = possibleNewPos
map[currentPos] = '@'
case 'O':
if push(possibleNewPos, move, map):
map[currentPos] = '.'
currentPos = possibleNewPos
map[currentPos] = '@'
boxPositions = [100 * y + x for (x,y) in map.keys() if map[(x,y)] == 'O']
return sum(boxPositions)
def do2(map, moves):
currentPos = [x for x in map.keys() if map[x] == '@'][0]
for move in moves:
x,y = currentPos
match move:
case '^':
possibleNewPos = (x, y-1)
case 'v':
possibleNewPos = (x, y+1)
case '<':
possibleNewPos = (x-1, y)
case '>':
possibleNewPos = (x+1, y)
match map[possibleNewPos]:
case '#':
continue
case '.':
map[currentPos] = '.'
currentPos = possibleNewPos
map[currentPos] = '@'
case '[':
newMap = copy.deepcopy(map)
boxPos1X,boxPos1Y = possibleNewPos
if pushBigBox([possibleNewPos,(boxPos1X+1,boxPos1Y)], move, newMap):
map = newMap
map[currentPos] = '.'
currentPos = possibleNewPos
map[currentPos] = '@'
case ']':
newMap = copy.deepcopy(map)
boxPos1X,boxPos1Y = possibleNewPos
if pushBigBox([(boxPos1X-1,boxPos1Y),possibleNewPos], move, newMap):
map = newMap
map[currentPos] = '.'
currentPos = possibleNewPos
map[currentPos] = '@'
boxPositions = [100 * y + x for (x,y) in map.keys() if map[(x,y)] == '[']
return sum(boxPositions)
def do():
strInput = readInputFile(15)
mapInput,moveInput = strInput.split('\n\n')
map1 = createMap1(mapInput)
map2 = createMap2(mapInput)
moves = createMoves(moveInput)
print(do1(map1, moves))
print(do2(map2, moves))
print('done')
do()