-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from collections import Counter | ||
import functools | ||
|
||
pairs = set() | ||
trips = set() | ||
|
||
def findPossibleHistorianConnections(): | ||
possibleConnections = 0 | ||
for triplet in trips: | ||
for connection in triplet: | ||
firstChar = connection[0] | ||
if firstChar == 't': | ||
possibleConnections += 1 | ||
break | ||
|
||
return possibleConnections | ||
|
||
@functools.cache | ||
def maybeAddTriplet(newTriplet): | ||
trips.add(newTriplet) | ||
|
||
@functools.cache | ||
def interconnected(connections): | ||
first, second, third = connections | ||
rules = [(first, second) in pairs or (second, first) in pairs, | ||
(first, third) in pairs or (third, first) in pairs, | ||
(second, third) in pairs or (third, second) in pairs] | ||
|
||
return True if all(rules) else False | ||
|
||
|
||
def makeAllPossibleConnections(pair): | ||
first, second = pair | ||
|
||
for otherPair in pairs: | ||
if Counter(pair) == Counter(otherPair): | ||
continue | ||
|
||
otherFirst, otherSecond = otherPair | ||
|
||
if otherFirst in [first, second]: | ||
connections = (first, second, otherSecond) | ||
connectionsSorted = tuple(sorted(connections)) | ||
if interconnected(connectionsSorted): | ||
maybeAddTriplet(connectionsSorted) | ||
elif otherSecond in [first, second]: | ||
connections = (first, second, otherFirst) | ||
connectionsSorted = tuple(sorted(connections)) | ||
if interconnected(connectionsSorted): | ||
maybeAddTriplet(connectionsSorted) | ||
|
||
|
||
|
||
|
||
with open('2024/23/input.txt') as f: | ||
for line in f: | ||
line = line.strip() | ||
|
||
pairs.add(tuple(line.split('-'))) | ||
|
||
print(pairs) | ||
print(len(pairs)) | ||
for i, pair in enumerate(pairs): | ||
makeAllPossibleConnections(pair) | ||
|
||
print(len(trips)) | ||
|
||
print(findPossibleHistorianConnections()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 |