-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.py
85 lines (70 loc) · 2.57 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
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
#!/usr/bin/env python
import numpy as np
from collections import defaultdict
blizzards = {'<':(0, -1), '>':(0, 1), '^':(-1, 0) ,'v':(1, 0)}
f = open("ressources/day24.txt", "r")
lines = f.readlines()
blizzardMap = []
for line in lines:
mapLine = line.replace("\n", "")
blizzardMap.append(list(mapLine))
blizzardSet = set()
fullGrid = set()
height = 0
width = 0
for i, blizzardLine in enumerate(blizzardMap[1:-1]):
for j, blizzardItem in enumerate(blizzardLine[1:-1]):
if blizzardItem in blizzards.keys():
blizzardSet.add(((i,j), blizzards[blizzardItem]))
fullGrid.add((i, j))
height = max(height, i+1)
width = max(width, j+1)
startPoint, endPoint = (-1, 0), (height, width-1)
fullGrid.add(startPoint)
fullGrid.add(endPoint)
def moveBlizzards(fullGrid, blizzardSet, width, height):
newBlizzardSet = set()
for blizzardItem in blizzardSet:
(i, j), (windI, windJ) = blizzardItem
newI, newJ = (i + windI) % height, (j + windJ) % width
newBlizzardSet.add(((newI,newJ), (windI, windJ)))
freeCoordinates = fullGrid - set(blizzardItem[0] for blizzardItem in blizzardSet)
return newBlizzardSet, freeCoordinates
presentPoints = set()
presentPoints.add(startPoint)
def step(presentPoints, freeCoordinates):
exploration = [(1,0), (-1,0), (0,1), (0,-1), (0,0)]
endPoints = set()
for point in presentPoints:
for direction in exploration:
newPoint = (point[0]+direction[0], point[1]+direction[1])
if newPoint in freeCoordinates and newPoint not in endPoints:
endPoints.add(newPoint)
return endPoints
time = -1
while True:
blizzardSet, freeCoordinates = moveBlizzards(fullGrid, blizzardSet, width, height)
presentPoints = step(presentPoints, freeCoordinates)
time += 1
if endPoint in presentPoints:
break
print("Time to reach the end is: {}".format(time))
endPoint, startPoint = (-1, 0), (height, width-1)
presentPoints = set()
presentPoints.add(startPoint)
while True:
blizzardSet, freeCoordinates = moveBlizzards(fullGrid, blizzardSet, width, height)
presentPoints = step(presentPoints, freeCoordinates)
time += 1
if endPoint in presentPoints:
break
endPoint, startPoint = (height, width-1), (-1, 0)
presentPoints = set()
presentPoints.add(startPoint)
while True:
blizzardSet, freeCoordinates = moveBlizzards(fullGrid, blizzardSet, width, height)
presentPoints = step(presentPoints, freeCoordinates)
time += 1
if endPoint in presentPoints:
break
print("Time to get the snack is: {}".format(time))