Created
December 2, 2021 09:52
-
-
Save raeq/563184a2642839c3b1464964600d64ac to your computer and use it in GitHub Desktop.
AOC 2021 Day2 Part2
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
def get_data(fn): | |
data: list = [] | |
with open(fn) as f: | |
for line in f: | |
direction, distance = line.rstrip().split() | |
data.append((direction, int(distance))) | |
return data | |
class Submarine: | |
x: int | |
y: int | |
z: int | |
_aim: int | |
def __init__(self, x: int = 0, y: int = 0, z: int = 0): | |
self._aim = 0 | |
self.x = x | |
self.y = y | |
self.z = z | |
def __repr__(self): | |
return f'{self.__class__.__name__}(x = {self.x}, y = {self.y}, z = {self.z})' | |
def forward(self, distance: int): | |
self.x += distance | |
self.z += self._aim * distance | |
def up(self, distance: int): | |
self._aim -= distance | |
def down(self, distance: int): | |
self._aim += distance | |
def move(self, function_name, distance): | |
return getattr(self, function_name)(distance) | |
if __name__ == "__main__": | |
data = get_data("day02.txt") | |
sub = Submarine() | |
for direction, distance in data: | |
sub.move(direction, distance) | |
print(f'Day 1 Part 2 answer: {sub.x * sub.z}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment