Skip to content

Commit 3ed51c9

Browse files
Add utilities for 2D navigation
1 parent c4e1f76 commit 3ed51c9

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

2023/python/aoc_2023/day10.py

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
"""Advent of Code 2023 - Day 10."""
22

33
from itertools import pairwise
4-
from aoc_2023.input import Input
54

6-
UP = (0, -1)
7-
DOWN = (0, 1)
8-
LEFT = (-1, 0)
9-
RIGHT = (1, 0)
5+
from aoc_2023.input import Input
6+
from aoc_2023.navigation import UP, DOWN, LEFT, RIGHT
107

118
start_directions = {
129
UP: "|F7",
@@ -24,24 +21,17 @@
2421
"F": {UP: RIGHT, LEFT: DOWN},
2522
}
2623

27-
28-
def move(a: tuple[int, int], b: tuple[int, int]) -> tuple[int, int]:
29-
ax, ay = a
30-
bx, by = b
31-
return ax + bx, ay + by
32-
33-
3424
grid = Input("10.txt").grid
3525
start = next(coord for coord, cell in grid.items() if cell == "S")
36-
direction = next(d for d, s in start_directions.items() if grid.get(move(start, d), "?") in s)
26+
direction = next(d for d, s in start_directions.items() if grid.get(start + d, "?") in s)
3727

38-
position = move(start, direction)
28+
position = start + direction
3929
loop = [start]
4030
while position != start:
4131
loop.append(position)
4232
symbol = grid[position]
4333
direction = directions[symbol][direction]
44-
position = move(position, direction)
34+
position = position + direction
4535

4636
print("Part one:", len(loop) // 2)
4737

2023/python/aoc_2023/day11.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""Advent of Code 2023 - Day 11."""
22

3-
from operator import itemgetter
43
from itertools import combinations
4+
from operator import itemgetter
55
from typing import Generator
66

77
from aoc_2023.input import Input
8+
from aoc_2023.navigation import XY, manhattan_distance
89

910

1011
def expand(rows: list[str], n: int) -> list[list[tuple[int, int, str]]]:
@@ -23,24 +24,17 @@ def expand(rows: list[str], n: int) -> list[list[tuple[int, int, str]]]:
2324
return result
2425

2526

26-
def parse(grid: list[list[tuple[int, int, str]]]) -> Generator[tuple[int, int], None, None]:
27+
def parse(grid: list[list[tuple[int, int, str]]]) -> Generator[XY, None, None]:
2728
y = 0
2829
for row in grid:
2930
x = 0
3031
for w, h, c in row:
3132
if c == "#":
32-
yield x, y
33+
yield XY(x, y)
3334
x += w
3435
y += row[0][1]
3536

3637

37-
def manhattan_distance(p1: tuple[int, int], p2: tuple[int, int]) -> int:
38-
x1, y1 = p1
39-
x2, y2 = p2
40-
41-
return abs(x2 - x1) + abs(y2 - y1)
42-
43-
4438
coordinates = list(parse(expand(Input("11.txt").lines, 2)))
4539
distances = list((p1, p2, manhattan_distance(p1, p2)) for p1, p2 in combinations(coordinates, 2))
4640
print("Part one:", sum(map(itemgetter(2), distances)))

2023/python/aoc_2023/input.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import functools
22
import os
33

4+
from aoc_2023.navigation import XY
5+
46

57
class Input:
68
def __init__(self, name: str):
@@ -20,10 +22,10 @@ def ints(self) -> list[int]:
2022
return list(map(int, self.lines))
2123

2224
@functools.cached_property
23-
def grid(self) -> dict[tuple[int, int], str]:
25+
def grid(self) -> dict[XY, str]:
2426
result = {}
2527
for y, row in enumerate(self.lines):
2628
for x, cell in enumerate(row):
27-
result[x, y] = cell
29+
result[XY(x, y)] = cell
2830

2931
return result

2023/python/aoc_2023/navigation.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from dataclasses import dataclass
2+
from typing import Iterator
3+
4+
5+
@dataclass(frozen=True)
6+
class XY:
7+
"""Represents a point on a 2-dimensional grid."""
8+
9+
x: int
10+
y: int
11+
12+
def __add__(self, other):
13+
return XY(self.x + other.x, self.y + other.y)
14+
15+
def __sub__(self, other):
16+
return XY(self.x - other.x, self.y - other.y)
17+
18+
def __iter__(self) -> Iterator[int]:
19+
yield from [self.x, self.y]
20+
21+
22+
UP = XY(0, -1)
23+
DOWN = XY(0, 1)
24+
LEFT = XY(-1, 0)
25+
RIGHT = XY(1, 0)
26+
27+
28+
def manhattan_distance(p1: XY, p2: XY) -> int:
29+
"""
30+
Calculates the manhattan distance between two points on a 2-dimensional grid.
31+
"""
32+
return abs(p2.x - p1.x) + abs(p2.y - p1.y)

0 commit comments

Comments
 (0)