@@ -20,7 +20,7 @@ class DigInstruction(NamedTuple):
20
20
big_amount : int
21
21
22
22
23
- Input = list [ DigInstruction ]
23
+ Input = InputData
24
24
Output1 = int
25
25
Output2 = int
26
26
@@ -42,89 +42,61 @@ class DigInstruction(NamedTuple):
42
42
U 2 (#7a21e3)
43
43
"""
44
44
45
+ DIRS = [
46
+ Direction .RIGHT ,
47
+ Direction .DOWN ,
48
+ Direction .LEFT ,
49
+ Direction .UP ,
50
+ ]
51
+
45
52
46
53
class Polygon (NamedTuple ):
47
54
vertices : list [Position ]
48
55
49
56
def shoelace (self ) -> int :
50
- ans = 0
51
57
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
57
64
58
65
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
+ )
68
70
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
71
73
72
74
73
75
class Solution (SolutionBase [Input , Output1 , Output2 ]):
74
76
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
94
78
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 )]
112
81
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 ])
115
84
)
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 )
128
100
129
101
@aoc_samples (
130
102
(
0 commit comments