DEV Community

Cover image for Advent of Code 2020 - Day 10

Advent of Code 2020 - Day 10

rhyn0bytes profile image Ryan ・1 min read

Day 10 started out relatively easy especially using Ruby's .each_cons(2) to grab pair and find the difference. the meat of my solution for part 1 is:

input.each_cons(2) do |a,b|
  if b-a == 1
    oneJoltCount += 1
  elsif b-a == 3
    threeJoltCount += 1
Enter fullscreen mode Exit fullscreen mode

Since the product of those 2 counters is the answer, I was done!

Part 2 - What am I doing?!?

This part was messy since I didn't feel like writing a path or tree like algorithm to count up all the valid options. I did attempt to use Ruby's combination method and test the output for validity, but this was incredibly memory intensive and didn't feel like setting my computer ablaze this evening.

After doing some combination building by hand, a way of checking for "runs" of consecutive integers for a multiplier became the cleanest way to solve this. I would love to put more detail into this post, but I am far too tired and I still have work tomorrow... gulp

Anyways, my part two solution:

# Part 2 madness
# using chains in order to calculate final total
# chain of 3 = *2, chain of 4 = *4, chain of 5 = *7
i = 1
previousVal = 0
runCount = 1
product = []
while i < input.count
  if input[i] - previousVal == 1
    runCount += 1
    case runCount
    when 3
    when 4
    when 5
    runCount = 1
  previousVal = input[i]
  i += 1

puts "Part 2: #{product.inject(:*)}"
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Editor guide