Skip to content

Instantly share code, notes, and snippets.

@thrasibule
Created December 6, 2023 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thrasibule/0f22bafc2a8454c7de743ffd4a541e17 to your computer and use it in GitHub Desktop.
Save thrasibule/0f22bafc2a8454c7de743ffd4a541e17 to your computer and use it in GitHub Desktop.
import sys
from itertools import islice
def parse_block(fh):
m = []
while True:
try:
l = next(fh).rstrip()
except StopIteration:
break
if l:
dest, source, ran = map(int, l.split())
m.append((source, source + ran, dest -source))
else:
break
return sorted(m)
with open(sys.argv[1]) as fh:
line = next(fh).rstrip()
seeds = map(int, line.split(": ")[1].split())
next(fh)
blocks = []
while True:
try:
next(fh)
except StopIteration:
break
blocks.append(parse_block(fh))
def batched(iterable, n):
# batched('ABCDEFG', 3) --> ABC DEF G
if n < 1:
raise ValueError('n must be at least one')
it = iter(iterable)
while batch := tuple(islice(it, n)):
yield batch
breakpoint()
locs = []
for start, ran in batched(seeds, 2):
for s in range(start, start + ran):
for b in blocks:
for start, end, diff in b:
if s < start:
break
if s >= start and s < end:
s += diff
break
locs.append(s)
print(min(locs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment