Aoc d 23
PantsB
python
2 months ago
1.5 kB
63
No Index
from functools import reduce import time filepath = 'input.txt' advent1file=open(filepath,'r') start_time = time.time() listoflines =advent1file.readlines() conn={} comps=set() for lines in listoflines: one,two=lines.split('-') two=two[:2] comps.add(one) comps.add(two) if one not in conn.keys(): conn[one]=set() conn[one].add(two) if two not in conn.keys(): conn[two]=set() conn[two].add(one) for k in conn.keys(): conn[k]=list(conn[k]) conn[k].sort() #found=set() largest=3 comps=list(comps) comps.sort() for cpu in comps: if cpu[0]!='t': continue if len(conn[cpu])<2: continue for i in range(len(conn[cpu])): networked=conn[cpu][i] LAN=[cpu,networked] for j in range(i,len(conn[networked])): thirdman=conn[networked][j] if thirdman in conn[cpu]: LAN.append(thirdman) if len(LAN)>largest: candidate=True for c in LAN: both=[v for v in conn[c] if v in LAN] if len(both)+1!=len(LAN): candidate=False break if candidate: LAN.sort() largest=len(LAN) print(reduce(lambda x,y:str(x) + ',' + str(y),LAN)) #print(len(found)) tim=time.time()-start_time print(tim)
Editor is loading...
Leave a Comment