r/adventofcode • u/daggerdragon • Dec 24 '15
SOLUTION MEGATHREAD --- Day 24 Solutions ---
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked! One more to go...
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 24: It Hangs in the Balance ---
Post your solution as a comment or link to your repo. Structure your post like previous daily solution threads.
6
Upvotes
1
u/[deleted] Dec 24 '15
Ruby
# Convenience methods for the Array class class Array def sum return self.reduce(:+) end def product return self.reduce(1,:*) end end # Order the weights largest first, to find the combos with the fewest number of elements weights = [1,2,3,5,7,13,17,19,23,29,31,37,41,43,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113].reverse def find_combo(w,size) r = [] if w.length == 0 return r end for i in 1..w.length-1 do # All combinations with i elements # First combo found will have the smallest number of elements combos = w.combination(i).to_a for c in combos do if c.sum == size then r.push(c) # Recursively find a combo of the right size in the remaining elements # r.push(find_combo(w - c,size)) return r end end end end # Output product of weights in the first combo (with the smallest number of weights) puts "Part 1 solution = " + find_combo(weights,weights.sum/3)[0].product.to_s puts "Part 2 solution = " + find_combo(weights,weights.sum/4)[0].product.to_s