Created
April 30, 2022 20:12
-
-
Save Mu-adventofcode/2515fabdf67f8be4f28cf90dbee41d76 to your computer and use it in GitHub Desktop.
Advent of Code 2021 day 02 part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Position: | |
def __init__(self, horpos=0, depth=0, aim=0): | |
self.horpos = horpos | |
self.depth = depth | |
self.aim = aim | |
def step(self, string): | |
direction, amount = string.split() | |
if direction == "forward": | |
self.horpos += int(amount) | |
self.depth += self.aim * int(amount) | |
elif direction == "up": | |
self.aim -= int(amount) | |
elif direction == "down": | |
self.aim += int(amount) | |
else: | |
raise ValueError("unknown direction") | |
def result(self): | |
return self.horpos * self.depth | |
if __name__ == "__main__": | |
p = Position(0, 0) | |
with open("input.txt") as txt: | |
course = txt.readlines() | |
for step in course: | |
p.step(step) | |
print(p.result()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment