aboutsummaryrefslogtreecommitdiff
path: root/challenge-100/abigail/ruby
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-02-19 01:47:09 +0100
committerAbigail <abigail@abigail.be>2021-02-19 01:47:09 +0100
commit5ab72f71a1b4686f5acf6ae5562f2829b0ee9c6e (patch)
treeece74ed7de09fb64fead2a94f7908c5b4f28b410 /challenge-100/abigail/ruby
parent66a910dbb2a803dc8e785b6564d4f52eb4e4f560 (diff)
downloadperlweeklychallenge-club-5ab72f71a1b4686f5acf6ae5562f2829b0ee9c6e.tar.gz
perlweeklychallenge-club-5ab72f71a1b4686f5acf6ae5562f2829b0ee9c6e.tar.bz2
perlweeklychallenge-club-5ab72f71a1b4686f5acf6ae5562f2829b0ee9c6e.zip
Ruby solution for week 100, part 1
Diffstat (limited to 'challenge-100/abigail/ruby')
-rw-r--r--challenge-100/abigail/ruby/ch-1.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-100/abigail/ruby/ch-1.rb b/challenge-100/abigail/ruby/ch-1.rb
new file mode 100644
index 0000000000..012c2e700f
--- /dev/null
+++ b/challenge-100/abigail/ruby/ch-1.rb
@@ -0,0 +1,43 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-1.rb < input-file
+#
+
+ARGF . each_line do |_|
+ #
+ # Parse data. Note that we have to map the hours and
+ # minutes from strings to integers.
+ #
+ m = _ . match /(?<hour>[0-9]+):(?<minute>[0-9]+)\s*(?<ampm>[ap]?)/
+ hour = m [:hour] . to_i
+ minute = m [:minute] . to_i
+ ampm = m [:ampm]
+
+ #
+ # Calculate the new AM/PM marker
+ #
+ new_ampm = ampm == "" ? hour >= 12 ? "pm" : "am" : ""
+
+ #
+ # Calculate the new hour
+ #
+ hour %= 12
+
+ if ampm == "" && hour == 0
+ then hour = 12
+ end
+
+ if ampm == "p"
+ then hour += 12
+ end
+
+ #
+ # Print the result
+ #
+ puts sprintf("%02d:%02d%s", hour, minute, new_ampm)
+end