Chinese zodiac

From Rosetta Code
Revision as of 14:08, 28 January 2017 by Thundergnat (talk | contribs) (→‎{{header|Perl 6}}: return a hash rather than a string; allows customization of display)
Chinese zodiac is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Determine the Chinese zodiac sign and related associations for a given year.

Traditionally, the Chinese have counted years using two simultaneous cycles, one of length 10 (the "celestial stems") and one of length 12 (the "terrestrial branches"); the combination results in a repeating 60-year pattern. Mapping the branches to twelve traditional animal deities results in the well-known "Chinese zodiac", assigning each year to a given animal. For example, Saturday, January 28, 2017 CE (in the common Gregorian calendar) begins the lunisolar year of the Rooster.

The celestial stems have no one-to-one mapping like that of the branches to animals; however, the five pairs of consecutive stems each belong to one of the five traditional Chinese elements (Wood, Fire, Earth, Metal, and Water). Further, one of the two years within each element's governance is associated with yin, the other with yang.

Thus, 2017 is also the yin year of Fire. Note that since 10 and 12 have the same parity, the association between animals and yin/yang doesn't change. Consecutive Years of the Rooster will cycle through the five elements, but will always be yin, despite the mismatch between the male animals and the female aspect.

Task
Create a subroutine or program that will return or output the animal, yin/yang association, and element for the lunisolar year that begins in a given CE year.

You may optionally provide more information in the form of the year's numerical position within the 60-year cycle and/or its actual Chinese stem-branch name (in Han characters or Pinyin transliteration).

Requisite information
  • The animal cycle runs in this order: Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig.
  • The element cycle runs in this order: Wood, Fire, Earth, Metal, Water.
  • The yang year precedes the yin year within each element.
  • The current 60-year cycle began in 1984 CE.

Thus, 1984 was the year of the Wood Rat (yang), 1985 was the year of the Wood Ox (yin), and 1986 the year of the Fire Tiger (yang).

Information for optional task
  • The ten celestial stems are jiă, , bĭng, dīng, , , gēng, xīn, rén, and gŭi. With the ASCII version of Pinyin tones, the names are written "jia3", "yi3", "bing3", "ding1", "wu4", "ji3", "geng1", "xin1", "ren2", and "gui3".
  • The twelve terrestrial branches are , chŏu, yín, măo, chén, , , wèi, shén, yŏu, , hài. In ASCII Pinyin, those are "zi3", "chou3", "yin2", "mao3", "chen2", "si4", "wu3", "wei4", "shen2", "you3", "xu1", and "hai4".

Therefore 1984 was 甲子 (jiă-zĭ, or jia3-zi3). 2017 is the 34th year of the current cycle, 丁酉 (dīng-yŏu or ding1-you3).


Perl 6

Works with: Rakudo version 2017.01
Translation of: Ruby

<lang perl6>sub Chinese-zodiac ( Int $year ) {

   my @heaven = (
       '甲' => 'jiă',
       '乙' => 'yĭ',
       '丙' => 'bĭng',
       '丁' => 'dīng',
       '戊' => 'wù',
       '己' => 'jĭ',
       '庚' => 'gēng',
       '辛' => 'xīn',
       '壬' => 'rén',
       '癸' => 'gŭi'
   );
   my @earth = (
       '子' => 'zĭ',
       '丑' => 'chŏu',
       '寅' => 'yín',
       '卯' => 'măo',
       '辰' => 'chén',
       '巳' => 'sì',
       '午' => 'wŭ',
       '未' => 'wèi',
       '申' => 'shén',
       '酉' => 'yŏu',
       '戌' => 'xū',
       '亥' => 'hài'
   );
   my @animal  = <Rat   Ox   Tiger  Rabbit  Dragon Snake
                  Horse Goat Monkey Rooster Dog    Pig>;
   my @element = <Wood Fire Earth Metal Water>;
   my @aspect  = <yang yin>;
   my $cycle_year = $year - 4;
   my $i2         = $cycle_year % 2;
   my $i10        = $cycle_year % 10;
   my $i12        = $cycle_year % 12;
   %(
       'Han'     => @heaven[$i10].key ~ @earth[$i12].key,
       'pinyin'  => "{ @heaven[$i10].value }-{ @earth[$i12].value }",
       'element' => @element[$i10 div 2],
       'animal'  => @animal[$i12],
       'aspect'  => @aspect[$i2]
   )

}

  1. TESTING

printf "%d: %s (%s, %s %s; %s)\n",

   $_, .&Chinese-zodiac<Han pinyin element animal aspect>
   for 1935, 1938, 1968, 1972, 1976, DateTime(now).year;</lang>
Output:
1935: 乙亥 (yĭ-hài, Wood Pig; yin)
1938: 戊寅 (wù-yín, Earth Tiger; yang)
1968: 戊申 (wù-shén, Earth Monkey; yang)
1972: 壬子 (rén-zĭ, Water Rat; yang)
1976: 丙辰 (bĭng-chén, Fire Dragon; yang)
2017: 丁酉 (dīng-yŏu, Fire Rooster; yin)

