diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-12-12 07:06:43 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-12-12 07:06:43 +0100 |
| commit | 7cb89fe4164c63eaafeb7542030af187a5e32a68 (patch) | |
| tree | 8cb1ff598c41df2808e0505a30c76b4ea433edfb /20 | |
| parent | 940094e90c65ac97bf376c51521e687ae23e8568 (diff) | |
| download | aoc-7cb89fe4164c63eaafeb7542030af187a5e32a68.tar.gz | |
day 12
Diffstat (limited to '20')
| -rw-r--r-- | 20/README | 2 | ||||
| -rw-r--r-- | 20/py/d12.py | 58 |
2 files changed, 60 insertions, 0 deletions
@@ -13,6 +13,7 @@ Day Time Ans Time Ans 9 2.741 26796446 2.875 3353494 10 0.027 2046 0.161 1157018619904 11 432.343 2338 580.579 2134 + 12 0.257 1645 0.279 35292 ------- ------- tot TBD TBD @@ -21,6 +22,7 @@ Stats: -------Part 1-------- -------Part 2-------- Day Time Rank Score Time Rank Score + 12 00:09:08 645 0 00:16:03 373 0 11 00:55:39 4799 0 01:20:27 4040 0 10 00:08:17 1496 0 00:51:01 2766 0 9 00:25:55 6220 0 00:31:44 4392 0 diff --git a/20/py/d12.py b/20/py/d12.py new file mode 100644 index 0000000..4057c7b --- /dev/null +++ b/20/py/d12.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +import aoc20 +import sys + + +def pt1(_in): + x, y = 0, 0 + dirs = ((1, 0), (0, -1), (-1, 0), (0, 1)) + dir = 0 + for inst in _in: + action, amount = inst[0], int(inst[1:]) + if action == "N": + y -= amount + elif action == "W": + x -= amount + elif action == "S": + y += amount + elif action == "E": + x += amount + elif action == "F": + x += dirs[dir][0] * amount + y += dirs[dir][1] * amount + elif action == "L": + dir = (dir + amount // 90) % 4 + elif action == "R": + dir = (dir - amount // 90) % 4 + return abs(x) + abs(y) + + +def pt2(_in): + ship_x, ship_y = 0, 0 + wp_x, wp_y = 10, -1 + for inst in _in: + action, amount = inst[0], int(inst[1:]) + if action == "N": + wp_y -= amount + elif action == "W": + wp_x -= amount + elif action == "S": + wp_y += amount + elif action == "E": + wp_x += amount + elif action == "F": + ship_x += wp_x * amount + ship_y += wp_y * amount + elif action == "L": + for _ in range((amount//90) % 4): + wp_x, wp_y = wp_y, -wp_x + elif action == "R": + for _ in range((amount//90) % 4): + wp_x, wp_y = -wp_y, wp_x + return abs(ship_x) + abs(ship_y) + + +if __name__ == "__main__": + input = aoc20.read_input(sys.argv[1:], 12) + print(pt1(input)) + print(pt2(input)) |
