From f7bb61b0e0e6abaf44b173855502047d598785ef Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Thu, 2 Dec 2021 06:56:48 +0100 Subject: [PATCH] Solve 2021 Day 2 in python --- 2021/Day2/Python/day2.py | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2021/Day2/Python/day2.py diff --git a/2021/Day2/Python/day2.py b/2021/Day2/Python/day2.py new file mode 100644 index 0000000..314adc5 --- /dev/null +++ b/2021/Day2/Python/day2.py @@ -0,0 +1,41 @@ +# Part 1 +with open("input.txt") as f: + lines = map(lambda l: l.strip(), f.readlines()) + depth = 0 + horizontal = 0 + for line in lines: + command = line.split(" ")[0] + amount = int(line.split(" ")[1]) + + if command == "forward": + horizontal += amount + elif command == "down": + depth += amount + elif command == "up": + depth -= amount + + print("Part 1:") + print(f"Horizontal Position: {horizontal}, Depth: {depth}") + print(f"Product: {horizontal * depth}") + +# Part 2 +with open("input.txt") as f: + lines = map(lambda l: l.strip(), f.readlines()) + aim = 0 + depth = 0 + horizontal = 0 + for line in lines: + command = line.split(" ")[0] + amount = int(line.split(" ")[1]) + + if command == "forward": + horizontal += amount + depth += amount * aim + elif command == "down": + aim += amount + elif command == "up": + aim -= amount + + print("Part 2:") + print(f"Horizontal Position: {horizontal}, Depth: {depth}") + print(f"Product: {horizontal * depth}") \ No newline at end of file