1111to satisfy numeric requirements for each machine.
1212"""
1313
14+ from collections import deque
1415import math
1516import re
17+ from typing import ClassVar
1618
17- from collections import deque
18- from itertools import combinations
1919from numpy import transpose
2020from scipy .optimize import linprog
21- from typing import ClassVar
2221
2322from aoc .models .base import SolutionBase
2423
@@ -71,14 +70,11 @@ def parse_data(self, line: str) -> tuple[list[str], list[tuple[int, ...]], list[
7170 curly = list (map (int , gr3 .split ("," )))
7271
7372 if square is None or curly is None :
74- raise ValueError (f"Invalid line (missing [] or {{}}): { line !r} " )
73+ err_msg = f"Invalid line (missing [] or {{}}): { line !r} "
74+ raise ValueError (err_msg )
7575
7676 if curly :
77- useful_parentheses : list [tuple [int , ...]] = []
78- for btn in parentheses :
79- if any (curly [idx ] > 0 for idx in btn ):
80- useful_parentheses .append (btn )
81-
77+ useful_parentheses = [btn for btn in parentheses if any (curly [idx ] > 0 for idx in btn )]
8278 parentheses = useful_parentheses
8379
8480 return square , parentheses , curly
@@ -87,9 +83,7 @@ def to_light_state(self, lights: list[str]) -> tuple[int, ...]:
8783 """Convert a light pattern ('.'/'#') into a boolean tuple state."""
8884 return tuple (ch == "#" for ch in lights )
8985
90- def apply_button (
91- self , state : tuple [int , ...], button : tuple [int , ...]
92- ) -> tuple [int , ...]:
86+ def apply_button (self , state : tuple [int , ...], button : tuple [int , ...]) -> tuple [int , ...]:
9387 """Toggle a set of indicator lights according to a button wiring.
9488
9589 Args:
@@ -106,9 +100,7 @@ def apply_button(
106100
107101 return tuple (arr )
108102
109- def min_presses_for_lights (
110- self , lights : list [str ], buttons : list [tuple [int , ...]]
111- ) -> int :
103+ def min_presses_for_lights (self , lights : list [str ], buttons : list [tuple [int , ...]]) -> int :
112104 """Compute minimum presses to reach target light pattern using BFS.
113105
114106 Treats each distinct light state as a node in a graph and each button
@@ -149,7 +141,8 @@ def min_presses_for_lights(
149141 if nxt not in visited :
150142 q .append ((nxt , steps + 1 ))
151143
152- raise ValueError (f"Unreachable lights pattern { lights } with given buttons" )
144+ err_msg = f"Unreachable lights pattern { lights } with given buttons"
145+ raise ValueError (err_msg )
153146
154147 def button_to_vector (self , button : tuple [int , ...], num_slots : int ) -> list [int ]:
155148 """Convert a button wiring into a vector for the joltage equation system.
@@ -168,7 +161,9 @@ def button_to_vector(self, button: tuple[int, ...], num_slots: int) -> list[int]
168161 return vec
169162
170163 def min_presses_for_machine (
171- self , buttons : list [tuple [int , ...]], target : list [int ],
164+ self ,
165+ buttons : list [tuple [int , ...]],
166+ target : list [int ],
172167 ) -> int :
173168 """Compute minimum button presses to satisfy machine joltage constraints.
174169
@@ -191,20 +186,21 @@ def min_presses_for_machine(
191186 if not target :
192187 return 0
193188
194- N = len (buttons )
189+ N = len (buttons ) # noqa: N806
195190 num_jolt = len (target )
196191
197192 if N == 0 :
198193 if any (t != 0 for t in target ):
199- raise ValueError (f"Unreachable target { target } with given buttons" )
194+ err_msg = f"Unreachable target { target } with given buttons"
195+ raise ValueError (err_msg )
200196 return 0
201197
202198 # Objective: minimize total button presses
203199 c = [1 ] * N
204200
205201 # Build equality constraints: sum(button_vectors * presses) = target
206- A_eq = [self .button_to_vector (btn , num_jolt ) for btn in buttons ]
207- A_eq = transpose (A_eq )
202+ A_eq = [self .button_to_vector (btn , num_jolt ) for btn in buttons ] # noqa: N806
203+ A_eq = transpose (A_eq ) # noqa: N806
208204 b_eq = target
209205 integrality = [1 ] * N
210206
@@ -216,7 +212,8 @@ def min_presses_for_machine(
216212 )
217213
218214 if not res .success :
219- raise ValueError (f"Unreachable target { target } with given buttons" )
215+ err_msg = f"Unreachable target { target } with given buttons"
216+ raise ValueError (err_msg )
220217
221218 return int (math .ceil (sum (res .x )))
222219
0 commit comments