2022-11-23 — 2 min read

Advent of Code 2015, Day 1 – In Ruby

Let’s get started: check out the first puzzle of the first day of the first year!

The first half of the puzzle

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.

To what floor do the instructions take Santa?

This seems to be quite easy: we’ll have to count the number of opening parentheses, then subtract from them the number of closing parentheses. The order of the parentheses is irrelevant because addition is commutative.

Alternatively, we could iterate over the input string and keep track of the current floor, but that is just a lot of extra work for no gain.

The solution:

#!/usr/bin/env ruby

f = File.read(ARGV.first)
puts "Floor: #{f.count('(') - f.count(')')}"

First, we read the input file which is given to the program in the first command-line argument.

Then, we count the number of each type of parentheses, calculate the difference, and print it.

The second half of the puzzle

Our answer was correct for the first puzzle, so here is the second one:

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.

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

This time, we can’t avoid iterating over the input, because we’ll have to find the first position where Santa enters the basement, and for that, we’ll have to go through the input sequentially.

The solution:

#!/usr/bin/env ruby

f = File.read(ARGV.first)
floor = 0
position = 1

f.each_char do |c|
  case c
  when '('
    floor += 1
  when ')'
    floor -= 1
  end
  break if floor == -1
  position += 1
end

puts "Position: #{position}"

We’ll use two variables, one to keep track of the current floor, and another to save our current position in the string. Then we iterate over the string, increasing or decreasing the floor number, depending on the current character. Once we enter the basement, we break the loop and print our position.

The code with my input text is available in the GitHub repo.

Thanks for reading! If you have any comments, additions, or corrections, feel free to reach me via e-mail.

Copyright © 2023 csm.hu
Contact