generated from public/aoc_template
day05 - cleaned
This commit is contained in:
parent
c1673d910c
commit
db7cf58798
38
day05.py
38
day05.py
@ -1,4 +1,5 @@
|
||||
from tools.aoc import AOCDay
|
||||
from tools.types import Range
|
||||
from typing import Any
|
||||
|
||||
|
||||
@ -14,40 +15,27 @@ class Day(AOCDay):
|
||||
]
|
||||
]
|
||||
|
||||
def parse_input(self) -> (list[tuple[int, int]], list[int]):
|
||||
def parse_input(self) -> (list[Range], list[int]):
|
||||
ranges, ids = self.getMultiLineInputAsArray()
|
||||
checked = []
|
||||
for _range in [Range(*map(int, x.split('-'))) for x in ranges]:
|
||||
for c in checked.copy():
|
||||
if _range.overlaps(c):
|
||||
checked.remove(c)
|
||||
_range = _range.merge(c)
|
||||
|
||||
return [tuple(map(int, x.split("-"))) for x in ranges], list(map(int, ids))
|
||||
checked.append(_range)
|
||||
|
||||
return checked, list(map(int, ids))
|
||||
|
||||
|
||||
def part1(self) -> Any:
|
||||
ranges, ids = self.parse_input()
|
||||
|
||||
ans = 0
|
||||
for _id in ids:
|
||||
for _range in ranges:
|
||||
if _range[0] <= _id <= _range[1]:
|
||||
ans += 1
|
||||
break
|
||||
return ans
|
||||
return sum(x in y for y in ranges for x in ids)
|
||||
|
||||
def part2(self) -> Any:
|
||||
ranges, _ = self.parse_input()
|
||||
|
||||
checked = []
|
||||
for _range in ranges:
|
||||
for c in checked.copy():
|
||||
if (c[0] <= _range[0] <= c[1] + 1) or (c[1] >= _range[1] >= c[0]) or (_range[0] <= c[0] and _range[1] >= c[1]):
|
||||
checked.remove(c)
|
||||
_range = (min(_range[0], c[0]), max(_range[1], c[1]))
|
||||
|
||||
checked.append(_range)
|
||||
|
||||
ans = 0
|
||||
for _range in checked:
|
||||
ans += _range[1] - _range[0] + 1
|
||||
|
||||
return ans
|
||||
return sum(len(x) for x in ranges)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Loading…
Reference in New Issue
Block a user