Last active
December 24, 2021 20:54
-
-
Save wmakley/c42d544bd286f409de60dbd09a2ba730 to your computer and use it in GitHub Desktop.
Count the number of times each gift is given
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
verses = { | |
1 => "Patridges in a pear tree", | |
2 => "Turtle doves", | |
3 => "French hens", | |
4 => "Calling birds", | |
5 => "Golden rings", | |
6 => "Geese a layin'", | |
7 => "Swans a swimmin'", | |
8 => "Maids a milkin'", | |
9 => "Ladies dancing", | |
10 => "Lords a leapin'", | |
11 => "Pipers pipin'", | |
12 => "Twelve drummers drummin'" | |
} | |
counts = {} | |
1.upto verses.size do |verse| | |
1.upto verse do |line| | |
counts[line] ||= 0 | |
counts[line] += line | |
end | |
end | |
counts.each do |verse, count| | |
puts "#{count} #{verses[verse]}" | |
end | |
total = counts.values.sum | |
puts "Total gifts: #{total}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment