aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2021/17/.gitignore1
-rw-r--r--2021/17/Makefile8
-rw-r--r--2021/17/input1
-rw-r--r--2021/17/puzzles.py57
4 files changed, 67 insertions, 0 deletions
diff --git a/2021/17/.gitignore b/2021/17/.gitignore
new file mode 100644
index 0000000..ffc46fe
--- /dev/null
+++ b/2021/17/.gitignore
@@ -0,0 +1 @@
+puzzle-[12].py
diff --git a/2021/17/Makefile b/2021/17/Makefile
new file mode 100644
index 0000000..247194a
--- /dev/null
+++ b/2021/17/Makefile
@@ -0,0 +1,8 @@
+all:
+ sed '/START PART 2/,/END PART 2/d' puzzles.py >puzzle-1.py
+ sed '/START PART 1/,/END PART 1/d' puzzles.py >puzzle-2.py
+ chmod +x puzzle-[12].py
+
+.PHONY: clean
+clean:
+ rm -f puzzle-[12].py
diff --git a/2021/17/input b/2021/17/input
new file mode 100644
index 0000000..52bdf2b
--- /dev/null
+++ b/2021/17/input
@@ -0,0 +1 @@
+target area: x=175..227, y=-134..-79
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()