aboutsummaryrefslogtreecommitdiff
path: root/challenge-002/abigail/ruby
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-25 17:44:14 +0100
committerAbigail <abigail@abigail.be>2021-01-25 17:44:14 +0100
commit73adfbabd45d6dcced17f410e123349a2b7150a5 (patch)
tree3f3eb4d6421c7c1660b6b3bf900dbc08141a0308 /challenge-002/abigail/ruby
parente17a558b2ebc880537e1743945304532eb2263bb (diff)
downloadperlweeklychallenge-club-73adfbabd45d6dcced17f410e123349a2b7150a5.tar.gz
perlweeklychallenge-club-73adfbabd45d6dcced17f410e123349a2b7150a5.tar.bz2
perlweeklychallenge-club-73adfbabd45d6dcced17f410e123349a2b7150a5.zip
Ruby solution for week 2, part 2
Diffstat (limited to 'challenge-002/abigail/ruby')
-rw-r--r--challenge-002/abigail/ruby/ch-2.rb50
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-002/abigail/ruby/ch-2.rb b/challenge-002/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..95efc096a7
--- /dev/null
+++ b/challenge-002/abigail/ruby/ch-2.rb
@@ -0,0 +1,50 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb {-f | -t} < input-file
+#
+
+require 'optparse'
+
+BASE = 35
+
+#
+# Parse and validate options
+#
+params = ARGV . getopts ('ft')
+do_from_base = params ["f"] ? 1 : 0
+do_to_base = params ["t"] ? 1 : 0
+if do_from_base + do_to_base != 1
+ STDERR . puts "Program requires exactly one of '-f' or '-t'"
+ exit 1
+end
+
+
+def to_base (number)
+ out = ""
+ while number > 0 do
+ rest = number % BASE
+ if rest < 10
+ then char = rest . to_s
+ else char = (65 + rest - 10) . chr
+ end
+ out = char + out
+ number = (number / BASE) . to_i
+ end
+ return out
+end
+
+def from_base (number)
+ return number . to_i (35)
+end
+
+ARGF . each_line do |_|
+ if do_from_base == 1
+ then puts from_base (_)
+ else puts to_base (_ . to_i)
+ end
+end