Skip to content

Commit a15445b

Browse files
committed
⭐⭐ Day 18
1 parent e8efc74 commit a15445b

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

2022/Readme.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
- `if min(minutes) <= 0: UnboundLocalError: cannot access local variable 'minutes' where it is not associated with a value` forgot to rename the variable
136136
- `all_minutes = (new_minutes,) + all_minutes[not eleflop] TypeError: can only concatenate tuple (not "int") to tuple` forgot I changed to index instead of slice
137137

138-
IIt's December 24th when I finally solved this with heavy help from the solutions thread. I lost my mind on this and out of frustration I stopped adding errors here, especially when I got unconcentrated over the weekend. Sorry.
138+
It's December 24th when I finally solved this with heavy help from the solutions thread. I lost my mind on this and out of frustration I stopped adding errors here, especially when I got unconcentrated over the weekend. Sorry.
139139

140140
### 2022-12-17
141141
- `new_pieces.append(i+ii*1j) NameError: name 'new_pieces' is not defined. Did you mean: 'new_piece'?` oops. Yes I did. The new Python errors are really good actually.
@@ -147,4 +147,9 @@ IIt's December 24th when I finally solved this with heavy help from the solution
147147
- `for i in range(self.height): TypeError: 'float' object cannot be interpreted as an integer` oops, yeah the `max()` function returned a float
148148
- `self.check_tetris(piece) TypeError: Tetris.check_tetris() takes 1 positional argument but 2 were given` forgot to provide the `piece` to the function
149149
- `signature = tuple(k-(self.height+23) for k, v in self.board.items() if k >= self.height - 23)TypeError: '>=' not supported between instances of 'complex' and 'int'` I will never not fall for this.
150-
- `self.height += height_offset - h_end UnboundLocalError: cannot access local variable 'height_offset' where it is not associated with a value` didn't initialize...
150+
- `self.height += height_offset - h_end UnboundLocalError: cannot access local variable 'height_offset' where it is not associated with a value` didn't initialize...
151+
152+
### 2022-12-18
153+
- Made a logic error, where I thought a single dimension has to be offset by one to be a neighbour. No idea how I had that error in my mind.
154+
- `cube = nx.grid_graph(dim=(range(min_x-2, max_x+2), range(min_y-2, max_y+2), range(min_z-2, max_z+2)) SyntaxError: '(' was never closed` just autocomplete things.
155+
- `KeyError: (-2, -2, -2)` oops. wrong minimum

2022/day18.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from day import Day
2+
from aocd import submit
3+
from itertools import combinations
4+
5+
import networkx as nx
6+
7+
8+
def check_neighbours(data):
9+
for pair in combinations(data, 2):
10+
diffs = (abs(a - b) for a, b in zip(*pair))
11+
if sum(diffs) == 1:
12+
data[pair[0]] -= 1
13+
data[pair[1]] -= 1
14+
return data
15+
16+
17+
def steam(data):
18+
min_x, max_x, min_y, max_y, min_z, max_z = find_extents(data)
19+
min_all, max_all = min(min_x, min_y, min_z), max(max_x, max_y, max_z)
20+
21+
cube = nx.grid_graph(dim=[range(min_all - 2, max_all + 2)] * 3)
22+
23+
hole = cube.copy()
24+
hole.remove_nodes_from(data.keys())
25+
26+
lava = max(nx.connected_components(hole), key=len)
27+
return nx.edge_boundary(cube, lava)
28+
29+
30+
def find_extents(data):
31+
min_x = min(data, key=lambda x: x[0])[0]
32+
max_x = max(data, key=lambda x: x[0])[0]
33+
min_y = min(data, key=lambda x: x[1])[1]
34+
max_y = max(data, key=lambda x: x[1])[1]
35+
min_z = min(data, key=lambda x: x[2])[2]
36+
max_z = max(data, key=lambda x: x[2])[2]
37+
return min_x, max_x, min_y, max_y, min_z, max_z
38+
39+
40+
def main(day, part=1):
41+
day.parse_list_of_lists(sep="\n", sep2=",", typing=int)
42+
day.data = {tuple(d): 6 for d in day.data}
43+
if part == 1:
44+
return sum(x for x in check_neighbours(day.data).values())
45+
if part == 2:
46+
return sum(1 for _ in steam(day.data))
47+
48+
49+
if __name__ == "__main__":
50+
day = Day(18)
51+
day.download()
52+
53+
day.load()
54+
p1 = main(day)
55+
print(p1)
56+
submit(p1, part="a", day=18, year=2022)
57+
58+
day.load()
59+
# data = """2,2,2
60+
# 1,2,2
61+
# 3,2,2
62+
# 2,1,2
63+
# 2,3,2
64+
# 2,2,1
65+
# 2,2,3
66+
# 2,2,4
67+
# 2,2,6
68+
# 1,2,5
69+
# 3,2,5
70+
# 2,1,5
71+
# 2,3,5"""
72+
73+
# day.load(data)
74+
p2 = main(day, part=2)
75+
print(p2)
76+
submit(p2, part="b", day=18, year=2022)

2022/tests/test_day18.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
import pytest
3+
4+
sys.path.insert(0, ".")
5+
from day import Day
6+
from day18 import *
7+
8+
@pytest.fixture(scope="function")
9+
def example():
10+
day = Day(18)
11+
data = """2,2,2
12+
1,2,2
13+
3,2,2
14+
2,1,2
15+
2,3,2
16+
2,2,1
17+
2,2,3
18+
2,2,4
19+
2,2,6
20+
1,2,5
21+
3,2,5
22+
2,1,5
23+
2,3,5"""
24+
25+
day.load(data)
26+
return day
27+
28+
@pytest.fixture(scope="function")
29+
def day():
30+
day = Day(18)
31+
day.load()
32+
return day
33+
34+
## Part 1
35+
def test_example(example):
36+
assert main(example, part=1) == 64
37+
38+
def test_part1(day):
39+
assert main(day, part=1) == 4308
40+
41+
## Part 2
42+
def test_example_p2(example):
43+
assert main(example, part=2) == 58
44+
45+
def test_part2(day):
46+
assert main(day, part=2) == 2540

0 commit comments

Comments
 (0)