aboutsummaryrefslogtreecommitdiff
path: root/challenge-100/abigail/ruby
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-02-19 02:12:08 +0100
committerAbigail <abigail@abigail.be>2021-02-19 02:12:08 +0100
commitc91195bb834b740c54cbd0073961f443bacf4f0e (patch)
tree589df4bca275098a5585b5a304b96cdb57a583a3 /challenge-100/abigail/ruby
parent5ab72f71a1b4686f5acf6ae5562f2829b0ee9c6e (diff)
downloadperlweeklychallenge-club-c91195bb834b740c54cbd0073961f443bacf4f0e.tar.gz
perlweeklychallenge-club-c91195bb834b740c54cbd0073961f443bacf4f0e.tar.bz2
perlweeklychallenge-club-c91195bb834b740c54cbd0073961f443bacf4f0e.zip
Ruby solution for week 100, part 2
Diffstat (limited to 'challenge-100/abigail/ruby')
-rw-r--r--challenge-100/abigail/ruby/ch-2.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-100/abigail/ruby/ch-2.rb b/challenge-100/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..1e0396dcef
--- /dev/null
+++ b/challenge-100/abigail/ruby/ch-2.rb
@@ -0,0 +1,35 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb < input-file
+#
+
+numbers = []
+
+#
+# Parse the data: iterate over the lines of input, split each
+# line on whitespace, turn each item into an integer, and
+# add the split elements as a row of numbers to the array numbers.
+#
+ARGF . each_line do |_|
+ numbers << _ . split . map {|_| _ . to_i}
+end
+
+#
+# Calculate the minimum path, bottom to top
+#
+(numbers . length - 2) . downto (0) do |x|
+ (0) . upto (numbers [x] . length - 1) do |y|
+ numbers [x] [y] += [numbers [x + 1] [y],
+ numbers [x + 1] [y + 1]] . min
+ end
+end
+
+#
+# Print result
+#
+puts numbers [0] [0]