advent-of-code/2021/python/day02.py

42 lines
754 B
Python

import shared
class Position:
def __init__(self):
self.horiz = 0
self.depth = 0
self.aim = 0
def parse_line(self, line: str):
direction, x = line.split(" ")
x = int(x)
if direction == "up":
self.aim -= x
elif direction == "down":
self.aim += x
elif direction == "forward":
self.horiz += x
self.depth += self.aim * x
def whereami(self):
return self.horiz * self.depth
def run(inp):
p = Position()
for i in inp:
p.parse_line(i)
return p.whereami()
def main():
with open(shared.get_fname(2), "r") as f:
inp = f.readlines()
print(run(inp))
if __name__ == "__main__":
main()