summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2022-12-12 10:44:42 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2022-12-12 10:44:42 +0000
commit7bfeff1cb9b142c1d0998ef3c63a780d231a7329 (patch)
tree4a87625daa9701d233f6ae7b8aa03645089e8d6c
parent7a71e86dacb03a4ed7dde223b4d720f738c60513 (diff)
downloadaoc2022-7bfeff1cb9b142c1d0998ef3c63a780d231a7329.tar.gz
aoc2022-7bfeff1cb9b142c1d0998ef3c63a780d231a7329.tar.xz
aoc2022-7bfeff1cb9b142c1d0998ef3c63a780d231a7329.zip
9
-rw-r--r--9.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/9.py b/9.py
new file mode 100644
index 0000000..1e9270b
--- /dev/null
+++ b/9.py
@@ -0,0 +1,52 @@
+from dataclasses import dataclass
+
+@dataclass(frozen=True)
+class Point:
+ x: int
+ y: int
+ def __add__(self, other):
+ return Point(self.x + other.x, self.y + other.y)
+ # def __mul__(self, other):
+ # return Point(self.x * other, self.y + other)
+ def __sub__(self, other):
+ return Point(self.x - other.x, self.y - other.y)
+ def norm(self):
+ return Point(min(max(self.x, -1), 1), min(max(self.y, -1), 1))
+
+def pabs(p):
+ return max(abs(p.x), abs(p.y))
+delta = {
+ 'U': Point(0, 1), 'D': Point(0, -1), 'L': Point(-1, 0), 'R': Point(1, 0)
+}
+with open('9.in') as f:
+ inp = [tuple(line.rstrip().split()) for line in f]
+inp = [(delta[d], int(q)) for d, q in inp]
+def solve(tails):
+ knots = [Point(0, 0) for _ in range(tails + 1)]
+ visited = set()
+ visited.add(knots[-1])
+ for move, count in inp:
+ for _ in range(count):
+ knots[0] += move
+ for i in range(1, len(knots)):
+ diff = knots[i-1] - knots[i]
+ if pabs(diff) > 1:
+ knots[i] += diff.norm()
+ visited.add(knots[-1])
+ # for y in range(20, -20, -1):
+ # line = []
+ # for x in range(-20, 20):
+ # p = Point(x, y)
+ # if p in visited:
+ # line.append('#')
+ # else:
+ # line.append(' ')
+ # try:
+ # line[-1] = str(knots.index(p))
+ # except ValueError:
+ # pass
+ # print(''.join(line))
+ return len(visited)
+
+print(solve(1))
+print(solve(9))