Skip to content

Commit da7ee96

Browse files
committed
Slihghtly simplify
1 parent e90ee40 commit da7ee96

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

2024/src/aoc/days/day15.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ def parse_input(data: str) -> tuple[numpy.array, str]:
1515
return grid_split, steps
1616

1717

18+
def convert_dir(c: str) -> tuple[int, int]:
19+
match c:
20+
case "^":
21+
return 0, -1
22+
case ">":
23+
return 1, 0
24+
case "<":
25+
return -1, 0
26+
case "v":
27+
return 0, 1
28+
case other:
29+
raise ValueError(f"Invalid movement: {other}")
30+
31+
1832
class DayRunner(SeparateRunner):
1933
@classmethod
2034
def part1(cls, input: str) -> None:
@@ -24,17 +38,7 @@ def part1(cls, input: str) -> None:
2438
x, y = x[0], y[0]
2539

2640
for c in steps:
27-
match c:
28-
case "^":
29-
dx, dy = 0, -1
30-
case ">":
31-
dx, dy = 1, 0
32-
case "<":
33-
dx, dy = -1, 0
34-
case "v":
35-
dx, dy = 0, 1
36-
case other:
37-
raise ValueError(f"Invalid movement: {other}")
41+
dx, dy = convert_dir(c)
3842

3943
match grid[y + dy, x + dx]:
4044
case "#":
@@ -81,17 +85,7 @@ def part2(cls, input: str) -> None:
8185
x, y = x[0], y[0]
8286

8387
for c in steps:
84-
match c:
85-
case "^":
86-
dx, dy = 0, -1
87-
case ">":
88-
dx, dy = 1, 0
89-
case "<":
90-
dx, dy = -1, 0
91-
case "v":
92-
dx, dy = 0, 1
93-
case other:
94-
raise ValueError(f"Invalid movement: {other}")
88+
dx, dy = convert_dir(c)
9589

9690
match grid[y + dy, x + dx]:
9791
case "#":

0 commit comments

Comments
 (0)