Skip to content

Instantly share code, notes, and snippets.

@tobidope
Created December 2, 2021 06:22
Show Gist options
  • Select an option

  • Save tobidope/93d67ab1a11fd340726d27aace99b631 to your computer and use it in GitHub Desktop.

Select an option

Save tobidope/93d67ab1a11fd340726d27aace99b631 to your computer and use it in GitHub Desktop.
AOC2021 day2
data = """
"""
def solve1(data):
hpos = 0
depth = 0
for instruction in data.splitlines():
direction, number = instruction.split()
number = int(number)
if direction == "forward":
hpos += number
elif direction == "up":
depth -= number
elif direction == "down":
depth += number
return hpos, depth
def solve2(data):
hpos = 0
depth = 0
aim = 0
for instruction in data.splitlines():
direction, number = instruction.split()
number = int(number)
if direction == "forward":
hpos += number
depth += aim * number
elif direction == "up":
aim -= number
elif direction == "down":
aim += number
return hpos, depth
hpos, depth = solve1(data)
print(f"Day 1.1 - {hpos}, {depth} = {hpos*depth}")
hpos, depth = solve2(data)
print(f"Day 1.2 - {hpos}, {depth} = {hpos*depth}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment