Skip to content

Instantly share code, notes, and snippets.

@tom-lord
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tom-lord/d93d5872e4e3be07769b to your computer and use it in GitHub Desktop.
Save tom-lord/d93d5872e4e3be07769b to your computer and use it in GitHub Desktop.
Now this is just silly...
PROBLEMS, SCORES = 6, [10, 7, 4, 1, 0] # Takes ~2 minutes for 20 problems and 5 scores :P
puts "#{SCORES.join(", ")}, Total"
puts (0..(PROBLEMS+1)**SCORES.length).lazy.map{|x| x.to_s(PROBLEMS+1).rjust(SCORES.length, '0').split("").map{|y| y.to_i(PROBLEMS+1)} }.reject{|x| x.inject(:+) != PROBLEMS}.map { |p| "#{p.join(", ")}, #{p.each_with_index.map {|n, i| n*SCORES[i]}.inject(:+)}" }.to_a
# Here is a "spaced out" version, you you can actually read it!
#
#puts (0..(PROBLEMS+1)**SCORES.length)
# .lazy # An optimisation, to prevent 1000000s of arrays being held in memory!
# .map{|x|
# x.to_s(PROBLEMS+1) # Convert to string, of base 7 (or whatever - i.e. this will still work for PROBLEMS=20)
# .rjust(SCORES.length, '0') # Pad with zeros, e.g. 15 is now mapped to "00021"
# .split("") # Split into array of chars, e.g. 15 is now ["0","0","0","2","1"]
# .map{|y| y.to_i(PROBLEMS+1)} # Map back into decimal numbers, from base 7 strings, e.g. 15 is now [0,0,0,2,1]
# }
# .reject{|x| x.inject(:+) != PROBLEMS} # Reject all arrays (most of them!!) whose values do not add up to 6
# .map { |p| "#{p.join(", ")}, #{p.each_with_index.map {|n, i| n*SCORES[i]}.inject(:+)}" } # Create a string with each "good" array!
# .to_a # "Undo the lazy method"
@tom-lord
Copy link
Author

This was written as a challenge to "produce the correct output, in as few lines as possible".

I do not normally write code this ugly and/or inefficient!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment