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
end
end
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
input.push(builtIn)
input.unshift(0)
i = 1
previousVal = 0
runCount = 1
product = []
while i < input.count
if input[i] - previousVal == 1
runCount += 1
else
case runCount
when 3
product.push(2)
when 4
product.push(4)
when 5
product.push(7)
end
runCount = 1
end
previousVal = input[i]
i += 1
end
puts "Part 2: #{product.inject(:*)}"
Top comments (0)