Skip to content

Commit 2b0eff8

Browse files
committed
AoC 2023 Day 18 - cleanup
1 parent fb01bd3 commit 2b0eff8

File tree

1 file changed

+40
-68
lines changed

1 file changed

+40
-68
lines changed

src/main/python/AoC2023_18.py

Lines changed: 40 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DigInstruction(NamedTuple):
2020
big_amount: int
2121

2222

23-
Input = list[DigInstruction]
23+
Input = InputData
2424
Output1 = int
2525
Output2 = int
2626

@@ -42,89 +42,61 @@ class DigInstruction(NamedTuple):
4242
U 2 (#7a21e3)
4343
"""
4444

45+
DIRS = [
46+
Direction.RIGHT,
47+
Direction.DOWN,
48+
Direction.LEFT,
49+
Direction.UP,
50+
]
51+
4552

4653
class Polygon(NamedTuple):
4754
vertices: list[Position]
4855

4956
def shoelace(self) -> int:
50-
ans = 0
5157
size = len(self.vertices)
52-
for i in range(size):
53-
ans += self.vertices[i].x * (
54-
self.vertices[(i + 1) % size].y - self.vertices[i - 1].y
55-
)
56-
return abs(ans) // 2
58+
s = sum(
59+
self.vertices[i].x
60+
* (self.vertices[(i + 1) % size].y - self.vertices[i - 1].y)
61+
for i in range(size)
62+
)
63+
return abs(s) // 2
5764

5865
def circumference(self) -> int:
59-
ans = 0
60-
for i in range(1, len(self.vertices)):
61-
ans += self.vertices[i].manhattan_distance(self.vertices[i - 1])
62-
return ans
63-
64-
def picks(self) -> int:
65-
a = self.shoelace()
66-
b = self.circumference()
67-
return a - b // 2 + 1
66+
return sum(
67+
self.vertices[i].manhattan_distance(self.vertices[i - 1])
68+
for i in range(1, len(self.vertices))
69+
)
6870

69-
def area(self) -> int:
70-
return self.picks() + self.circumference()
71+
def inside_area(self) -> int:
72+
return self.shoelace() + self.circumference() // 2 + 1
7173

7274

7375
class Solution(SolutionBase[Input, Output1, Output2]):
7476
def parse_input(self, input_data: InputData) -> Input:
75-
ans = []
76-
for line in input_data:
77-
d, a, hx = line.split()
78-
bd = [
79-
Direction.RIGHT,
80-
Direction.DOWN,
81-
Direction.LEFT,
82-
Direction.UP,
83-
][int(hx[7])]
84-
ba = int(hx[2:7], 16)
85-
ans.append(
86-
DigInstruction(
87-
Direction.from_str(d),
88-
int(a),
89-
bd,
90-
ba,
91-
)
92-
)
93-
return ans
77+
return input_data
9478

95-
def to_positions(
96-
self, instructions: list[DigInstruction]
97-
) -> list[Position]:
98-
pos = Position(0, 0)
99-
ans = [pos]
100-
for instruction in instructions:
101-
pos = pos.translate(
102-
instruction.direction.vector, instruction.amount
103-
)
104-
ans.append(pos)
105-
return ans
106-
107-
def to_positions_big(
108-
self, instructions: list[DigInstruction]
109-
) -> list[Position]:
110-
pos = Position(0, 0)
111-
ans = [pos]
79+
def solve(self, instructions: list[tuple[Direction, int]]) -> int:
80+
vertices = [Position(0, 0)]
11281
for instruction in instructions:
113-
pos = pos.translate(
114-
instruction.big_direction.vector, instruction.big_amount
82+
vertices.append(
83+
vertices[-1].translate(instruction[0].vector, instruction[1])
11584
)
116-
ans.append(pos)
117-
return ans
118-
119-
def part_1(self, instructions: Input) -> Output1:
120-
positions = self.to_positions(instructions)
121-
polygon = Polygon(positions)
122-
return polygon.area()
123-
124-
def part_2(self, instructions: Input) -> Output2:
125-
positions = self.to_positions_big(instructions)
126-
polygon = Polygon(positions)
127-
return polygon.area()
85+
return Polygon(vertices).inside_area()
86+
87+
def part_1(self, input: Input) -> Output1:
88+
instructions = [
89+
(Direction.from_str(d), int(a))
90+
for d, a, _ in (line.split() for line in input)
91+
]
92+
return self.solve(instructions)
93+
94+
def part_2(self, input: Input) -> Output2:
95+
instructions = [
96+
(DIRS[int(hx[7])], int(hx[2:7], 16))
97+
for _, _, hx in (line.split() for line in input)
98+
]
99+
return self.solve(instructions)
128100

129101
@aoc_samples(
130102
(

0 commit comments

Comments
 (0)