Skip to content

Commit

Permalink
23-1
Browse files Browse the repository at this point in the history
  • Loading branch information
koosvary committed Dec 23, 2024
1 parent 3642934 commit 06426ca
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
69 changes: 69 additions & 0 deletions 2024/23/23-1.py
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())

32 changes: 32 additions & 0 deletions 2024/23/testinput.txt
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

0 comments on commit 06426ca

Please sign in to comment.