Skip to content

Instantly share code, notes, and snippets.

@rejoycesong
Created December 5, 2021 09:29
Show Gist options
  • Save rejoycesong/205e27b6b8c6b0f8b213acb7efc57e08 to your computer and use it in GitHub Desktop.
Save rejoycesong/205e27b6b8c6b0f8b213acb7efc57e08 to your computer and use it in GitHub Desktop.
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