### Solve 2021 Day 2 in python

master Garrit Franke 10 months ago
parent
commit
f7bb61b0e0
Signed by: garrit
GPG Key ID: 65586C4DDA55EA2C
1 changed files with 41 additions and 0 deletions
1. 41
2021/Day2/Python/day2.py

#### 41 2021/Day2/Python/day2.py Unescape Escape View File

 `@ -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(" ")` ` amount = int(line.split(" "))` ``` ``` ` 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(" ")` ` amount = int(line.split(" "))` ``` ``` ` 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}")`