11"""Day 6 - Trash Compactor"""
22
33from math import prod
4- from typing import List
4+ import re
55from utils .aoc_utils import report_results , AoCResult , input_for_day
66
7+ # DISCLAIMER - THIS IS SO MESSY AND TYPING IS A MESS
8+ # BUT IT WORKS.
79
8- HomeworkStr = list [list [str ]]
9- HomeworkInt = List [list [str | int ]]
10+ GridSplits = list [list [str ]] # output of format as splits
11+ # e.g. [['123', '343'],['45', '500']]
12+ GridTokens = list [list [str ]] # output of format as lists
13+ # e.g. [['1', '2', '3', ' ', '3', '4', '3'], [...]]
14+ HomeworkInts = list [list [int | str ]] # [[1, 2, '+']]
15+ ColumnsStr = list [list [str ]]
1016
1117EXAMPLE : str = """123 328 51 64
1218 45 64 387 23
1319 6 98 215 314
1420* + * + """
15- DATA = input_for_day (6 , 2025 )
21+ DATA : str = input_for_day (6 , 2025 )
1622
1723
18- def format_homework ( data : str ) -> HomeworkStr :
19- return [ line . split () for line in data . splitlines ()]
24+ def format_homework_as_splits ( puzzle_input : str ) -> GridSplits :
25+ """Format homework by splitting on spaces.
2026
27+ Args:
28+ puzzle_input (str): Input puzzle grid.
2129
22- EXAMPLE_LINES : HomeworkStr = format_homework (EXAMPLE )
23- DATA_LINES : HomeworkStr = format_homework (DATA )
24- #print([list(x) for x in EXAMPLE.splitlines()])
25- # transpose
26- tmp = [list (x ) for x in EXAMPLE .splitlines ()]
30+ Returns:
31+ GridSplits: [['40', '30', '1'], ['10', '20', '3']]
32+ """
33+ return [line .split () for line in puzzle_input .splitlines ()]
2734
28- tmptmp = []
29- for i in range (len (tmp [0 ])):
30- n : list [int | str ] = []
31- for row in tmp :
32- try :
33- n .append (row [i ])
34- except ValueError :
35- n .append (row [i ])
36- tmptmp .append (n )
37- for x in tmptmp :
38- y = '' .join (x )
39- print (y )
4035
36+ def format_homework_as_lists (puzzle_input : str ) -> GridTokens :
37+ """Format homework; everything is a list!
4138
42- def rearrange_homework (data : HomeworkStr ) -> HomeworkInt :
43- new : HomeworkInt = []
44- print (len (data [0 ]))
39+ Args:
40+ puzzle_input (str): Input puzzle grid.
41+
42+ Returns:
43+ GridTokens: [['4', '0', '3', '0', '1'], ['1', '0', '2', '0', '3']]
44+ """
45+ return [list (line ) for line in puzzle_input .splitlines ()]
46+
47+
48+ def rearrange_homework_ints (data : GridSplits ) -> HomeworkInts :
49+ """Rearrange homework and convert to ints.
50+
51+ Args:
52+ data (GridSplits): e.g. [['40', '30', '1'], ['10', '20', '3']]
53+
54+ Returns:
55+ HomeworkInts: [[40, 10], [30, 20],[1, 3]]
56+ """
57+ new : HomeworkInts = []
4558 for i in range (len (data [0 ])):
4659 n : list [int | str ] = []
4760 for row in data :
@@ -53,34 +66,135 @@ def rearrange_homework(data: HomeworkStr) -> HomeworkInt:
5366 return new
5467
5568
56- def calculate_homework (data : HomeworkStr ) -> int :
57- homework : HomeworkInt = rearrange_homework (data )
69+ def rearrange_homework_str (data : GridTokens ) -> ColumnsStr :
70+ """Rearrange homework into R-L cols?
71+
72+ Args:
73+ data (GridTokens): [['A', 'B', 'C'], ['1', '2', '3']]
74+
75+ Returns:
76+ ColumnsStr: [['A', '1'], ['B', '2'], ['C', '3']... etc]
77+ """
78+ new : ColumnsStr = []
79+ for i in range (len (data [0 ])):
80+ col : list [str ] = []
81+ for row in data :
82+ col .append (row [i ])
83+ new .append (col )
84+ return new
85+
86+
87+ def add_or_sum (numbers : list [int ], operator : str ) -> int :
88+ """Add or sum the numbers provided, depending on operator.
89+
90+ Args:
91+ numbers (list[int]): [1, 2, 3]
92+ operator (str): * or +
93+
94+ Returns:
95+ int: result of operation on list.
96+ """
97+ if operator == '+' :
98+ return sum (numbers )
99+ elif operator == '*' :
100+ return prod (numbers )
101+ else :
102+ raise ValueError ('What operator is that?!' )
103+
104+
105+ def calculate_homework (data : str ) -> int :
106+ """PART 1:
107+ Data comes in as string.
108+ Gets split into lines and then each line split by space.
109+ Each string number gets converted to int, and operator should
110+ be final element in list.
111+
112+ Then for each row [int, int, int, operator], get the value of
113+ them sum or prod, add to total.
114+
115+ Args:
116+ data (str): puzzle input data.
117+
118+ Returns:
119+ int: answer for part 1
120+ """
121+ homework : GridSplits = format_homework_as_splits (data )
122+ int_homework : HomeworkInts = rearrange_homework_ints (homework )
58123 total : int = 0
59- for row in homework :
124+
125+ for row in int_homework :
60126 nums : list [int ] = [v for v in row [:- 1 ] if isinstance (v , int )]
61- if row [- 1 ] == '+' :
62- total += sum ( nums )
63- elif row [ - 1 ] == '*' :
64- total += prod (nums )
127+ sign = row [- 1 ]
128+ assert isinstance ( sign , str )
129+
130+ total += add_or_sum (nums , sign )
65131 return total
66132
67133
134+ def solve_p2 (data : str ) -> int :
135+ """PART 2:
136+ Input data as str, gets split into lines, then each line split
137+ into a list.
138+ It then gets rearranged column wise, and seperated whenever
139+ there's a column of all spaces.
140+ It ends up as a janky string so then just regex the numbers/operators
141+ and do the same sum/prod to get total.
142+
143+ Args:
144+ data (str): _description_
145+
146+ Returns:
147+ int: _description_
148+ """
149+ formatted_data : GridTokens = format_homework_as_lists (data )
150+ max_len : int = max (len (x ) for x in formatted_data )
151+
152+ for grid_row in formatted_data :
153+ extra : list [str ] = [' ' ] * (max_len - len (grid_row ))
154+ grid_row .extend (extra )
155+
156+ homework : GridTokens = rearrange_homework_str (formatted_data )
157+ t2 : list [str ] = []
158+ current : list [str ] = []
159+
160+ # col of all spaces seperates 'problems'
161+ for col in homework :
162+ if all (c == ' ' for c in col ):
163+ if current :
164+ t2 .append ('' .join (current ))
165+ current = []
166+ else :
167+ current .append ('' .join (col ))
168+
169+ if current :
170+ t2 .append ('' .join (current ))
171+
172+ total_p2 : int = 0
173+
174+ for x in t2 :
175+ numbers = list (map (int , re .findall (r'\d+' , x )))
176+ sign = re .findall (r'\*|\+' , x )
177+ total_p2 += add_or_sum (numbers , sign [0 ])
178+
179+ return total_p2
180+
181+
68182@report_results
69- def solveday (data : HomeworkStr ) -> AoCResult :
183+ def solveday (data : str ) -> AoCResult :
70184 p1 : int = calculate_homework (data )
71- p2 : int = 0
185+ p2 : int = solve_p2 ( data )
72186 return p1 , p2
73187
74188
75- expected_test_results : AoCResult = (4277556 , 0 )
189+ expected_test_results : AoCResult = (4277556 , 3263827 )
76190
77191
78- def tests (test_input : HomeworkStr ) -> None :
192+ def tests (test_input : str ) -> None :
79193 p1 , p2 = solveday (test_input )
80194 assert (p1 , p2 ) == expected_test_results
81195 print ("☑️ Tests passed!" )
82196
83197
84198if __name__ == "__main__" :
85- tests (EXAMPLE_LINES )
86- # solveday(DATA_LINES )
199+ tests (EXAMPLE )
200+ solveday (DATA )
0 commit comments