Ruby

This is written as a command-line tool that takes a list of CE year numbers as arguments and outputs their information; if no arguments are supplied, it displays the information for the current year.

<lang ruby># encoding: utf-8 pinyin = {

 '甲' => 'jiă',
 '乙' => 'yĭ',
 '丙' => 'bĭng',
 '丁' => 'dīng',
 '戊' => 'wù',
 '己' => 'jĭ',
 '庚' => 'gēng',
 '辛' => 'xīn',
 '壬' => 'rén',
 '癸' => 'gŭi',
 '子' => 'zĭ',
 '丑' => 'chŏu',
 '寅' => 'yín',
 '卯' => 'măo',
 '辰' => 'chén',
 '巳' => 'sì',
 '午' => 'wŭ',
 '未' => 'wèi',
 '申' => 'shén',
 '酉' => 'yŏu',
 '戌' => 'xū',
 '亥' => 'hài'

} celestial = %w(甲 乙 丙 丁 戊 己 庚 辛 壬 癸) terrestrial = %w(子 丑 寅 卯 辰 巳 午 未 申 酉 戌 亥) animals = %w(Rat Ox Tiger Rabbit Dragon Snake

                  Horse Goat Monkey Rooster Dog    Pig)

elements = %w(Wood Fire Earth Metal Water) aspects = %w(yang yin)

BASE = 4

args = if !ARGV.empty?

        ARGV
      else
        [Time.new.year]
      end

args.each do |arg|

 ce_year = Integer(arg)
 print "#{ce_year}: " if ARGV.length > 1
 cycle_year     = ce_year - BASE
 stem_number    = cycle_year % 10
 stem_han       = celestial[stem_number]
 stem_pinyin    = pinyin[stem_han]
 element_number = stem_number / 2
 element        = elements[element_number]
 branch_number  = cycle_year % 12
 branch_han     = terrestrial[branch_number]
 branch_pinyin  = pinyin[branch_han]
 animal         = animals[branch_number]
 aspect_number = cycle_year % 2
 aspect        = aspects[aspect_number]
 print stem_han, branch_han
 puts " (#{stem_pinyin}-#{branch_pinyin}, #{element} #{animal}; #{aspect})"

end</lang>

Output:

Given arguments 1935 1938 1968 1972 1976:

1935: 乙亥 (yĭ-hài, Wood Pig; yin)
1938: 戊寅 (wù-yín, Earth Tiger; yang)
1968: 戊申 (wù-shén, Earth Monkey; yang)
1972: 壬子 (rén-zĭ, Water Rat; yang)
1976: 丙辰 (bĭng-chén, Fire Dragon; yang)

Given no arguments and run during the year 2017:

丁酉 (dīng-yŏu, Fire Rooster; yin)

zkl

Translation of: Ruby

<lang zkl>fcn ceToChineseZodiac(ce_year){ // --> list of strings

  # encoding: utf-8
  var [const] pinyin=SD(  // create some static variables, SD is small fixed dictionary
    "甲","jiă",  "乙","yĭ",  "丙","bĭng", "丁","dīng", "戊","wù", "己","jĭ",
    "庚","gēng", "辛","xīn", "壬","rén",  "癸","gŭi",
    "子","zĭ",  "丑","chŏu", "寅","yín", "卯","măo",  "辰","chén", "巳","sì",
    "午","wŭ",  "未","wèi",  "申","shén","酉","yŏu",  "戌","xū",   "亥","hài"
  ),
  celestial  =T("甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"),
  terrestrial=T("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"),
  animals    =T("Rat",   "Ox",   "Tiger",  "Rabbit",  "Dragon", "Snake",

"Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"),

  elements   =T("Wood", "Fire", "Earth", "Metal", "Water"),
  aspects    =T("yang","yin"),
  ;
  const BASE=4;

  cycle_year:=ce_year - BASE;
  cycle_year,aspect    := ce_year - BASE,         aspects[cycle_year%2];
  stem_number,element  := cycle_year%10,          elements[stem_number/2];
  stem_han,stem_pinyin := celestial[stem_number], pinyin[stem_han];
  branch_number,animal     := cycle_year%12,      animals[branch_number];
  branch_han,branch_pinyin := terrestrial[branch_number], pinyin[branch_han];
  return(stem_han,branch_han,

stem_pinyin,branch_pinyin, element,animal,aspect) }</lang> <lang zkl>foreach ce_year in (T(1935,1938,1968,1972,1976,Time.Clock.UTC[0])){

  println("%d: %s%s (%s-%s, %s %s; %s)"
          .fmt(ce_year,ceToChineseZodiac(ce_year).xplode()));

}</lang>

Output:
1935: 乙亥 (yĭ-hài, Wood Pig; yin)
1938: 戊寅 (wù-yín, Earth Tiger; yang)
1968: 戊申 (wù-shén, Earth Monkey; yang)
1972: 壬子 (rén-zĭ, Water Rat; yang)
1976: 丙辰 (bĭng-chén, Fire Dragon; yang)
2017: 丁酉 (dīng-yŏu, Fire Rooster; yin)