#!/bin/python3 import sys,re from copy import deepcopy from pprint import pprint sys.path.insert(0, '../../') from fred import list2int, lprint,get_re input_f = 'test' part = 1 log = True ######################################### # # # Part 1 # # # ######################################### # def find_match(comp:list,match:str,result:list): # #i_match = get_re(r"^(\d+)\/(\d+)$",match) # #print(comp,match) # #comp = [i for i in comp if i != match] # #print(comp,result) # #for i in result: # # try: # # comp = [j for j in comp if j != i] # # except: # # print(i,'cant be removed from',j) # for j in comp: # j_match = get_re(r"^(\d+)\/(\d+)$",j) # #print(match,j) # #input() # if i_match.group(2) == j_match.group(1) or i_match.group(2) == j_match.group(2): # result.append(j) # find_match(comp[:],match,result[:]) # return result # def find_match(components,match,results): # #print(match,results) # for j in components[:]: # if match[1] == j[0] and j not in results: # results.append(j) # return find_match(components,j,results) # return results def build_chain(chain:list, components:list): last_componet = chain[len(chain)-1] next_component = None while len(components) > 0: next_component = components.pop() if ( next_component[0] == last_componet[1] or next_component[0] == last_componet[0] or next_component[1] == last_componet[1] or next_component[1] == last_componet[0] ): #print(next_component,components) chain.append(next_component) build_chain(chain,deepcopy(components)) return chain if part == 1: components = [] with open(input_f) as file: for line in file: l = get_re(r"^(\d+)\/(\d+)$",line.rstrip()) components.append(sorted([int(l.group(1)),int(l.group(2))])) pprint(components) original_comp = deepcopy(components) for c in components[:]: if c[0] == 0: chain = build_chain([c],components) print(chain) ######################################### # # # Part 2 # # # ######################################### if part == 2: exit()