Skip to content

Commit 3a51a0f

Browse files
committed
Added days 2019-24 and 2019-25
1 parent 3b4a38a commit 3a51a0f

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed

2019/24-Planet of Discord.py

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, pathfinding
3+
4+
from complex_utils import *
5+
6+
test_data = {}
7+
8+
test = 1
9+
test_data[test] = {
10+
"input": """....#
11+
#..#.
12+
#..##
13+
..#..
14+
#....""",
15+
"expected": ["2129920", "99"],
16+
}
17+
18+
test = "real"
19+
input_file = os.path.join(
20+
os.path.dirname(__file__),
21+
"Inputs",
22+
os.path.basename(__file__).replace(".py", ".txt"),
23+
)
24+
test_data[test] = {
25+
"input": open(input_file, "r+").read(),
26+
"expected": ["20751345", "1983"],
27+
}
28+
29+
# -------------------------------- Control program execution ------------------------- #
30+
31+
case_to_test = "real"
32+
part_to_test = 2
33+
34+
# -------------------------------- Initialize some variables ------------------------- #
35+
36+
puzzle_input = test_data[case_to_test]["input"]
37+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
38+
puzzle_actual_result = "Unknown"
39+
40+
41+
# -------------------------------- Actual code execution ----------------------------- #
42+
43+
44+
def grid_to_vertices(self, grid):
45+
self.vertices = {}
46+
y = 0
47+
for line in grid.splitlines():
48+
for x in range(len(line)):
49+
self.vertices[x - y * j] = line[x]
50+
y += 1
51+
52+
for source in self.vertices:
53+
for direction in directions_straight:
54+
target = source + direction
55+
if target in self.vertices:
56+
if source in self.edges:
57+
self.edges[source].append(target)
58+
else:
59+
self.edges[source] = [target]
60+
61+
return True
62+
63+
64+
pathfinding.Graph.grid_to_vertices = grid_to_vertices
65+
66+
67+
def biodiversity_rating(self):
68+
rating = 0
69+
for y in range(int(min_imag(self.vertices)), int(max_imag(self.vertices) + 1)):
70+
for x in range(int(min_real(self.vertices)), int(max_real(self.vertices) + 1)):
71+
if self.vertices[x + y * j] == "#":
72+
rating += pow(2, -y * (max_real(self.vertices) + 1) + x)
73+
74+
return int(rating)
75+
76+
77+
pathfinding.Graph.biodiversity_rating = biodiversity_rating
78+
79+
80+
if part_to_test == 1:
81+
empty_grid = ("." * 5 + "\n") * 5
82+
area = pathfinding.Graph()
83+
new_area = pathfinding.Graph()
84+
area.grid_to_vertices(puzzle_input)
85+
86+
previous_ratings = []
87+
while area.biodiversity_rating() not in previous_ratings:
88+
previous_ratings.append(area.biodiversity_rating())
89+
new_area.grid_to_vertices(empty_grid)
90+
for position in area.vertices:
91+
if area.vertices[position] == "#":
92+
living_neighbors = len(
93+
[
94+
neighbor
95+
for neighbor in area.neighbors(position)
96+
if area.vertices[neighbor] == "#"
97+
]
98+
)
99+
if living_neighbors == 1:
100+
new_area.vertices[position] = "#"
101+
else:
102+
new_area.vertices[position] = "."
103+
else:
104+
living_neighbors = len(
105+
[
106+
neighbor
107+
for neighbor in area.neighbors(position)
108+
if area.vertices[neighbor] == "#"
109+
]
110+
)
111+
if living_neighbors in (1, 2):
112+
new_area.vertices[position] = "#"
113+
else:
114+
new_area.vertices[position] = "."
115+
116+
area.vertices = new_area.vertices.copy()
117+
118+
puzzle_actual_result = area.biodiversity_rating()
119+
120+
else:
121+
122+
def neighbors(self, vertex):
123+
neighbors = []
124+
position, level = vertex
125+
for dir in directions_straight:
126+
if (position + dir, level) in self.vertices:
127+
neighbors.append((position + dir, level))
128+
129+
# Connection to lower (outside) levels
130+
if position.imag == 0:
131+
neighbors.append((2 - 1 * j, level - 1))
132+
elif position.imag == -4:
133+
neighbors.append((2 - 3 * j, level - 1))
134+
if position.real == 0:
135+
neighbors.append((1 - 2 * j, level - 1))
136+
elif position.real == 4:
137+
neighbors.append((3 - 2 * j, level - 1))
138+
139+
# Connection to higher (inside) levels
140+
if position == 2 - 1 * j:
141+
neighbors += [(x, level + 1) for x in range(5)]
142+
elif position == 2 - 3 * j:
143+
neighbors += [(x - 4 * j, level + 1) for x in range(5)]
144+
elif position == 1 - 2 * j:
145+
neighbors += [(-y * j, level + 1) for y in range(5)]
146+
elif position == 3 - 2 * j:
147+
neighbors += [(4 - y * j, level + 1) for y in range(5)]
148+
149+
return neighbors
150+
151+
pathfinding.Graph.neighbors = neighbors
152+
153+
empty_grid = ("." * 5 + "\n") * 5
154+
area = pathfinding.Graph()
155+
area.grid_to_vertices(puzzle_input)
156+
area.add_walls([2 - 2 * j])
157+
158+
nb_minutes = 200 if case_to_test == "real" else 10
159+
160+
recursive = pathfinding.Graph()
161+
recursive.vertices = {
162+
(position, level): "."
163+
for position in area.vertices
164+
for level in range(-nb_minutes // 2, nb_minutes // 2 + 1)
165+
}
166+
167+
recursive.vertices.update(
168+
{(position, 0): area.vertices[position] for position in area.vertices}
169+
)
170+
171+
for generation in range(nb_minutes):
172+
new_grids = pathfinding.Graph()
173+
new_grids.vertices = {}
174+
for position in recursive.vertices:
175+
if recursive.vertices[position] == "#":
176+
living_neighbors = len(
177+
[
178+
neighbor
179+
for neighbor in recursive.neighbors(position)
180+
if recursive.vertices.get(neighbor, ".") == "#"
181+
]
182+
)
183+
if living_neighbors == 1:
184+
new_grids.vertices[position] = "#"
185+
else:
186+
new_grids.vertices[position] = "."
187+
else:
188+
living_neighbors = len(
189+
[
190+
neighbor
191+
for neighbor in recursive.neighbors(position)
192+
if recursive.vertices.get(neighbor, ".") == "#"
193+
]
194+
)
195+
if living_neighbors in (1, 2):
196+
new_grids.vertices[position] = "#"
197+
else:
198+
new_grids.vertices[position] = "."
199+
200+
recursive.vertices = new_grids.vertices.copy()
201+
202+
puzzle_actual_result = len(
203+
[x for x in recursive.vertices if recursive.vertices[x] == "#"]
204+
)
205+
206+
207+
# -------------------------------- Outputs / results --------------------------------- #
208+
209+
print("Expected result : " + str(puzzle_expected_result))
210+
print("Actual result : " + str(puzzle_actual_result))

2019/25-Cryostasis.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, pathfinding, IntCode
3+
4+
from complex_utils import *
5+
6+
test_data = {}
7+
8+
test = 1
9+
test_data[test] = {
10+
"input": """""",
11+
"expected": ["Unknown", "Unknown"],
12+
}
13+
14+
test = "real"
15+
input_file = os.path.join(
16+
os.path.dirname(__file__),
17+
"Inputs",
18+
os.path.basename(__file__).replace(".py", ".txt"),
19+
)
20+
test_data[test] = {
21+
"input": open(input_file, "r+").read(),
22+
"expected": "Objects: coin, shell, space heater, fuel cell - code : 805306888",
23+
}
24+
25+
# -------------------------------- Control program execution ------------------------- #
26+
27+
case_to_test = "real"
28+
29+
# -------------------------------- Initialize some variables ------------------------- #
30+
31+
puzzle_input = test_data[case_to_test]["input"]
32+
puzzle_expected_result = test_data[case_to_test]["expected"]
33+
puzzle_actual_result = "Unknown"
34+
35+
36+
# -------------------------------- Actual code execution ----------------------------- #
37+
38+
droid = IntCode.IntCode(puzzle_input)
39+
droid.run()
40+
41+
while True:
42+
for number in droid.outputs:
43+
print(chr(number), end="")
44+
45+
data = input()
46+
for letter in data:
47+
print(data)
48+
droid.add_input(ord(letter))
49+
droid.add_input(ord("\n"))
50+
droid.restart()
51+
droid.run()
52+
53+
# north, south, east, or west.
54+
# take <name of item>
55+
# drop <name of item>
56+
# inv
57+
58+
59+
# -------------------------------- Outputs / results --------------------------------- #
60+
61+
print("Expected result : " + str(puzzle_expected_result))
62+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy