Created
December 5, 2021 09:29
-
-
Save rejoycesong/205e27b6b8c6b0f8b213acb7efc57e08 to your computer and use it in GitHub Desktop.
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
input = open('input/day2.txt', 'r').readlines() | |
directions = [line for line in input] | |
# O(n) | |
def partOne(directions=directions): | |
horizontal = 0 | |
depth = 0 | |
for step in directions: | |
direction, val = step.split(" ", 1) | |
if direction == 'forward': | |
horizontal += int(val) | |
elif direction == 'down': | |
depth += int(val) | |
elif direction == 'up': | |
depth -= int(val) | |
return horizontal*depth | |
# # O(n) | |
def partTwo(directions=directions): | |
horizontal = 0 | |
depth = 0 | |
aim = 0 | |
for step in directions: | |
direction, val = step.split(" ", 1) | |
if direction == 'forward': | |
horizontal += int(val) | |
depth += aim*int(val) | |
elif direction == 'down': | |
aim += int(val) | |
elif direction == 'up': | |
aim -= int(val) | |
return horizontal*depth | |
print("Part One:", partOne()) | |
print("Part Two:", partTwo()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment