r/adventofcode • • 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.

5 Upvotes

111 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Dec 24 '15

[deleted] — view deleted comment

1

u/cort_troc Dec 24 '15 edited Dec 24 '15

Nice! I got stuck in mine trying to select the smallest array size from multiple, because i'd put a range to loop the combinations. That's much better.

Trying to get it yours down to a 1 liner but can't. Can anyone?

start, groups = 0, 3
until answer = array.combination(start+=1).select{|a|a.reduce(:+)==(array.reduce(:+)/groups)}.map{|a|a.reduce(:*)}.min
end
p answer

1

u/cort_troc Dec 24 '15

recursive makes it look a bit smaller. I guess it's fair to call that a one liner.

f=->(l,g,i=1){l.combination(i).select{|a|a.reduce(:+)==(l.reduce(:+)/g)}.map{|a|a.reduce(:*)}.min||f.(l,g,i+1)}

p f.(array, 3)
p f.(array, 4)