Skip to content

Instantly share code, notes, and snippets.

@vadim2404
Last active December 5, 2023 12:12
Show Gist options
  • Save vadim2404/d50d223396c0551ccf739274457e62db to your computer and use it in GitHub Desktop.
Save vadim2404/d50d223396c0551ccf739274457e62db to your computer and use it in GitHub Desktop.
day5
#!/usr/bin/env python3
with open("input.txt") as f:
seeds = [int(x) for x in f.readline().strip().split(':')[1].split(' ') if x]
f.readline()
steps = []
f.readline()
for line in f:
d = {}
line = f.readline().strip()
while line:
destination, source, length = map(int, line.split(' '))
d[(source, source + length - 1)] = destination
line = f.readline().strip()
steps.append(d)
ans = []
for seed in seeds:
value = seed
for step in steps:
new_value = None
for (start, end), destination in step.items():
if start <= value <= end:
new_value = destination + value - start
break
value = new_value or value
ans.append(value)
print(min(ans))
#!/usr/bin/env python3
def traverse(range_from, range_to, steps, step) -> int:
if step == len(steps):
return range_from
for (start, end), destination in steps[step].items():
if start <= range_from <= end and start <= range_to <= end:
return traverse(destination + range_from - start, destination + range_to - start, steps, step + 1)
if start <= range_from <= end:
return min(
traverse(destination + range_from - start, destination + end - start, steps, step + 1),
traverse(end + 1, range_to, steps, step)
)
if start <= range_to <= end:
return min(
traverse(destination, destination + range_to - start, steps, step + 1),
traverse(range_from, start - 1, steps, step)
)
return traverse(range_from, range_to, steps, step + 1)
with open("input.txt") as f:
seeds = [int(x) for x in f.readline().strip().split(':')[1].split(' ') if x]
new_seeds = []
for i in range(0, len(seeds), 2):
new_seeds.append([seeds[i], seeds[i] + seeds[i + 1]])
seeds = new_seeds
f.readline()
steps = []
f.readline()
for line in f:
d = {}
line = f.readline().strip()
while line:
destination, source, length = map(int, line.split(' '))
d[(source, source + length - 1)] = destination
line = f.readline().strip()
steps.append(d)
print(min(traverse(range_from, range_to, steps, 0) for range_from, range_to in seeds))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment