1
- import math
2
- from typing import Tuple , List , Set
3
-
4
- import tqdm
5
-
6
- from shared .paul2708 .input_reader import *
7
- from shared .paul2708 .output import write
8
-
9
- import math
10
- from typing import Tuple , List , Set
11
-
12
- import tqdm
1
+ from typing import Collection
13
2
14
3
from shared .paul2708 .input_reader import *
15
4
from shared .paul2708 .output import write
18
7
links = [tuple (line .split ("-" )) for line in lines ]
19
8
computers = set ([a for a , b in links ] + [b for a , b in links ])
20
9
10
+ connected_to = set ()
21
11
22
- def is_connected_to (from_node , to_node ):
23
- for a , b in links :
24
- if from_node == a and to_node == b or from_node == b and to_node == a :
25
- return True
26
-
27
- return False
12
+ for a , b in links :
13
+ connected_to .add ((a , b ))
14
+ connected_to .add ((b , a ))
28
15
29
16
30
- connections = []
17
+ def is_connected_to (from_node : str , to_node : str ) -> bool :
18
+ return (from_node , to_node ) in connected_to
31
19
32
- for a , b in tqdm .tqdm (links ):
33
- for c in computers :
34
- if is_connected_to (a , c ) and is_connected_to (b , c ):
35
- if {a , b , c } in connections :
36
- continue
37
20
38
- connections .append ({a , b , c })
21
+ def is_connected_to_all (from_nodes : Collection [str ], to_node : str ) -> bool :
22
+ for from_node in from_nodes :
23
+ if not is_connected_to (from_node , to_node ):
24
+ return False
39
25
40
- res = 0
41
- for nodes in tqdm .tqdm (connections ):
42
- if any (map (lambda n : n .startswith ("t" ), nodes )):
43
- res += 1
26
+ return True
44
27
45
- print (res )
46
28
47
- lines = read_plain_input (day = 23 , example = None )
48
- links = [tuple (line .split ("-" )) for line in lines ]
49
- computers = set ([a for a , b in links ] + [b for a , b in links ])
50
-
51
- connected_to = dict ()
29
+ # Part 1
30
+ connections = []
52
31
53
32
for a , b in links :
54
- connected_to [(a , b )] = True
55
- connected_to [(b , a )] = True
56
-
57
-
58
- def is_connected_to (from_node , to_node ):
59
- return (from_node , to_node ) in connected_to
60
-
61
-
62
- def is_connected_to_all (from_nodes , to_node ):
63
- for f in from_nodes :
64
- if not is_connected_to (f , to_node ):
65
- return False
33
+ for c in computers :
34
+ if is_connected_to (a , c ) and is_connected_to (b , c ):
35
+ connections .append ({a , b , c })
66
36
67
- return True
37
+ matches = 0
38
+ for nodes in connections :
39
+ if any (map (lambda node : node .startswith ("t" ), nodes )):
40
+ matches += 1
68
41
42
+ write (f"<{ matches // 3 } > triple connections contain at least one computer <starting with t>." )
69
43
44
+ # Part 2
70
45
max_links = []
71
- for a , b in tqdm .tqdm (links ):
46
+
47
+ for a , b in links :
72
48
curr_links = {a , b }
73
49
curr_len = 0
74
50
@@ -82,5 +58,5 @@ def is_connected_to_all(from_nodes, to_node):
82
58
if len (curr_links ) > len (max_links ):
83
59
max_links = curr_links
84
60
85
-
86
- print ( "," . join ( sorted ( max_links )) )
61
+ password = "," . join ( sorted ( max_links ))
62
+ write ( f"The password is < { password } >." )
0 commit comments