From af4e4f012e4c962108da0d7a556edd4084cc6029 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Sat, 12 Dec 2020 12:12:01 +0100 Subject: [PATCH] Day 12 (replace array with dict) --- day12/part1.py | 7 +++---- day12/part2.py | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/day12/part1.py b/day12/part1.py index fb0ccbb..e534690 100644 --- a/day12/part1.py +++ b/day12/part1.py @@ -2,12 +2,11 @@ lines = ((x[0], int(x[1:].strip())) for x in open("input.txt")) pos = 0 drctn = 1 -targets = [1, 1j, -1, -1j] -target_str = "ENWS" +targets = {"E": 1, "N": 1j, "W": -1, "S": -1j} for cmd, num in lines: - if cmd in target_str: - pos += targets[target_str.index(cmd)] * num + if cmd in targets: + pos += targets[cmd] * num elif cmd in "LR": times = (-1 if cmd == "R" else 1) * num / 90 drctn = drctn * 1j ** times diff --git a/day12/part2.py b/day12/part2.py index 39ae5f8..14eff52 100644 --- a/day12/part2.py +++ b/day12/part2.py @@ -2,12 +2,11 @@ lines = ((x[0], int(x[1:].strip())) for x in open("input.txt")) pos = 0 drctn = 10+1j -targets = [1, 1j, -1, -1j] -target_str = "ENWS" +targets = {"E": 1, "N": 1j, "W": -1, "S": -1j} for cmd, num in lines: - if cmd in target_str: - drctn += targets[target_str.index(cmd)] * num + if cmd in targets: + drctn += targets[cmd] * num elif cmd in "LR": times = (-1 if cmd == "R" else 1) * num / 90 drctn = drctn * 1j ** times