Juha-Matti Santala
Community Builder. Dreamer. Adventurer.

Advent of Code - 2015

This is a solution to Day 1 of Advent of Code 2015.

Day 1 - Not Quite Lisp

I solved these 2015 puzzles in 2021 out of curiosity to see how the puzzles and the lore has changed over the years. Welcome to the journey with me.

On each day, I'll combine my own commentary with the code that solves the puzzles.

Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th.

Collect stars by helping Santa solve puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

Here's an easy puzzle to warm you up.

Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.

An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.

The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors.

For example:

  • (()) and ()() both result in floor 0.
  • ((( and (()(()( both result in floor 3.
  • ))((((( also results in floor 3.
  • ()) and ))( both result in floor -1 (the first basement level).
  • ))) and )())()) both result in floor -3.

Read input

To read inputs, I have a helper function to make life nicer.

It accepts the day as integer and an optional transformer function that is run on each line. By default, the transformer function is str and reads the line as a string.

from utils import read_input

instructions = read_input(1)[0]

Part 1

To what floor do the instructions take Santa?

Since we're only interested in the final floor, it doesn't matter in which order we do them.

Here we count the times we go up and we count the times we go down and subtract from each other for the result.

up = instructions.count('(')
down = instructions.count(')')

result = up - down
print(f'Solution: {result}')
assert result == 138

Part 2

Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on.

For example:

  • ) causes him to enter the basement at character position 1.
  • ()()) causes him to enter the basement at character position 5.

In part 2, we need to run the floors in order to find out where we reach -1.

def find_basement(instructions):
    floor = 0
    for idx, instruction in enumerate(instructions):
        match instruction:
            case '(':
                floor +=1
            case ')':
                floor -= 1
            case _:
                raise Exception(f'Incorrect input: {instruction}')
        if floor == -1:
            return idx + 1
    raise Exception(f'Never reached basement')

What is the position of the character that causes Santa to first enter the basement?

result = find_basement(instructions)
print(f'Solution: {result}')
assert result == 1771