From 82b8354edc5f06f4657dc79ffb11ce0d9d923087 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 2 Dec 2021 10:10:29 +0100 Subject: [PATCH] 2021: d02: ex1: add solution --- 2021/d02/ex1/ex1.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 2021/d02/ex1/ex1.py diff --git a/2021/d02/ex1/ex1.py b/2021/d02/ex1/ex1.py new file mode 100755 index 0000000..f891b43 --- /dev/null +++ b/2021/d02/ex1/ex1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +import sys +from typing import List + + +def solve(input: List[str]) -> int: + x, y = 0, 0 + + for instruction in input: + dir, length_ = instruction.split(" ") + length = int(length_) + if dir == "forward": + x += length + elif dir == "down": + y += length + elif dir == "up": + y -= 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()