Day 12 (switched to dict.get())
This commit is contained in:
@@ -4,14 +4,12 @@ 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}
|
angles = {"L": 1, "R": -1}
|
||||||
|
forward = {"F": 1}
|
||||||
|
|
||||||
for cmd, num in lines:
|
for cmd, num in lines:
|
||||||
if cmd in targets:
|
pos += targets.get(cmd, 0) * num
|
||||||
pos += targets[cmd] * num
|
times = angles.get(cmd, 0) * num / 90
|
||||||
elif cmd in angles:
|
|
||||||
times = angles[cmd] * num / 90
|
|
||||||
direction = direction * 1j ** times
|
direction = direction * 1j ** times
|
||||||
else:
|
pos += forward.get(cmd, 0) * direction * num
|
||||||
pos += direction * num
|
|
||||||
|
|
||||||
print(int(abs(pos.real) + abs(pos.imag)))
|
print(int(abs(pos.real) + abs(pos.imag)))
|
||||||
|
|||||||
@@ -4,14 +4,12 @@ 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}
|
angles = {"L": 1, "R": -1}
|
||||||
|
forward = {"F": 1}
|
||||||
|
|
||||||
for cmd, num in lines:
|
for cmd, num in lines:
|
||||||
if cmd in targets:
|
direction += targets.get(cmd, 0) * num
|
||||||
direction += targets[cmd] * num
|
times = angles.get(cmd, 0) * num / 90
|
||||||
elif cmd in angles:
|
|
||||||
times = angles[cmd] * num / 90
|
|
||||||
direction = direction * 1j ** times
|
direction = direction * 1j ** times
|
||||||
else:
|
pos += forward.get(cmd, 0) * direction * 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