-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay24.py
39 lines (33 loc) · 1.61 KB
/
Day24.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
from helpers.importHelpers import *
# A few global variables:
def getBlizzards(stringInput, width, height): # blizzards = list(tuple(startX, startY, movingDirection))
blizzards = []
blizzardDirections = {'<': (-1,0), '>': (1,0), '^': (0,-1), 'v': (0,1)}
for y in range(1, height - 1):
for x in range(1, width - 1):
if stringInput[y][x] != '.':
blizzards.append((x, y, blizzardDirections[stringInput[y][x]]))
return blizzards
def simulation(blizzards, start, targetPosition, minute = 0):
myPositions = set([start])
while True:
minute += 1
currentBlizzardPositions = set()
for x,y,(dx,dy) in blizzards:
currentBlizzardPositions.add((1 + (x-1+dx*minute)%(width-2), 1 + (y-1+dy*minute)%(height-2)))
for position in list(myPositions):
for direction in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
newPosition = (position[0] + direction[0], position[1] + direction[1])
if newPosition == targetPosition:
return minute
if newPosition not in currentBlizzardPositions and newPosition[0] > 0 and newPosition[0] < width - 1 and newPosition[1] > 0 and newPosition[1] < height - 1:
myPositions.add(newPosition)
if position in currentBlizzardPositions:
myPositions.remove(position)
stringInput = getInput().split("\n")
width, height = len(stringInput[0]), len(stringInput)
blizzards = getBlizzards(stringInput, width, height)
start = (1, 0)
target = (width-2, height-1)
print("Part 1: ", resultPart1 := simulation(blizzards, start, target))
print("Part 2: ", simulation(blizzards, start, target, simulation(blizzards, target, start, resultPart1)))