AdventOfCode/2017/24/solution.py

72 lines
2.1 KiB
Python
Raw Normal View History

2024-11-29 22:12:47 +01:00
#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
2024-12-01 23:24:40 +01:00
from fred import list2int,get_re,nprint,lprint
2024-11-29 22:12:47 +01:00
2024-12-01 23:24:40 +01:00
input_f = 'input'
2024-11-29 22:12:47 +01:00
2024-12-01 23:24:40 +01:00
part = 2
2024-11-29 22:12:47 +01:00
#########################################
# #
# Part 1 #
# #
#########################################
2024-12-01 23:24:40 +01:00
def parse_input(input_f):
list_of_components = []
2024-11-29 22:12:47 +01:00
with open(input_f) as file:
for line in file:
2024-12-01 23:24:40 +01:00
l = get_re(r"^(\d+)\/(\d+)$",line.rstrip())
list_of_components.append([int(l.group(1)),int(l.group(2))])
return list_of_components
2024-11-30 23:15:29 +01:00
2024-12-01 23:24:40 +01:00
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
2024-11-29 22:12:47 +01:00
2024-12-01 23:24:40 +01:00
if part == 1:
components = parse_input(input_f)
2024-11-29 22:12:47 +01:00
2024-12-01 23:24:40 +01:00
bridge = make_bridge(components,0)
print(bridge)
2024-11-29 22:12:47 +01:00
#########################################
# #
# Part 2 #
# #
#########################################
2024-12-01 23:24:40 +01:00
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]
2024-12-02 07:58:17 +01:00
x = component + make_bridge_longest(components[:cdx]+components[cdx+1:],tmp)
if len(x) >= len(longest_bridge):
2024-12-01 23:24:40 +01:00
longest_bridge = x
return longest_bridge
2024-11-29 22:12:47 +01:00
if part == 2:
2024-12-01 23:24:40 +01:00
components = parse_input(input_f)
bridge = make_bridge_longest(components,0)
2024-12-02 07:58:17 +01:00
print(sum(bridge))