diff options
Diffstat (limited to '2022/day1.rb')
-rw-r--r-- | 2022/day1.rb | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/2022/day1.rb b/2022/day1.rb new file mode 100644 index 0000000..f7a7c24 --- /dev/null +++ b/2022/day1.rb @@ -0,0 +1,48 @@ + + +TEST_S = <<-TEST +1000 +2000 +3000 + +4000 + +5000 +6000 + +7000 +8000 +9000 + +10000 +TEST + +def elves_food(input_str) + return input_str.split(/^$/).map do + |elf_rations| + elf_rations.strip.split("\n").map(&:to_i) + end +end + +def calories_by_elf(input_str) + elves_food(input_str).map(&:sum) +end + +def max_calories(input_str) + calories_by_elf(input_str).max +end + +def top_3_calories(input_str) + calories_by_elf(input_str).sort.reverse.take(3).sum +end + +def day1 + File.open('day1.txt') do |f| + input_str = f.read + puts "part1: #{max_calories(input_str)}" + puts "part2: #{top_3_calories(input_str)}" + end +end + +day1() +#puts elves_food(TEST_S) |