From d073dcaa66567e58d6f9a36b15c9e54b91ed8c54 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 3 Dec 2023 15:11:37 -0300 Subject: Add 2022 and 2023 --- 2022/day1.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2022/day1.rb (limited to '2022/day1.rb') 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) -- cgit v1.2.3-70-g09d2