Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit d5b696f

Browse files
authored
Refactor solution
1 parent 25ccb9c commit d5b696f

File tree

1 file changed

+27
-51
lines changed

1 file changed

+27
-51
lines changed

Day-23/python/paul2708/day23.py

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
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
132

143
from shared.paul2708.input_reader import *
154
from shared.paul2708.output import write
@@ -18,57 +7,44 @@
187
links = [tuple(line.split("-")) for line in lines]
198
computers = set([a for a, b in links] + [b for a, b in links])
209

10+
connected_to = set()
2111

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))
2815

2916

30-
connections = []
17+
def is_connected_to(from_node: str, to_node: str) -> bool:
18+
return (from_node, to_node) in connected_to
3119

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
3720

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
3925

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
4427

45-
print(res)
4628

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 = []
5231

5332
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})
6636

67-
return True
37+
matches = 0
38+
for nodes in connections:
39+
if any(map(lambda node: node.startswith("t"), nodes)):
40+
matches += 1
6841

42+
write(f"<{matches // 3}> triple connections contain at least one computer <starting with t>.")
6943

44+
# Part 2
7045
max_links = []
71-
for a, b in tqdm.tqdm(links):
46+
47+
for a, b in links:
7248
curr_links = {a, b}
7349
curr_len = 0
7450

@@ -82,5 +58,5 @@ def is_connected_to_all(from_nodes, to_node):
8258
if len(curr_links) > len(max_links):
8359
max_links = curr_links
8460

85-
86-
print(",".join(sorted(max_links)))
61+
password = ",".join(sorted(max_links))
62+
write(f"The password is <{password}>.")

0 commit comments

Comments
 (0)