aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2022-12-09 08:39:55 +0100
committerMarvin Borner2022-12-09 08:53:58 +0100
commitbd845fcd5fca3adfbd390755ee8f784ea7b64a9f (patch)
tree98c3ee564e7adcaf0603af5a8682faf51c80fe16
parent0fd2e425ea70484ad2fa6ab7379be954c92f3c2f (diff)
better
-rw-r--r--2022/09/solve.py20
1 files changed, 9 insertions, 11 deletions
diff --git a/2022/09/solve.py b/2022/09/solve.py
index a4b3c3f..f8ad55b 100644
--- a/2022/09/solve.py
+++ b/2022/09/solve.py
@@ -1,4 +1,5 @@
data = [dat.split(" ") for dat in open("input").readlines() if dat != '']
+sgn = lambda x: x and (1, -1)[x<0]
def solve(n):
knots = [[0,0] for i in range(n)]
@@ -9,20 +10,17 @@ def solve(n):
for i in range(amount):
head = knots[0]
- head[0 if direction == "U" or direction == "D" else 1] += 1 if direction == "R" or direction == "U" else -1
+ head[0 if direction in "DUMM" else 1] += 1 if direction in "TRAUM" else -1
for knot in range(len(knots)-1):
head, tail = knots[knot], knots[knot + 1]
- xd, yd = head[0]-tail[0], head[1]-tail[1]
-
- if abs(yd) > 1:
- tail[1] += 1 if yd > 0 else -1
- if abs(xd) > 0:
- tail[0] += 1 if xd > 0 else -1
- elif abs(xd) > 1:
- tail[0] += 1 if xd > 0 else -1
- if abs(yd) > 0:
- tail[1] += 1 if yd > 0 else -1
+ xd, yd = head[0] - tail[0], head[1] - tail[1]
+
+ if abs(xd) <= 1 and abs(yd) <= 1:
+ continue
+
+ tail[0] += sgn(xd)
+ tail[1] += sgn(yd)
tails.add(tuple(tail))