From 2c31559722eed2a25d15b909fabae63a7e3cf3d7 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Dec 2020 10:06:19 +0100 Subject: [PATCH] 2020: d12: ex2: add solution --- 2020/d12/ex2/ex2.py | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 2020/d12/ex2/ex2.py diff --git a/2020/d12/ex2/ex2.py b/2020/d12/ex2/ex2.py new file mode 100755 index 0000000..90d5f87 --- /dev/null +++ b/2020/d12/ex2/ex2.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +import sys +from dataclasses import dataclass +from typing import List, Tuple + +Instruction = Tuple[str, int] +Position = Tuple[int, int] + + +@dataclass +class Ship: + pos: Position + rel_waypoint: Position + + +ORIENTATIONS = ["N", "W", "S", "E"] +MOVES = { + "N": (1, 0), + "W": (0, -1), + "S": (-1, 0), + "E": (0, 1), +} + + +def move_ship(pos: Ship, instr: Instruction) -> Ship: + if instr[0] == "L": + assert instr[1] % 90 == 0 + turns = instr[1] // 90 + x, y = pos.rel_waypoint + for __ in range(turns): + x, y = y, -x + return Ship(pos.pos, (x, y)) + elif instr[0] == "R": + assert instr[1] % 90 == 0 + turns = instr[1] // 90 + x, y = pos.rel_waypoint + for __ in range(turns): + x, y = -y, x + return Ship(pos.pos, (x, y)) + elif instr[0] == "F": + dx, dy = pos.rel_waypoint + x, y = pos.pos + x += dx * instr[1] + y += dy * instr[1] + return Ship((x, y), pos.rel_waypoint) + else: + dx, dy = MOVES[instr[0]] + x, y = pos.rel_waypoint + x += dx * instr[1] + y += dy * instr[1] + return Ship(pos.pos, (x, y)) + + +def solve(raw: List[str]) -> int: + instructions = [(i[0], int(i[1:])) for i in raw] + ship = Ship((0, 0), (1, 10)) + for i in instructions: + ship = move_ship(ship, i) + return sum(abs(c) for c in ship.pos) + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()