From 7cb89fe4164c63eaafeb7542030af187a5e32a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sat, 12 Dec 2020 07:06:43 +0100 Subject: day 12 --- 20/README | 2 ++ 20/py/d12.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 20/py/d12.py (limited to '20') diff --git a/20/README b/20/README index 65c94cf..a289864 100644 --- a/20/README +++ b/20/README @@ -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)) -- cgit v1.2.1