1
1
"""2024 - Day 14 Part 2: Restroom Redoubt"""
2
2
3
+ import sys
3
4
from dataclasses import dataclass
5
+ from itertools import combinations
4
6
5
7
from src .year2024 .day14a import Robot
6
8
7
9
10
+ def distance (r1 : Robot , r2 : Robot ) -> int :
11
+ return abs (r1 .x - r2 .x ) + abs (r1 .y - r2 .y )
12
+
13
+
8
14
@dataclass
9
15
class Field :
10
16
width : int
@@ -16,6 +22,10 @@ def make_step(self) -> None:
16
22
robot .x = (robot .x + robot .dx ) % self .width
17
23
robot .y = (robot .y + robot .dy ) % self .height
18
24
25
+ @property
26
+ def entropy (self ) -> int :
27
+ return sum (distance (a , b ) for a , b in combinations (self .robots , 2 ))
28
+
19
29
def __str__ (self ) -> str :
20
30
data = [[" " for _ in range (self .width )] for _ in range (self .height )]
21
31
for robot in self .robots :
@@ -27,9 +37,15 @@ def solve(task: str, width: int = 101, height: int = 103) -> int:
27
37
robots = [Robot .from_line (x ) for x in task .split ("\n " )]
28
38
field = Field (width , height , robots )
29
39
40
+ min_entropy_second = 0
41
+ min_entropy_seen = sys .maxsize
42
+
30
43
for i in range (1 , 10_000 ):
31
44
field .make_step ()
32
- print ("second" , i )
33
- print (field )
45
+ field_entropy = field .entropy
46
+
47
+ if field_entropy < min_entropy_seen :
48
+ min_entropy_seen = field_entropy
49
+ min_entropy_second = i
34
50
35
- return - 1
51
+ return min_entropy_second
0 commit comments