-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.py
113 lines (95 loc) · 3.73 KB
/
day17.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
#!/usr/bin/env python
import numpy as np
def rightPush(rock, rockTower):
for pixel in rock:
pixelPosition = rock[pixel]
if pixelPosition[1] == 6:
return rock
if rockTower[pixelPosition[0], pixelPosition[1]+1] > 0:
return rock
for pixel in rock:
rock[pixel] = [rock[pixel][0], rock[pixel][1]+1]
return rock
def leftPush(rock, rockTower):
for pixel in rock:
pixelPosition = rock[pixel]
if pixelPosition[1] == 0:
return rock
if rockTower[pixelPosition[0], pixelPosition[1]-1] > 0:
return rock
for pixel in rock:
rock[pixel] = [rock[pixel][0], rock[pixel][1]-1]
return rock
def moveDownOne(rock, rockTower):
for pixel in rock:
pixelPosition = rock[pixel]
if pixelPosition[0] == 0:
return rock, False
if rockTower[pixelPosition[0]-1, pixelPosition[1]] > 0:
return rock, False
for pixel in rock:
rock[pixel] = [rock[pixel][0]-1, rock[pixel][1]]
return rock, True
def rockFall(rock, rockTower, currentJetInstructionId, jet, currentHeight):
hasMove = True
while hasMove:
if jet[currentJetInstructionId % len(jet)] == '>':
rock = rightPush(rock, rockTower)
else:
rock = leftPush(rock, rockTower)
rock, hasMove = moveDownOne(rock, rockTower)
currentJetInstructionId +=1
for pixel in rock:
pixelPosition = rock[pixel]
rockTower[pixelPosition[0], pixelPosition[1]] = 1
if pixelPosition[0] > currentHeight:
currentHeight = pixelPosition[0]
return rockTower, currentJetInstructionId, currentHeight
f = open("ressources/day17.txt", "r")
lines = f.readlines()
jet = list(lines[0].replace("\n", ""))
rockTower = np.zeros((6000, 7))
currentHeight = -1
currentJetInstructionId = 0
rock_tiret = {"0":[0,0], "1":[0,1], "2":[0,2], "3":[0,3]}
rock_plus = {"0":[0,1], "1":[1,0], "2":[1,1], "3":[1,2], "4":[2,1]}
rock_lshape = {"0":[0,0], "1":[0,1], "2":[0,2], "3":[1,2], "4":[2,2]}
rock_bar = {"0":[0,0], "1":[1,0], "2":[2,0], "3":[3,0]}
rock_cube = {"0":[0,0], "1":[0,1], "2":[1,0], "3":[1,1]}
rockShapes = [rock_tiret, rock_plus, rock_lshape, rock_bar, rock_cube]
for i in range(2022):
rock = rockShapes[i%5].copy()
for pixel in rock:
rock[pixel] = [rock[pixel][0]+currentHeight+4, rock[pixel][1]+2]
rockTower, currentJetInstructionId, currentHeight = rockFall(rock, rockTower, currentJetInstructionId, jet, currentHeight)
print("Height after 2022 rock fell is: {}".format(currentHeight+1))
jet = list(lines[0].replace("\n", ""))
rockTower = np.zeros((6000, 7))
currentHeight = -1
currentJetInstructionId = 0
totalHeight = 0
patterns = {}
numberOfRocks = 1000000000000
keepGoing = True
i = 0
while keepGoing:
rock = rockShapes[i%5].copy()
for pixel in rock:
rock[pixel] = [rock[pixel][0]+currentHeight+4, rock[pixel][1]+2]
rockTower, currentJetInstructionId, currentHeight = rockFall(rock, rockTower, currentJetInstructionId, jet, currentHeight)
rockCycle = i % 5
jetCycle = currentJetInstructionId % len(jet)
cycleIndex = (rockCycle, jetCycle)
if cycleIndex in patterns:
previousIndex, previousHeight = patterns[cycleIndex]
period = i - previousIndex
if i % period == numberOfRocks % period:
elevationPerCycle = currentHeight - previousHeight
leftRocks = numberOfRocks - i
leftCycles = (leftRocks // period) + 1
totalHeight = previousHeight + (leftCycles * elevationPerCycle)
keepGoing = False
else:
patterns[cycleIndex] = (i, currentHeight)
i+=1
print("Height after 1000000000000 rock fell is: {}".format(totalHeight))