aboutsummaryrefslogtreecommitdiff
path: root/challenge-103/abigail/ruby
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-09 00:26:24 +0100
committerAbigail <abigail@abigail.be>2021-03-14 19:59:45 +0100
commitd2bc04d70173727d35ad316ca1fd60d563b36361 (patch)
treea3b721102fe238de0fe379d9a67ca3d850335240 /challenge-103/abigail/ruby
parent6f3ab8c4168aec362fdd50bf6445680bd62cac49 (diff)
downloadperlweeklychallenge-club-d2bc04d70173727d35ad316ca1fd60d563b36361.tar.gz
perlweeklychallenge-club-d2bc04d70173727d35ad316ca1fd60d563b36361.tar.bz2
perlweeklychallenge-club-d2bc04d70173727d35ad316ca1fd60d563b36361.zip
Ruby solution for week 103, part 1
Diffstat (limited to 'challenge-103/abigail/ruby')
-rw-r--r--challenge-103/abigail/ruby/ch-1.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-103/abigail/ruby/ch-1.rb b/challenge-103/abigail/ruby/ch-1.rb
new file mode 100644
index 0000000000..69b6b21561
--- /dev/null
+++ b/challenge-103/abigail/ruby/ch-1.rb
@@ -0,0 +1,45 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-1.rb < input-file
+#
+
+#
+# We're reading years from standard input, one year per line, outputting
+# years from the sexagenary cycle [1]. This is slightly more than what
+# the challenge ask; the challenge asks to output the heavenly stem [2],
+# and the earthly branch [3]. But we also output its Yin/Yang.
+#
+# [1] https://en.wikipedia.org/wiki/Sexagenary_cycle
+# [2] https://en.wikipedia.org/wiki/Heavenly_Stems
+# [3] https://en.wikipedia.org/wiki/Earthly_Branches
+#
+
+#
+# Each of the cycles have been rotated so the first entry corresponds to
+# the year 0 in the Proleptic Gregorian calendar. (We're using the
+# convention of having a year 0, as per ISO 8601).
+# That way, we can just mod the year with the number of entries, without
+# first having to subtract something from the year.
+#
+# The heavenly stems last for 2 years, so we just duplicate the entries.
+#
+
+yin_yang = ["Yang", "Yin"]
+heavenly_stems = ["Metal", "Metal", "Water", "Water", "Wood", "Wood",
+ "Fire", "Fire", "Earth", "Earth"]
+earthly_branches = ["Monkey", "Rooster", "Dog", "Pig", "Rat", "Ox",
+ "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat"]
+
+
+ARGF . each_line do |year|
+ year = year . to_i
+ print yin_yang [year % yin_yang . length] + " " +
+ heavenly_stems [year % heavenly_stems . length] + " " +
+ earthly_branches [year % earthly_branches . length] + "\n"
+end
+