Skip to content

Commit 6c76d7c

Browse files
committed
feat: 2024 day 23
1 parent 35e43dc commit 6c76d7c

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

2024/23a.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from aoc import *
2+
from itertools import combinations
3+
from collections import defaultdict
4+
5+
6+
def main(infi: str):
7+
sets = set()
8+
connections = defaultdict(set)
9+
computers = set()
10+
for l in lines_stripped(infi):
11+
left, right = l.split('-')
12+
connections[left].add(right)
13+
connections[right].add(left)
14+
computers.add(left)
15+
computers.add(right)
16+
for a, b, c in combinations(computers, 3):
17+
if b in connections[a] and b in connections[c] and c in connections[a]:
18+
sets.add((a, b, c))
19+
return sum(1 for a, b, c in sets if any(pc[0] == 't' for pc in (a, b, c)))
20+
21+
22+
DAY = 23
23+
FILE_TEST = f"{DAY}_testa.txt"
24+
# FILE_TEST = f"{DAY}_testb.txt"
25+
FILE_EXP = f"{DAY}_exp.txt"
26+
FILE = f"{DAY}.txt"
27+
# test_and_submit(main, FILE_TEST, FILE_EXP, FILE, DAY)
28+
# print(main(FILE_TEST))
29+
print(main(FILE))

2024/23b.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from aoc import *
2+
from functools import reduce
3+
from collections import defaultdict
4+
import operator
5+
6+
7+
def main(infi: str):
8+
connections = defaultdict(set)
9+
pairs = set()
10+
networks = set()
11+
for l in lines_stripped(infi):
12+
left, right = l.split('-')
13+
connections[left].add(right)
14+
connections[right].add(left)
15+
pairs.add(frozenset((left, right)))
16+
17+
done = set()
18+
19+
def create_networks(pair):
20+
fpair = frozenset(pair)
21+
if fpair in done:
22+
return
23+
done.add(fpair)
24+
25+
while newpcs := reduce(operator.and_, (connections[pc] for pc in pair)):
26+
if len(newpcs) > 1:
27+
for newpc in newpcs:
28+
create_networks(pair | set((newpc,)))
29+
return
30+
pair.update(newpcs)
31+
networks.add(frozenset(pair))
32+
33+
for pair in pairs:
34+
pair = set(pair)
35+
create_networks(pair)
36+
37+
return ','.join(sorted(list(max(networks, key=lambda x: len(x)))))
38+
39+
40+
DAY = 23
41+
FILE_TEST = f"{DAY}_testa.txt"
42+
# FILE_TEST = f"{DAY}_testb.txt"
43+
FILE_EXP = f"{DAY}_exp.txt"
44+
FILE = f"{DAY}.txt"
45+
# test_and_submit(main, FILE_TEST, FILE_EXP, FILE, DAY)
46+
# print(main(FILE_TEST))
47+
print(main(FILE))

README.org

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Personal Stats 2024
55
| Day | Time | Rank | Score | Time | Rank | Score |
66
|-----+----------+------+-------+----------+-------+-------|
7+
| 23 | 00:24:26 | 2632 | 0 | 01:09:24 | 2938 | 0 |
78
| 22 | 00:07:06 | 766 | 0 | 05:23:29 | 7143 | 0 |
89
| 19 | 01:44:22 | 6641 | 0 | 01:50:06 | 5444 | 0 |
910
| 17 | 00:27:08 | 1748 | 0 | 07:06:10 | 5688 | 0 |

0 commit comments

Comments
 (0)