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]
- for section in sections[1:]:
- output_intervals = []
- for line in section.splitlines()[1:]:
- dst, src, num = map(int, line.split())
- new_intervals = []
- for start, stop in intervals:
- i0, i1 = max(src, start), min(src + num, stop)
- if i0 < i1:
- output_intervals.append((dst + (i0 - src), dst + (i1 - src)))
- if i0 > start:
- new_intervals.append((start, min(i0, stop)))
- if i1 < stop:
- new_intervals.append((max(i1, start), stop))
- intervals = new_intervals
- intervals.extend(output_intervals)
- return min(start for start, _ in intervals)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement