diff options
| author | Abigail <abigail@abigail.be> | 2021-03-12 19:21:36 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-14 19:59:46 +0100 |
| commit | a2ac29d46cceac9bcf5fa5e9881acd5e4496d7e2 (patch) | |
| tree | 04ac18eee777b21cd80d2901ff173fd005b51dae /challenge-103 | |
| parent | 790b1c8fe5584b1c6e5754744d7c7d0922a1fd65 (diff) | |
| download | perlweeklychallenge-club-a2ac29d46cceac9bcf5fa5e9881acd5e4496d7e2.tar.gz perlweeklychallenge-club-a2ac29d46cceac9bcf5fa5e9881acd5e4496d7e2.tar.bz2 perlweeklychallenge-club-a2ac29d46cceac9bcf5fa5e9881acd5e4496d7e2.zip | |
Lua solution for week 103, part 1
Diffstat (limited to 'challenge-103')
| -rw-r--r-- | challenge-103/abigail/README.md | 2 | ||||
| -rw-r--r-- | challenge-103/abigail/lua/ch-1.lua | 45 |
2 files changed, 46 insertions, 1 deletions
diff --git a/challenge-103/abigail/README.md b/challenge-103/abigail/README.md index 03b5dc3a3c..8a14476af8 100644 --- a/challenge-103/abigail/README.md +++ b/challenge-103/abigail/README.md @@ -123,7 +123,7 @@ Output: ~~~~ ### Solutions -* [Lua](lua/ch-1.lua) +* [Lua](lua/ch-2.lua) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-103/abigail/lua/ch-1.lua b/challenge-103/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..352158ea06 --- /dev/null +++ b/challenge-103/abigail/lua/ch-1.lua @@ -0,0 +1,45 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < 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. +-- + +local yin_yang = {"Yang", "Yin"}; +local heavenly_stems = {"Metal", "Metal", "Water", "Water", + "Wood", "Wood", "Fire", "Fire", + "Earth", "Earth"}; +local earthly_branches = {"Monkey", "Rooster", "Dog", "Pig", + "Rat", "Ox", "Tiger", "Rabbit", + "Dragon", "Snake", "Horse", "Goat"}; + +for line in io . lines () do + local year = tonumber (line) + io . write (yin_yang [1 + (year % #yin_yang)], " ", + heavenly_stems [1 + (year % #heavenly_stems)], " ", + earthly_branches [1 + (year % #earthly_branches)], "\n") +end |
