Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day5(s, *, part2=False):
- sections = s.split('\n\n')
- (line,) = sections[0].splitlines()
- values = map(int, line.split(': ')[1].split())
- if part2:
- intervals = [(start, start + n) for start, n in more_itertools.batched(values, 2)]
- else:
- intervals = [(start, start + 1) for start in values]
- def remap(start, stop, lines):
- if not lines:
- yield start, stop
- else:
- dst, src, num = map(int, lines[0].split())
- i0, i1 = max(src, start), min(src + num, stop)
- if i0 < i1:
- yield dst + (i0 - src), dst + (i1 - src)
- if i0 > start:
- yield from remap(start, min(i0, stop), lines[1:])
- if i1 < stop:
- yield from remap(max(i1, start), stop, lines[1:])
- for section in sections[1:]:
- lines = section.splitlines()[1:]
- chain = itertools.chain.from_iterable
- intervals = list(chain(remap(*interval, lines) for interval in intervals))
- return min(start for start, _ in intervals)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement