diff --git a/2024/23/23-1.py b/2024/23/23-1.py new file mode 100644 index 0000000..dc9bc0d --- /dev/null +++ b/2024/23/23-1.py @@ -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()) + \ No newline at end of file diff --git a/2024/23/testinput.txt b/2024/23/testinput.txt new file mode 100644 index 0000000..d8576a8 --- /dev/null +++ b/2024/23/testinput.txt @@ -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 \ No newline at end of file