Skip to content

Commit 59a8296

Browse files
2023 day 10
1 parent 65d6e45 commit 59a8296

File tree

6 files changed

+220
-2
lines changed

6 files changed

+220
-2
lines changed

2023/python/aoc_2023/day10.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Advent of Code 2023 - Day 10."""
2+
3+
from itertools import pairwise
4+
from aoc_2023.input import Input
5+
6+
UP = (0, -1)
7+
DOWN = (0, 1)
8+
LEFT = (-1, 0)
9+
RIGHT = (1, 0)
10+
11+
start_directions = {
12+
UP: "|F7",
13+
DOWN: "|JL",
14+
RIGHT: "-J7",
15+
LEFT: "-FL",
16+
}
17+
18+
directions = {
19+
"-": {LEFT: LEFT, RIGHT: RIGHT},
20+
"|": {UP: UP, DOWN: DOWN},
21+
"L": {LEFT: UP, DOWN: RIGHT},
22+
"J": {RIGHT: UP, DOWN: LEFT},
23+
"7": {RIGHT: DOWN, UP: LEFT},
24+
"F": {UP: RIGHT, LEFT: DOWN},
25+
}
26+
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+
34+
grid = Input("10.txt").grid
35+
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)
37+
38+
position = move(start, direction)
39+
loop = [start]
40+
while position != start:
41+
loop.append(position)
42+
symbol = grid[position]
43+
direction = directions[symbol][direction]
44+
position = move(position, direction)
45+
46+
print("Part one:", len(loop) // 2)
47+
48+
area = abs(sum(x1 * y2 - x2 * y1 for (x1, y1), (x2, y2) in pairwise([*loop, start]))) // 2
49+
50+
print("Part two:", area - len(loop) // 2 + 1)

2023/python/aoc_2023/input.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@ def text(self) -> str:
1313

1414
@functools.cached_property
1515
def lines(self) -> list[str]:
16-
return [line.rstrip() for line in self._text.splitlines()]
16+
return list(line.rstrip() for line in self.text.splitlines())
1717

1818
@functools.cached_property
1919
def ints(self) -> list[int]:
20-
return [int(line.rstrip()) for line in self._text.splitlines()]
20+
return list(map(int, self.lines))
21+
22+
@functools.cached_property
23+
def grid(self) -> dict[tuple[int, int], str]:
24+
result = {}
25+
for y, row in enumerate(self.lines):
26+
for x, cell in enumerate(row):
27+
result[x, y] = cell
28+
29+
return result

2023/python/input/10.example-1.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-L|F7
2+
7S-7|
3+
L|7||
4+
-L-J|
5+
L|-JF

2023/python/input/10.example-2.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
7-F7-
2+
.FJ|7
3+
SJLL7
4+
|F--J
5+
LJ.LJ

2023/python/input/10.example-3.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
...........
2+
.S-------7.
3+
.|F-----7|.
4+
.||.....||.
5+
.||.....||.
6+
.|L-7.F-J|.
7+
.|..|.|..|.
8+
.L--J.L--J.
9+
...........

0 commit comments

Comments
 (0)