Day 12 (replace array with dict)
This commit is contained in:
@@ -2,12 +2,11 @@ lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
|
|||||||
|
|
||||||
pos = 0
|
pos = 0
|
||||||
drctn = 1
|
drctn = 1
|
||||||
targets = [1, 1j, -1, -1j]
|
targets = {"E": 1, "N": 1j, "W": -1, "S": -1j}
|
||||||
target_str = "ENWS"
|
|
||||||
|
|
||||||
for cmd, num in lines:
|
for cmd, num in lines:
|
||||||
if cmd in target_str:
|
if cmd in targets:
|
||||||
pos += targets[target_str.index(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
|
drctn = drctn * 1j ** times
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
|
|||||||
|
|
||||||
pos = 0
|
pos = 0
|
||||||
drctn = 10+1j
|
drctn = 10+1j
|
||||||
targets = [1, 1j, -1, -1j]
|
targets = {"E": 1, "N": 1j, "W": -1, "S": -1j}
|
||||||
target_str = "ENWS"
|
|
||||||
|
|
||||||
for cmd, num in lines:
|
for cmd, num in lines:
|
||||||
if cmd in target_str:
|
if cmd in targets:
|
||||||
drctn += targets[target_str.index(cmd)] * num
|
drctn += 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
|
drctn = drctn * 1j ** times
|
||||||
|
|||||||
Reference in New Issue
Block a user