aboutsummaryrefslogtreecommitdiff
path: root/2022/day1.rb
diff options
context:
space:
mode:
authorMatias Linares <matias@deprecated.org>2023-12-03 15:11:37 -0300
committerMatias Linares <matias@deprecated.org>2023-12-03 15:11:37 -0300
commitd073dcaa66567e58d6f9a36b15c9e54b91ed8c54 (patch)
treeb26480988df75847eba75892bdf33089ec08bcd7 /2022/day1.rb
parent129ac460f73972097700ad2def6b4c36d6ebe6e1 (diff)
downloadadvent-of-code-d073dcaa66567e58d6f9a36b15c9e54b91ed8c54.tar.gz
Add 2022 and 2023
Diffstat (limited to '2022/day1.rb')
-rw-r--r--2022/day1.rb48
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)