Day 12 (LR dict)

This commit is contained in:
Sebastian Seedorf
2020-12-12 12:19:13 +01:00
parent 801ac37dde
commit f6fde17543
2 changed files with 6 additions and 4 deletions

View File

@@ -3,12 +3,13 @@ lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
pos = 0 pos = 0
direction = 1 direction = 1
targets = {"E": 1, "N": 1j, "W": -1, "S": -1j} targets = {"E": 1, "N": 1j, "W": -1, "S": -1j}
angles = {"L": 1, "R": -1}
for cmd, num in lines: for cmd, num in lines:
if cmd in targets: if cmd in targets:
pos += targets[cmd] * num pos += targets[cmd] * num
elif cmd in "LR": elif cmd in angles:
times = (-1 if cmd == "R" else 1) * num / 90 times = angles[cmd] * num / 90
direction = direction * 1j ** times direction = direction * 1j ** times
else: else:
pos += direction * num pos += direction * num

View File

@@ -3,12 +3,13 @@ lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
pos = 0 pos = 0
direction = 10 + 1j direction = 10 + 1j
targets = {"E": 1, "N": 1j, "W": -1, "S": -1j} targets = {"E": 1, "N": 1j, "W": -1, "S": -1j}
angles = {"L": 1, "R": -1}
for cmd, num in lines: for cmd, num in lines:
if cmd in targets: if cmd in targets:
direction += targets[cmd] * num direction += targets[cmd] * num
elif cmd in "LR": elif cmd in angles:
times = (-1 if cmd == "R" else 1) * num / 90 times = angles[cmd] * num / 90
direction = direction * 1j ** times direction = direction * 1j ** times
else: else:
pos += direction * num pos += direction * num