Day 12 (rename drctn to direction)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
|
lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
|
||||||
|
|
||||||
pos = 0
|
pos = 0
|
||||||
drctn = 1
|
direction = 1
|
||||||
targets = {"E": 1, "N": 1j, "W": -1, "S": -1j}
|
targets = {"E": 1, "N": 1j, "W": -1, "S": -1j}
|
||||||
|
|
||||||
for cmd, num in lines:
|
for cmd, num in lines:
|
||||||
@@ -9,8 +9,8 @@ for cmd, num in lines:
|
|||||||
pos += targets[cmd] * num
|
pos += targets[cmd] * num
|
||||||
elif cmd in "LR":
|
elif cmd in "LR":
|
||||||
times = (-1 if cmd == "R" else 1) * num / 90
|
times = (-1 if cmd == "R" else 1) * num / 90
|
||||||
drctn = drctn * 1j ** times
|
direction = direction * 1j ** times
|
||||||
else:
|
else:
|
||||||
pos += drctn * num
|
pos += direction * num
|
||||||
|
|
||||||
print(int(abs(pos.real) + abs(pos.imag)))
|
print(int(abs(pos.real) + abs(pos.imag)))
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
|
lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
|
||||||
|
|
||||||
pos = 0
|
pos = 0
|
||||||
drctn = 10+1j
|
direction = 10 + 1j
|
||||||
targets = {"E": 1, "N": 1j, "W": -1, "S": -1j}
|
targets = {"E": 1, "N": 1j, "W": -1, "S": -1j}
|
||||||
|
|
||||||
for cmd, num in lines:
|
for cmd, num in lines:
|
||||||
if cmd in targets:
|
if cmd in targets:
|
||||||
drctn += targets[cmd] * num
|
direction += targets[cmd] * num
|
||||||
elif cmd in "LR":
|
elif cmd in "LR":
|
||||||
times = (-1 if cmd == "R" else 1) * num / 90
|
times = (-1 if cmd == "R" else 1) * num / 90
|
||||||
drctn = drctn * 1j ** times
|
direction = direction * 1j ** times
|
||||||
else:
|
else:
|
||||||
pos += drctn * num
|
pos += direction * num
|
||||||
|
|
||||||
print(int(abs(pos.real) + abs(pos.imag)))
|
print(int(abs(pos.real) + abs(pos.imag)))
|
||||||
|
|||||||
Reference in New Issue
Block a user