11"""Everyone Codes Day N."""
22
3- import logging
43import math
54import re
65
7- log = logging .info
86
97def solve (part : int , data : str ) -> int :
108 """Solve the parts."""
11- steps = 100
12-
9+ snails = []
10+ for line in data .splitlines ():
11+ match = re .match (r"x=(.*) y=(.*)" , line )
12+ assert match
13+ x , y = [int (i ) for i in match .groups ()]
14+ period = x + y - 1
15+ snails .append ((x , y , period ))
16+
1317 if part == 1 :
1418 total = 0
15- for line in data .splitlines ():
16- x , y = [int (i ) for i in re .match (r"x=(.*) y=(.*)" , line ).groups ()]
17- period = x + y - 1
19+ steps = 100
20+ for x , y , period in snails :
1821 final_x = (x + steps - 1 ) % period + 1
1922 final_y = (y - steps - 1 ) % period + 1
2023 total += final_x + 100 * final_y
2124 return total
2225
23- if part == 2 or part == 3 :
24- step = 1
25- offset = 0
26- for line in data .splitlines ():
27- x , y = [int (i ) for i in re .match (r"x=(.*) y=(.*)" , line ).groups ()]
28- period = x + y - 1
29- while (y - offset - 1 ) % period != 0 :
30- offset += step
31- step = math .lcm (step , period )
26+ combined_period = 1
27+ days = 0
28+ for x , y , period in snails :
29+ while (y - days - 1 ) % period != 0 :
30+ days += combined_period
31+ combined_period = math .lcm (combined_period , period )
3232
33- return offset
33+ return days
3434
3535
3636TEST_DATA = [
@@ -56,5 +56,4 @@ def solve(part: int, data: str) -> int:
5656 (1 , TEST_DATA [0 ], 1310 ),
5757 (2 , TEST_DATA [1 ], 14 ),
5858 (2 , TEST_DATA [2 ], 13659 ),
59- # (3, TEST_DATA[2], None),
6059]
0 commit comments