-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode22d14.py
179 lines (152 loc) · 5.78 KB
/
adventOfCode22d14.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
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 18 17:41:44 2022
oauthor: xBubblex
"""
import re
import numpy as np
import random as rd
import functools as ft
def load_files():
fContent = []
with open("input14.txt") as f:
for (lineIndex, line) in enumerate(f): #loading the file into an np.array
if bool(line) and line != "\n":
fContent.append(line.strip("\n").split(" -> "))
return(fContent)
# print(load_files())
class Stone:
def __init__(self):
self.raw = load_files()
self.lines = []
self.board = np.array([])
self.maxH = 0
self.maxV = 0
self.minH = 0
self.minV = 0
self.boardReady = False
def __preprocess__(self):
self.lines = []
for lineIdx, line in enumerate(self.raw):
lineCoords = {"ver": [], "hor": []}
for coordsIdx, coords in enumerate(line):
coord = coords.split(",")
lineCoords["ver"].append(int(coord[1]))
lineCoords["hor"].append(int(coord[0]))
self.lines.append(lineCoords)
return self
def __prepare_board__(self):
allCoords = {"ver": [], "hor": []}
for line in self.lines:
allCoords["ver"] += line["ver"]
allCoords["hor"] += line["hor"]
maxH = max(allCoords["hor"])
maxV = max(allCoords["ver"])
minH = min(allCoords["hor"])
minV = min(allCoords["ver"])
self.maxH = maxH
self.maxV = maxV
self.minH = minH
self.minV = minV
self.board = np.zeros((maxV + 3, maxH + 2), dtype = object)
return self
def __prepare_stones__(self):
for line in self.lines:
prevCoord = ["v", "h"]
for coordIdx, coord in enumerate(zip(line["ver"], line["hor"])):
coordV = coord[0]
coordH = coord[1]
if prevCoord == ["v", "h"]:
prevCoord = [coordV, coordH]
else:
self.board[min(prevCoord[0], coordV) : max(prevCoord[0], coordV) + 1, min(prevCoord[1], coordH) : max(prevCoord[1], coordH) + 1] = "#"
prevCoord = [coordV, coordH]
return self
def board_ready(self, mode = "void"):
if not self.boardReady:
self.__preprocess__()
self.__prepare_board__()
self.__prepare_stones__()
self.board[np.where(self.board == 0)] = "."
if mode == "noVoid":
# print(self.board.shape)
for counter in range(2 * self.maxV):
self.entry[1] += 1
self.board = np.insert(self.board, 0, ".", axis=1)
self.board = np.insert(self.board, -1, ".", axis=1)
self.board[-1, :] = "#"
# print(self.board.shape)
# print(self.board[-1, :])
self.boardReady = True
return self
class Sand(Stone):
def __init__(self, entry = [0, 500]):
super().__init__()
self.entry = entry
self.grainCounter = 0
self.limReached = False
def __add_grain__(self):
prevPos = self.entry
canMove = True
# print(self.grainCounter)
if self.board[prevPos[0], prevPos[1]] == "o":
return self
self.board[prevPos[0], prevPos[1]] = "o"
while canMove:
# print(prevPos)
try:
self.board[prevPos[0], prevPos[1]]
if self.board[prevPos[0] + 1, prevPos[1]] not in ["#", "o"]:
self.board[prevPos[0] + 1, prevPos[1]] = "o"
self.board[prevPos[0], prevPos[1]] = "."
prevPos = [prevPos[0] + 1, prevPos[1]]
elif self.board[prevPos[0] + 1, prevPos[1] - 1] not in ["#", "o"]:
self.board[prevPos[0] + 1, prevPos[1] - 1] = "o"
self.board[prevPos[0], prevPos[1]] = "."
prevPos = [prevPos[0] + 1, prevPos[1] - 1]
elif self.board[prevPos[0] + 1, prevPos[1] + 1] not in ["#", "o"]:
self.board[prevPos[0] + 1, prevPos[1] + 1] = "o"
self.board[prevPos[0], prevPos[1]] = "."
prevPos = [prevPos[0] + 1, prevPos[1] + 1]
else:
canMove = False
if prevPos == self.entry:
self.grainCounter += 1
self.limReached = True
return self
except:
self.limReached = True
# print("BREAK")
return self
self.grainCounter += 1
# print(self.grainCounter)
return self
def simulate_sand(self, mode = "void"):
self.board_ready(mode)
# if mode == "noVoid":
# entry = [0, 500 + self.maxH]
self.grainCounter = 0
self.limReached = False
while not self.limReached:
self.__add_grain__()
return self
def print_sand(self, rangeView = 40):
for line in self.board:
for symbol in line[self.entry[1] - rangeView : self.entry[1] + rangeView]:
print(symbol, end = "")
print()
print()
def run():
stonePath = Stone()
stonePath.board_ready()
sand = Sand()
# for line in sand.board_ready().board:
# print(line[490 : 520])
# sand.board_ready(mode = "noVoid").print_sand()
grainNo = sand.simulate_sand(mode = "noVoid").grainCounter
sand.print_sand()
return grainNo
print(run())
# a = np.arange(25).reshape((5, -1))
# a[1 : 2, 1 : -1] = 0
# print(a)