#!/bin/python3 import sys,time,re from pprint import pprint sys.path.insert(0, '../../') from fred import list2int,get_re,nprint,lprint,loadFile,TSP start_time = time.time() input_f = 'input' ######################################### # # # Part 1 # # # ######################################### def parseInput(): inst = [] graph = {} with open(input_f) as file: for line in file: inst = line.rstrip().split(' happiness units by sitting next to ') t = inst[0].split(' would ') inst = [t[0]] + [inst[1].replace('.','')] + [int(t[1].replace('lose ','-').replace('gain ',''))] if inst[0] not in graph: graph[inst[0]] = [] #if inst[1] not in graph: # graph[inst[1]] = [] graph[inst[0]].append((inst[1],inst[2])) #graph[inst[1]].append((inst[0],inst[2])) return graph def addBothWays(graph): n_graph = {} for _,a in enumerate(graph): for b in graph[a]: for c in graph[b[0]]: if c[0] == a: if a not in n_graph: n_graph[a] = [] n_graph[a].append((b[0],b[1]+c[1])) return n_graph def part1(): graph = addBothWays(parseInput()) pprint(graph) longest = 0 people = set(graph.keys()) for neighbors in graph.values(): for neighbor, _ in neighbors: people.add(neighbor) for c in people: route,distance = TSP(graph, c,'longest',True) if longest < distance: longest = distance return longest start_time = time.time() print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') ######################################### # # # Part 2 # # # ######################################### def part2(): # Just add 'Myself' to the input file with gain of 0 towards everyone return start_time = time.time() print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')