2024 - Day 23 (LAN Party)
View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
#Advent of Code
#2024 Day 23, Part 1
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from collections import defaultdict
from itertools import combinations
def solve(puzzle_input: list[str]) -> int:
computers: defaultdict[str, set[str]] = defaultdict(set)
for connection in puzzle_input:
a, b = connection.split("-")
computers[a].add(b)
computers[b].add(a)
considered_sets: set[str] = set()
found_sets: set[str] = set()
for initial in computers:
if len(computers[initial]) >= 2:
for connected in combinations(computers[initial], 2):
computer_set: set[str] = {initial}.union(connected)
if str(sorted(computer_set)) in considered_sets:
continue
considered_sets.add(str(sorted(computer_set)))
if not any(computer[0] == "t" for computer in computer_set):
continue
valid: bool = True
for root in computer_set:
for other in computer_set:
if root == other:
continue
elif other not in computers[root] or root not in computers[other]:
valid = False
break
if not valid:
break
if valid:
found_sets.add(str(sorted(computer_set)))
return len(found_sets)
def main() -> tuple[str, int]:
puzzle_input = util.read.as_lines()
return "The number of sets of three inter-connected computers where at least one computer name starts with t is {}.", solve(puzzle_input)
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve(["kh-tc",
"qp-kh",
"de-cg",
"ka-co",
"yn-aq",
"qp-ub",
"cg-tb",
"vc-aq",
"tb-ka",
"wh-tc",
"yn-cg",
"kh-ub",
"ta-co",
"de-co",
"tc-td",
"tb-wq",
"wh-td",
"ta-ka",
"td-qp",
"aq-cg",
"wq-ub",
"ub-vc",
"de-ta",
"wq-aq",
"wq-vc",
"wh-yn",
"ka-de",
"kh-ta",
"co-tc",
"wh-qp",
"tb-vc",
"td-yn"]), 7)
if __name__ == "__main__":
run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
#Advent of Code
#2024 Day 23, Part 2
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from collections import defaultdict
from itertools import combinations
def solve(puzzle_input: list[str]) -> str:
computers: defaultdict[str, set[str]] = defaultdict(set)
for connection in puzzle_input:
a, b = connection.split("-")
computers[a].add(b)
computers[b].add(a)
considered_sets: set[str] = set()
found_sets: dict[str, int] = {}
max_connections: int = max(map(len, computers.values()))
for initial in computers:
for other_amount in range(1, max_connections + 1):
for connected in combinations(computers[initial], other_amount):
computer_set: set[str] = {initial}.union(connected)
if (computer_set_name := ",".join(sorted(computer_set))) in considered_sets:
continue
considered_sets.add(computer_set_name)
if not any(computer[0] == "t" for computer in computer_set):
continue
valid: bool = True
for root in computer_set:
for other in computer_set:
if root == other:
continue
elif other not in computers[root] or root not in computers[other]:
valid = False
break
if not valid:
break
if valid:
found_sets[computer_set_name] = len(computer_set)
largest_set: int = max(found_sets.values())
return [password for password, length in found_sets.items() if length == largest_set][0]
def main() -> tuple[str, str]:
puzzle_input = util.read.as_lines()
return "The password to get into the LAN party is {}.", solve(puzzle_input)
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve(["kh-tc",
"qp-kh",
"de-cg",
"ka-co",
"yn-aq",
"qp-ub",
"cg-tb",
"vc-aq",
"tb-ka",
"wh-tc",
"yn-cg",
"kh-ub",
"ta-co",
"de-co",
"tc-td",
"tb-wq",
"wh-td",
"ta-ka",
"td-qp",
"aq-cg",
"wq-ub",
"ub-vc",
"de-ta",
"wq-aq",
"wq-vc",
"wh-yn",
"ka-de",
"kh-ta",
"co-tc",
"wh-qp",
"tb-vc",
"td-yn"]), "co,de,ka,ta")
if __name__ == "__main__":
run(main)