Skip to content

Commit 5e6176d

Browse files
committed
Solve puzzle 2 of day 5
1 parent 83045b3 commit 5e6176d

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

05/hydrothermal_venture.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,60 @@ class VentMap():
99
def __init__(self, size: int = 9):
1010
self.map = np.zeros((size, size), dtype=np.int64)
1111

12-
def mark_line(self, endpoints: npt.ArrayLike) -> None:
12+
def mark_line(self, endpoints: npt.ArrayLike, diagonal: bool = False) -> None:
1313
"""Mark the points that a horizontal or vertical line occupies on the map
1414
1515
Args:
1616
endpoints (npt.ArrayLike): A list of the end point coordinates: [x1, y1, x2, y2]
1717
"""
18+
endpoints = np.asarray(endpoints)
1819
if endpoints[0] == endpoints[2] or endpoints[1] == endpoints[3]:
19-
endpoints = np.asarray(endpoints)
2020
x1, x2 = np.sort(endpoints[[0, 2]])
2121
y1, y2 = np.sort(endpoints[[1, 3]])
2222
self.map[y1:y2+1, x1:x2+1] += 1
23+
elif diagonal:
24+
x1, x2 = endpoints[[0, 2]]
25+
y1, y2 = endpoints[[1, 3]]
26+
dx = np.abs(x2 - x1)
27+
dy = np.abs(y2 - y1)
28+
if dx == dy:
29+
xrange = np.linspace(start=x1, stop=x2, num=dx+1, dtype=np.int64)
30+
yrange = np.linspace(start=y1, stop=y2, num=dy+1, dtype=np.int64)
31+
self.map[yrange, xrange] += 1
2332

24-
def mark_all_lines(self, endpoints_list: npt.ArrayLike) -> None:
33+
def mark_all_lines(self, endpoints_list: npt.ArrayLike, diagonal: bool = False) -> None:
2534
"""Mark the points that a horizontal or vertical line occupies on the map
2635
2736
Args:
2837
endpoints (npt.ArrayLike): A list of lists of the end point coordinates: [[x1, y1, x2, y2], [x1, y1, x2, y2], ...]
2938
"""
3039
for line in endpoints_list:
31-
self.mark_line(endpoints=line)
40+
self.mark_line(endpoints=line, diagonal=diagonal)
3241

3342
def calculate_overlap(self):
3443
return (self.map >= 2).sum()
3544

3645

37-
def puzzle_1(inputfile: Path) -> int:
46+
def puzzle_1(inputfile: Path, diagonal=False) -> int:
3847
line_parse = re.compile(r'(\d+),(\d+)\s+->\s+(\d+),(\d+)')
3948
endpoints_list = []
4049
with open(inputfile, 'r') as file:
4150
for line in file:
4251
endpoints_list.append([int(x) for x in line_parse.search(line).groups()])
4352
endpoints_list = np.array(endpoints_list)
4453
vent_map = VentMap(size=endpoints_list.max() + 1)
45-
vent_map.mark_all_lines(endpoints_list)
54+
vent_map.mark_all_lines(endpoints_list, diagonal=diagonal)
4655
return vent_map.calculate_overlap()
4756

4857

4958
def puzzle_2(inputfile: Path) -> int:
50-
pass
51-
# with open(inputfile, 'r') as file:
52-
# draw = [int(x) for x in file.readline().strip('\n').split(',')]
53-
# file.readline()
54-
# board_strings = []
55-
# for line in file:
56-
# board_strings.append(line.strip('\n'))
57-
# bingo_boards = BingoBoardCollection(boards=board_strings)
58-
# return bingo_boards.find_losing_score(draw=draw)
59+
return puzzle_1(inputfile=inputfile, diagonal=True)
5960

6061

6162
def main():
6263
inputfile = Path("puzzle_1_input.txt")
6364
print(f"Puzzle 1 Answer = {puzzle_1(inputfile)}")
64-
# print(f"Puzzle 2 Answer = {puzzle_2(inputfile)}")
65+
print(f"Puzzle 2 Answer = {puzzle_2(inputfile)}")
6566

6667

6768
if __name__ == "__main__":

05/test_hydrothermal_venture.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from numpy.core.fromnumeric import diagonal
12
from hydrothermal_venture import VentMap, np, Path
23
from hydrothermal_venture import puzzle_1, puzzle_2
34
import pytest
@@ -60,6 +61,35 @@ def endstate_map():
6061
[2, 2, 2, 1, 1, 1, 0, 0, 0, 0]])
6162

6263

64+
@pytest.fixture
65+
def diagonal_mark():
66+
return ([5, 5, 8, 2],
67+
np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
68+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
69+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
70+
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
71+
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
72+
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
73+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
74+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
75+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
76+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]))
77+
78+
79+
@pytest.fixture
80+
def endstate_diagonal_map():
81+
return np.array([[1, 0, 1, 0, 0, 0, 0, 1, 1, 0],
82+
[0, 1, 1, 1, 0, 0, 0, 2, 0, 0],
83+
[0, 0, 2, 0, 1, 0, 1, 1, 1, 0],
84+
[0, 0, 0, 1, 0, 2, 0, 2, 0, 0],
85+
[0, 1, 1, 2, 3, 1, 3, 2, 1, 1],
86+
[0, 0, 0, 1, 0, 2, 0, 0, 0, 0],
87+
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
88+
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
89+
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0],
90+
[2, 2, 2, 1, 1, 1, 0, 0, 0, 0]])
91+
92+
6393
@pytest.mark.parametrize(("line, line_mark"), [
6494
([0, 9, 5, 9], line2_mark), ([2, 2, 2, 1], line1_mark)
6595
])
@@ -80,7 +110,22 @@ def test_ventmap_calculate_overlap(vent_map, endstate_map):
80110

81111
def test_puzzle_1():
82112
assert puzzle_1(Path(__file__).parent / "puzzle_1_test_input.txt") == 5
83-
#
84-
#
113+
114+
115+
def test_ventmap_diagonal_mark(vent_map, diagonal_mark):
116+
endpoints, endstate = diagonal_mark
117+
vent_map.mark_line(endpoints=endpoints, diagonal=True)
118+
assert np.equal(vent_map.map, endstate).all()
119+
120+
121+
def test_ventmap_diagonal_mark_all_lines(vent_map, list_of_lines, endstate_diagonal_map):
122+
vent_map.mark_all_lines(endpoints_list=list_of_lines, diagonal=True)
123+
assert np.equal(vent_map.map, endstate_diagonal_map).all()
124+
125+
126+
def test_puzzle_2():
127+
assert puzzle_2(Path(__file__).parent / "puzzle_1_test_input.txt") == 12
128+
129+
#
85130
# def test_puzzle_2():
86131
# assert puzzle_2(Path(__file__).parent / "puzzle_1_test_input.txt") == 1924

0 commit comments

Comments
 (0)