diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-17 07:01:15 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-17 07:01:15 +0100 |
commit | 8e265a66fed0f2e79f0856e1ad59c64f3f210ab3 (patch) | |
tree | 4d4082b62f0ae3a0b62cb4e8a5cde9f8e5057aad /2021/17/puzzles.py | |
parent | c21b29f12f4abeb90fed3b99f8ed3be16f8b79e7 (diff) |
Add day 17 solutions
Diffstat (limited to '2021/17/puzzles.py')
-rw-r--r-- | 2021/17/puzzles.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/2021/17/puzzles.py b/2021/17/puzzles.py new file mode 100644 index 0000000..30fb347 --- /dev/null +++ b/2021/17/puzzles.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +import re +from itertools import product +from math import inf +from typing import NamedTuple, Optional + + +class Range(NamedTuple): + minx: int + maxx: int + miny: int + maxy: int + + +def fire(r: Range, dy: int, dx: int) -> Optional[int]: + x, y = 0, 0 + h = -inf + while x <= r.maxx and y >= r.miny: + x += dx + y += dy + + if dx > 0: + dx -= 1 + elif dx < 0: + dx += 1 + dy -= 1 + h = max(h, y) + + if r.minx <= x <= r.maxx and r.miny <= y <= r.maxy: + return h + return None + + +def main() -> None: + with open("input", "r", encoding="utf-8") as f: + m = re.split(r"target area: x=(\d+)..(\d+), y=(-?\d+)..(-?\d+)", f.read()) + r = Range(*tuple(map(int, m[1:-1]))) + + xr = max(abs(r.minx), abs(r.maxx)) + yr = max(abs(r.miny), abs(r.maxy)) + + heights = tuple( + filter( + lambda h: h != None, + (fire(r, x, y) for x, y in product(range(-yr, yr + 1), range(-xr, xr + 1))), + ) + ) + # START PART 1 + print(max(heights)) + # END PART 1 START PART 2 + print(len(heights)) + # END PART 2 + + +if __name__ == "__main__": + main() |