Created
April 30, 2022 20:12
-
-
Save Mu-adventofcode/5612881243d61a94b71a2f45966b0cf1 to your computer and use it in GitHub Desktop.
Advent of Code 2021 day 02 part 1
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: | |
""" | |
The position of the vessel. | |
""" | |
def __init__(self, horpos=0, depth=0): | |
self.horpos = horpos | |
self.depth = depth | |
def step(self, string): | |
direction, amount = string.split() | |
if direction == "forward": | |
self.horpos += int(amount) | |
elif direction == "up": | |
self.depth -= int(amount) | |
elif direction == "down": | |
self.depth += 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