AdventOfCode/2017/24/solution.py

72 lines
2.1 KiB
Python

#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint
input_f = 'input'
part = 2
#########################################
# #
# Part 1 #
# #
#########################################
def parse_input(input_f):
list_of_components = []
with open(input_f) as file:
for line in file:
l = get_re(r"^(\d+)\/(\d+)$",line.rstrip())
list_of_components.append([int(l.group(1)),int(l.group(2))])
return list_of_components
def make_bridge(components, last):
score = 0
longest_bridge = []
for cdx, component in enumerate(components):
if component[0] == last or component[1] == last:
if component[0] == last:
tmp = component[1]
else:
tmp = component[0]
x = component + make_bridge(components[:cdx]+components[cdx+1:],tmp)
if sum(x) > score:
score = sum(x)
longest_bridge = x
return longest_bridge
if part == 1:
components = parse_input(input_f)
bridge = make_bridge(components,0)
print(bridge)
#########################################
# #
# Part 2 #
# #
#########################################
def make_bridge_longest(components, last):
score = 0
longest_bridge = []
for cdx, component in enumerate(components):
if component[0] == last or component[1] == last:
if component[0] == last:
tmp = component[1]
else:
tmp = component[0]
x = component + make_bridge_longest(components[:cdx]+components[cdx+1:],tmp)
if len(x) >= len(longest_bridge):
longest_bridge = x
return longest_bridge
if part == 2:
components = parse_input(input_f)
bridge = make_bridge_longest(components,0)
print(sum(bridge))