Skip to content

Instantly share code, notes, and snippets.

@BruceHubbard
Created November 9, 2012 20:56
Show Gist options
  • Save BruceHubbard/4048187 to your computer and use it in GitHub Desktop.
Save BruceHubbard/4048187 to your computer and use it in GitHub Desktop.
Reddit Post
INPUT = "./input/1.txt"
TARGET = 250000
DAYS_LEFT = 7
PEOPLE = [
{:name => "I", :perDay => 1, :yield => 1.0},
{:name => "My son", :perDay => 2, :yield => 0.95},
{:name => "My brother", :perDay => 3, :yield => 0.85},
{:name => "My niece", :perDay => 4, :yield => 0.60}
]
LINES = File.open(INPUT, "r").read.strip.split("\n").collect{|s| s.to_i}
REPLACEMENT_COST = LINES.shift
#puts "People: #{PEOPLE}"
#puts "Replacement Cost: #{REPLACEMENT_COST}"
#puts "Lines: #{LINES}"
#puts "Target: #{TARGET}"
#puts "Total Banana Money: #{LINES.reduce(:+)}"
def naive
total_collected = 0
collected = Hash[PEOPLE.map{|p| [p[:name], []]}]
by_yield = PEOPLE.sort_by {|p| p[:yield]}.reverse
for p in by_yield
for i in 0...(p[:perDay]*DAYS_LEFT)
if total_collected < TARGET then
index_of_biggest_amount = LINES.each_with_index.max[1]
value = LINES[index_of_biggest_amount]
total_collected += value
collected[p[:name]] << [index_of_biggest_amount, value]
LINES[index_of_biggest_amount] = 0
puts "#{p[:name]} smashed panel ##{index_of_biggest_amount} and got $#{value} bringing the total collected to $#{total_collected}"
end
end
end
puts collected
end
naive
Richard-Hubbards-MacBook-Pro:reddit bruce_hubbard$ ruby compute.rb
I smashed panel #18 and got $24858 bringing the total collected to $24858
I smashed panel #37 and got $24780 bringing the total collected to $49638
I smashed panel #86 and got $24533 bringing the total collected to $74171
I smashed panel #65 and got $24358 bringing the total collected to $98529
I smashed panel #93 and got $24218 bringing the total collected to $122747
I smashed panel #90 and got $24081 bringing the total collected to $146828
I smashed panel #54 and got $24011 bringing the total collected to $170839
My son smashed panel #9 and got $24008 bringing the total collected to $194847
My son smashed panel #27 and got $23803 bringing the total collected to $218650
My son smashed panel #91 and got $23193 bringing the total collected to $241843
My son smashed panel #47 and got $23050 bringing the total collected to $264893
{"I"=>[[18, 24858], [37, 24780], [86, 24533], [65, 24358], [93, 24218], [90, 24081], [54, 24011]], "My son"=>[[9, 24008], [27, 23803], [91, 23193], [47, 23050]], "My brother"=>[], "My niece"=>[]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment