Browse Source

Solve 2021 Day 2 in python

master
Garrit Franke 10 months ago
parent
commit
f7bb61b0e0
Signed by: garrit
GPG Key ID: 65586C4DDA55EA2C
  1. 41
      2021/Day2/Python/day2.py

41
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}")
Loading…
Cancel
Save