60 lines
No EOL
1.6 KiB
Python
60 lines
No EOL
1.6 KiB
Python
#!/usr/bin/env python3
|
|
|
|
lines = []
|
|
with open('/home/wasp/personal/x/fun/advent-of-code/2023/inputs/day05.txt') as f:
|
|
lines = f.readlines()
|
|
|
|
# part 1
|
|
seeds = []
|
|
maps = []
|
|
parsing_map = False
|
|
current_map = {}
|
|
|
|
for line in lines:
|
|
if 'seeds:' in line:
|
|
seeds = list(map(lambda x: int(x.strip()), line.split(':')[1].strip().split()))
|
|
else:
|
|
if 'map' in line:
|
|
parsing_map = True
|
|
spl = line.split(' ')[0].split('-')
|
|
source = spl[0]
|
|
target = spl[2]
|
|
current_map['source'] = source
|
|
current_map['target'] = target
|
|
current_map['map'] = {}
|
|
elif parsing_map:
|
|
if line == '\n':
|
|
maps.append(current_map)
|
|
current_map = {}
|
|
parsing_map = False
|
|
else:
|
|
spl = line.split(' ')
|
|
dest = int(spl[0])
|
|
source = int(spl[1])
|
|
length = int(spl[2])
|
|
for i in range(length):
|
|
current_map['map'][source + i] = dest + i
|
|
|
|
|
|
maps.append(current_map)
|
|
|
|
destinations = []
|
|
for seed in seeds:
|
|
source_type = 'seed'
|
|
m = next((m for m in maps if m['source'] == source_type), None)
|
|
source = seed
|
|
destination = None
|
|
|
|
while (not m is None):
|
|
if source in m['map'].keys():
|
|
destination = m['map'][source]
|
|
else:
|
|
destination = source
|
|
|
|
source_type = m['target']
|
|
source = destination
|
|
m = next((m for m in maps if m['source'] == source_type), None)
|
|
|
|
destinations.append(destination)
|
|
|
|
print(min(destinations)) |