From 57a2d60dc08aa126f3fa0a45a9199dea79c06118 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 2 Dec 2021 10:10:45 +0100 Subject: [PATCH] 2020: d02: ex2: add solution --- 2021/d02/ex2/ex2.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 2021/d02/ex2/ex2.py diff --git a/2021/d02/ex2/ex2.py b/2021/d02/ex2/ex2.py new file mode 100755 index 0000000..a5a5f3d --- /dev/null +++ b/2021/d02/ex2/ex2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +import itertools +import sys +from typing import List + + +def solve(input: List[str]) -> int: + x, y, aim = 0, 0, 0 + + for instruction in input: + dir, length = instruction.split(" ") + length = int(length) + if dir == "forward": + x += length + y += length * aim + elif dir == "down": + aim += length + elif dir == "up": + aim -= length + else: + assert False + + return x * y + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()