Advent of Code - 2021
This is a solution to Day 2 of Advent of Code 2021.
Day 2 - Dive!
Now, you need to figure out how to pilot this thing.
It seems like the submarine can take a series of commands like forward 1, down 2, or up 3:
forward X
increases the horizontal position by X units.
down X
increases the depth by X units.
up X
decreases the depth by X units.Note that since you're on a submarine, down and up affect your depth, and so they have the opposite result of what you might expect.
The submarine seems to already have a planned course (your puzzle input). You should probably figure out where it's going. For example:
forward 5 down 5 forward 8 up 3 down 8 forward 2
Your horizontal position and depth both start at 0. The steps above would then modify them as follows:
forward 5
adds 5 to your horizontal position, a total of 5.down 5
adds 5 to your depth, resulting in a value of 5.forward 8
adds 8 to your horizontal position, a total of 13.up 3
decreases your depth by 3, resulting in a value of 2.down 8
adds 8 to your depth, resulting in a value of 10.forward 2
adds 2 to your horizontal position, a total of 15.After following these instructions, you would have a horizontal position of 15 and a depth of 10. (Multiplying these together produces 150.)
Read input
Like every day, we'll start by reading our input data using the helper function in utils.py
.
from utils import read_input
def transformer(instruction):
op, delta = instruction.split(' ')
delta = int(delta)
return [op, delta]
instructions = read_input(2, transformer)
Part 1
Named tuples
This challenge is a great place to use named tuples! They make the code much nicer to read and easier to interact with since we have a position of two values (depth and horizontal position) so we could use a list ([0, 1]
) or a tuple ((0, 1)
) but we'd always have to try to remember which one is which.
Using namedtuple from the collections
module, we can give our position and its values names and refer to those while being completely compatible with any code that deals with regular tuples.
And not only that, it has a really nice print output so we can just print the tuple itself and not have to wrap it around custom f-strings to showcase what value is what.
from collections import namedtuple
Position = namedtuple('Position', ['depth', 'horizontal'])
Pattern matching
Finally I get to use pattern matching that was added to Python in the latest 3.10 release!
Pattern matching is really nice for situations like these where you have a finite set of options.
If you attempt to run the below code yourself and get a syntax error on match op
, check that you're running 3.10 or later version of Python (you can do it with python --version
or within Python with
import platform
print(platform.python_version())
def calculate_new_position(instructions):
depth = 0
horizontal = 0
for op, delta in instructions:
match op:
case 'forward':
horizontal += delta
case 'up':
depth -= delta
case 'down':
depth += delta
case _:
pass
return Position(depth, horizontal)
part1_final_position = calculate_new_position(instructions)
print(f'Final position: {part1_final_position}')
Final position: Position(depth=1030, horizontal=2010)
Calculate the horizontal position and depth you would have after following the planned course.
What do you get if you multiply your final horizontal position by your final depth?
print(f'Result: {part1_final_position.depth * part1_final_position.horizontal}')
Result: 2070300
Part 2
Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated.
In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:
down X increases your aim by X units.
up X decreases your aim by X units.
forward X does two things:
- It increases your horizontal position by X units.
- It increases your depth by your aim multiplied by X.
Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction.
Now, the above example does something different:
forward 5
adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
down 5
adds 5 to your aim, resulting in a value of 5.
forward 8
adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 85=40.
up 3
decreases your aim by 3, resulting in a value of 2.
down 8
adds 8 to your aim, resulting in a value of 10.
forward 2
adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 210=20 to a total of 60.After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.)
To adjust to the new process, let's create a new function that takes the aim into account.
def calculate_real_position(instructions):
depth = 0
horizontal = 0
aim = 0
for op, delta in instructions:
match op:
case 'forward':
horizontal += delta
depth += aim * delta
case 'up':
aim -= delta
case 'down':
aim += delta
case _:
pass
return Position(depth, horizontal)
part2_final_position = calculate_real_position(instructions)
print(part2_final_position)
Position(depth=1034321, horizontal=2010)
print(f'Result: {part2_final_position.depth * part2_final_position.horizontal}')
Result: 2078985210