From ff5432d8ea4f435d0df1ff8de9682b1684d9458f Mon Sep 17 00:00:00 2001 From: setop Date: Tue, 24 Dec 2024 02:03:04 +0100 Subject: [PATCH] day 22, slow without hint --- d23/part1.py | 18 ++++++++++++++++++ d23/part2_naive.py | 36 ++++++++++++++++++++++++++++++++++++ d23/part2_set.py | 16 ++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 d23/part1.py create mode 100644 d23/part2_naive.py create mode 100644 d23/part2_set.py diff --git a/d23/part1.py b/d23/part1.py new file mode 100644 index 0000000..d16b443 --- /dev/null +++ b/d23/part1.py @@ -0,0 +1,18 @@ +import sys +from collections import defaultdict as DD + +D = DD(set) +for i in sys.stdin.read().strip().split('\n'): + a, b = i.split("-") + D[a].add(b) + D[b].add(a) + +ans = 0 +for (a,B) in D.items(): + for b in B: + for c in D[b]: + for d in D[c]: + if d == a and a!=b and b!=c and c!=a and (a.startswith("t") or b.startswith("t")or c.startswith("t")): + ans += 1 +print(ans//6) + diff --git a/d23/part2_naive.py b/d23/part2_naive.py new file mode 100644 index 0000000..6d7ad38 --- /dev/null +++ b/d23/part2_naive.py @@ -0,0 +1,36 @@ +import sys +from collections import defaultdict as DD + + +D = DD(set) +S = set() +for i in sys.stdin.read().strip().split('\n'): + a, b = i.split("-") + D[a].add(b) + D[b].add(a) + S.add(a) + S.add(b) + +G = set() +m = 0 +def grow(group:set[str], l:int): + global m + key = ','.join(sorted(group)) + if key in G: + return + G.add(key) + if l>m: + print(l, key) + m = l + rest = S - group + for r in rest: + if len(group & D[r]) == l: + ng = group.copy() + ng.add(r) + yield (l+1, ng) + yield from grow(ng, l+1) + +for a, B in D.items(): + for b in B: + for i,s in grow({a,b},2): + pass diff --git a/d23/part2_set.py b/d23/part2_set.py new file mode 100644 index 0000000..ec0cf31 --- /dev/null +++ b/d23/part2_set.py @@ -0,0 +1,16 @@ +import sys +from collections import defaultdict as DD +from itertools import combinations as comb + +D = DD(set) +for i in sys.stdin.read().strip().split('\n'): + a, b = i.split("-") + D[a].add(b) + D[b].add(a) + +for k, v in D.items(): + for i in v: + w = v - {i} + if all(b in D[a] for a,b in comb(w,2)): + print(",".join(sorted({k}|w))) + #sys.exit(0)