advent_of_code/2023/5/5.py

41 lines
1.1 KiB
Python
Raw Permalink Normal View History

2023-12-05 18:32:18 +01:00
#!/usr/bin/env python
# 2023 - Advent Of Code 5
# file = 'input_example.txt'
file = 'input.txt'
# pylint: disable=consider-using-with
input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
seeds = [int(x) for x in input_lines[0].split(':')[1].split(' ') if x != '']
print(f'seeds: {seeds}')
maps = []
count = 0
cur_list = []
for line in input_lines[2:]:
if line == '':
count += 1
maps.append(cur_list)
elif line.endswith(':'):
cur_list = []
else:
dest, source, length = line.split(' ')
# print(f'parsed dest: {dest} source: {source} len: {length}')
cur_list.append((int(dest), int(source), int(length)))
maps.append(cur_list)
seeds_location = []
for s in seeds:
location = s
for m in maps:
for r in m:
old = location
if r[1] <= location < (r[1] + r[2]):
location = r[0] + (location - r[1])
break
# print(f'{old} -> {location}')
print(f'{s} final location is {location}')
seeds_location.append(location)
print(min(seeds_location))