From 72724bb3a507465b8f1df65eeb6a3bee62549705 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 2 Dec 2021 17:28:01 +0000 Subject: day 2 --- 2.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2.py (limited to '2.py') diff --git a/2.py b/2.py new file mode 100644 index 0000000..618cd86 --- /dev/null +++ b/2.py @@ -0,0 +1,28 @@ +Command = tuple[str, int] + +def part1(commands: list[Command]) -> int: + pos: int = 0 + depth: int = 0 + for command in commands: + match command: + case ('up', X): depth -= X + case ('down', X): depth += X + case ('forward', X): pos += X + return pos * depth + +def part2(commands: list[Command]) -> int: + pos: int = 0 + aim: int = 0 + depth: int = 0 + for command in commands: + match command: + case ('up', X): aim -= X + case ('down', X): aim += X + case ('forward', X): + pos += X + depth += aim * X + return pos * depth + +commands: list[Command] = [(c, int(q)) for c, q in (l.split() for l in open('2.in'))] +print(part1(commands)) +print(part2(commands)) -- cgit v1.2.3-54-g00ecf