AoC2015/day01/solution.rb

18 lines
576 B
Ruby
Raw Permalink Normal View History

2022-07-12 14:54:21 +00:00
#!/bin/env ruby
# Part 1, To what floor do the instructions take Santa?
directions = File.read('./input')
p1answer = directions.each_char.map { |char| char == '(' ? 1 : -1 }.sum
2022-07-12 14:56:07 +00:00
puts 'Part 1 Answer: ' + p1answer.to_s
2022-07-12 14:54:21 +00:00
# Part 2, What is the position of the character that causes Santa to first enter the basement?
directions = directions.each_char.map { |char| char == '(' ? 1 : -1 }
position = 0
catch :basement do
2022-07-12 14:56:07 +00:00
directions.each.with_index do |_x, i|
position = i + 1
throw :basement if directions[0..i].sum == -1
end
2022-07-12 14:54:21 +00:00
end
2022-07-12 14:56:07 +00:00
puts 'Part 2 Answer: ' + position.to_s