Chinese zodiac: Difference between revisions

m
no edit summary
(RPL: add section)
mNo edit summary
 
(18 intermediate revisions by 10 users not shown)
Line 1:
{{task}}Determine the Chinese zodiac sign and related associations for a given year.
 
Traditionally,In the Chinese have countedcalendar, years are identified using two lists of labels, one of length 10 (the "celestial" (or stems"heavenly") "stems" – and one of length 12 (the "terrestrial" (or branches"earthly") "branches". The labels do not really have any meaning outside their positions in the two lists; they're simply a traditional enumeration device, used much as Westerners use letters and numbers. They were historically used for months and days as well as years, and the stems are still sometimes used for school grades.
 
Years cycle through both lists concurrently, so that both stem and branch advance each year; if we used Roman letters for the stems and numbers for the branches, consecutive years would be labeled A1, B2, C3, etc. Since the two lists are different lengths, they cycle back to their beginning at different points: after J10 we get A11, and then after B12 we get C1. However, since both lists are of even length, only like-parity pairs occur (A1, A3, A5, but not A2, A4, A6), so only half of the 120 possible pairs are included in the sequence. The result is a repeating 60-year pattern within which each pairname of namespair occurs only once.
 
Mapping the branches to twelve traditional animal deities results in the well-known "Chinese zodiac", assigning each year to a given animal. For example, SundaySaturday, JanuaryFebruary 2210, 20232024 CE (in the common Gregorian calendar) began the lunisolar Year of the RabbitDragon.
 
The celestial stems do not have a one-to-one mapping like that of the branches to animals; however, the five <i>pairs</i> of consecutive stems are each associated with one of the five traditional Chinese<i>wǔxíng</i> elements (Wood, Fire, Earth, Metal, and Water). Further, one of the two years within each element is assigned to <i>yin</i>, the other to <i>yang</i>.
 
Thus, the Chinese year beginning in 20232024 CE is also the yinyang year of WaterWood. Since 12 is an even number, the association between animals and yin/yang aspect doesn't change; consecutive Years of the RabbitDragon will cycle through the five elements, but will always be yinyang.
 
;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.
Line 19:
* The element cycle runs in this order: Wood, Fire, Earth, Metal, Water.
* Each element gets two consecutive years; a yang followed by a yin.
* The currentfirst 60-year cycle(Wood beganRat, inyang) 1984;of anythe multiple ofcurrent 60-year yearscycle frombegan thatin point may be used to reckon1984 fromCE.
 
Thus,The year 1 of a cycle is thelunisolar year ofbeginning thein Wood Rat (yang), year 2 the Wood Ox (yin), and year 3 the Fire Tiger (yang). The year2024 2023CE - which, as already noted, is the year of the WaterWood RabbitDragon (yinyang) - is the 40th year41st of the current cycle.
 
;Information for optional task:
Line 27:
* The twelve terrestrial branches are '''子''' ''zĭ'', '''丑''' ''chŏu'', '''寅''' ''yín'', '''卯''' ''măo'', '''辰''' ''chén'', '''巳''' ''sì'', '''午''' ''wŭ'', '''未''' ''wèi'', '''申''' ''shēn'', '''酉''' ''yŏu'', '''戌''' ''xū'', '''亥''' ''hài''. In ASCII Pinyin, those are "zi3", "chou3", "yin2", "mao3", "chen2", "si4", "wu3", "wei4", "shen1", "you3", "xu1", and "hai4".
 
Therefore 1984 was '''甲子''' (''jiă-zĭ'', or jia3-zi3)., while 20232024 is '''癸卯甲辰''' (''gŭijĭa-măochén'' or gui3jia3-mao3chen2).
=={{header|11l}}==
{{trans|C++}}
Line 385:
2017 Fire Rooster Yin
</pre>
=={{header|APL}}==
{{works with|Dyalog APL}}
{{works with|GNU APL}}
 
This simply creates a vector of the attributes of the year.
 
<syntaxhighlight lang="apl">hanzi ← '甲' '乙' '丙' '丁' '戊' '己' '庚' '辛' '壬' '癸' '子' '丑' '寅' '卯' '辰' '巳' '午' '未' '申' '酉' '戌' '亥'
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'
 
pinyinFor ← { pinyin /⍨ ⍵ ⍷ hanzi }
 
stems ← '甲' '乙' '丙' '丁' '戊' '己' '庚' '辛' '壬' '癸'
branches ← '子' '丑' '寅' '卯' '辰' '巳' '午' '未' '申' '酉' '戌' '亥'
animals ← 'Rat' 'Ox' 'Tiger' 'Rabbit' 'Dragon' 'Snake' 'Horse' 'Goat' 'Monkey' 'Rooster' 'Dog' 'Pig'
elements ← 'Wood' 'Fire' 'Earth' 'Metal' 'Water'
aspects ← 'yang' 'yin'
 
position ← { 1 + 60 | ⍵ - 4 }
item ← { ⍺ ⌷⍨ 1 + (≢⍺) | 1 -⍨ position ⍵ }
celestial ← { stems item ⍵ }
terrestrial ← { branches item ⍵ }
animal ← { animals item ⍵ }
aspect ← { aspects item ⍵ }
element ← { elements ⌷⍨ 1+⌊2÷⍨1×⍨10 | 1 -⍨ position ⍵ }
 
∇vec ← cz year ; cs ; tb
cs ← celestial year
tb ← terrestrial year
vec ← year, (position year), cs, tb, (pinyinFor cs), (pinyinFor tb), (element year), (animal year), (aspect year)
10 1 ⍴ cz ¨ 1935 1938 1941 1944 1947 1968 1972 1976 2003 2006</syntaxhighlight>
 
{{Out}}
<pre>
1935 12 乙亥 yĭ hài Wood Pig yin
1938 15 戊寅 wù yín Earth Tiger yang
1941 18 辛巳 xīn sì Metal Snake yin
1944 21 甲申 jiă shēn Wood Monkey yang
1947 24 丁亥 dīng hài Fire Pig yin
1968 45 戊申 wù shēn Earth Monkey yang
1972 49 壬子 rén zĭ Water Rat yang
1976 53 丙辰 bĭng chén Fire Dragon yang
2003 20 癸未 gŭi wèi Water Goat yin
2006 23 丙戌 bĭng xū Fire Dog yang
</pre>
 
=={{header|AppleScript}}==
{{Trans|JavaScript}}
Line 683 ⟶ 729:
dīngyŏu huǒ jī yīn
34/60 fire rooster </pre>
 
=={{header|AutoHotkey}}==
Chinese Animal/Element/Yin/Yang Characters Copied From "AppleScript"
Line 870 ⟶ 917:
{{Out}}
https://www.dropbox.com/s/ckhu3u5pbwq7p5n/Chinese_zodiac%20%28MSX%29.png?dl=0
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Zodiac.bas"
110 STRING ANIMALS$(0 TO 11)*7,ELEMENTS$(0 TO 4)*5,YINYANG$(0 TO 1)*4
120 FOR I=0 TO 11
130 READ ANIMALS$(I)
140 NEXT
150 FOR I=0 TO 4
160 READ ELEMENTS$(I)
170 NEXT
180 LET YINYANG$(0)="Yang":LET YINYANG$(1)="Yin"
190 CALL ZODIAC(1935):CALL ZODIAC(1938)
200 CALL ZODIAC(1968):CALL ZODIAC(1972)
210 CALL ZODIAC(1976):CALL ZODIAC(2017)
220 DEF ZODIAC(YEAR)
230 LET YELEMENT$=ELEMENTS$(MOD((YEAR-4),10)/2)
240 LET YANIMAL$=ANIMALS$(MOD((YEAR-4),12))
250 LET YYINYANG$=YINYANG$(MOD(YEAR,2))
260 PRINT YEAR;"is the year of the ";YELEMENT$;" ";YANIMAL$;" (";YYINYANG$;")."
270 END DEF
280 DATA Rat,Ox,Tiger,Rabbit,Dragon,Snake, Horse,Goat,Monkey,Rooster,Dog,Pig
290 DATA Wood,Fire,Earth,Metal,Water</syntaxhighlight>
 
==={{header|QBasic}}===
Line 1,104 ⟶ 1,173:
</pre>
=={{header|Commodore BASIC}}==
{{works with|Commodore 64/128}}
<syntaxhighlight lang="basic">1000 rem display the chinese zodiac for a given year
1010 poke 53281,7: rem yellow background
Line 1,178 ⟶ 1,247:
{{Out}}
https://i.imgur.com/f3Fo1xm.png
 
=={{header|Common Lisp}}==
{{trans|Ruby}}
Line 1,471 ⟶ 1,541:
1985 is the year of the Wood Ox (yin). 乙丑
2017 is the year of the Fire Rooster (yin). 丁酉</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
animal$[] = [ "Rat" "Ox" "Tiger" "Rabbit" "Dragon" "Snake" "Horse" "Goat" "Monkey" "Rooster" "Dog" "Pig" ]
element$[] = [ "Wood" "Fire" "Earth" "Metal" "Water" ]
yingyang$[] = [ "Yang" "Yin" ]
animal_ch$[] = strchars "子丑寅卯辰巳午未申酉戌亥"
stem_ch$[] = strchars "甲乙丙丁戊己庚辛壬癸"
#
proc get year . el$ an$ yy$ anch$ stch$ .
idx = (year - 4) mod 10
el$ = element$[idx div 2 + 1]
stch$ = stem_ch$[idx + 1]
idx = (year - 4) mod 12
an$ = animal$[idx + 1]
anch$ = animal_ch$[idx + 1]
yy$ = yingyang$[year mod 2 + 1]
.
proc zodiac year . .
get year el$ an$ yy$ anch$ stch$
print year
cycle = (year - 1983) mod1 60
print " " & el$ & " " & an$ & " " & yy$ & " " & cycle & "/60"
print " " & stch$ & anch$
print ""
.
zodiac 1935
zodiac 1938
zodiac 1968
zodiac 1972
zodiac 1976
zodiac 1984
zodiac 2017
</syntaxhighlight>
 
 
=={{header|Excel}}==
===LAMBDA===
Line 1,875 ⟶ 1,980:
| style="text-align:center" | bright
|}
 
=={{header|F Sharp|F#}}==
<syntaxhighlight lang="fsharp">
Line 2,017 ⟶ 2,123:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Chinese_zodiac}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Chinese zodiac 01.png]]
 
'''Test case'''
 
[[File:Fōrmulæ - Chinese zodiac 02.png]]
 
[[File:Fōrmulæ - Chinese zodiac 03.png]]
 
In '''[https://formulae.org/?example=Chinese_zodiac this]''' page you can see the program(s) related to this task and their results.
=={{header|Go}}==
<syntaxhighlight lang="go">package main
Line 2,531 ⟶ 2,644:
rényín shuǐ hǔ yáng
39/60 water tiger </pre>
=={{header|jq}}==
'''Works with jq and gojq, that is, the C and Go implementations of jq.'''
 
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang=jq>
def ChineseZodiac: {
animals : ["Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
"Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"],
aspects : ["Yang","Yin"],
elements : ["Wood", "Fire", "Earth", "Metal", "Water"],
stems : ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"],
branches : ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"],
sNames : ["jiă", "yĭ", "bĭng", "dīng", "wù", "jĭ", "gēng", "xīn", "rén", "gŭi"],
bNames : ["zĭ", "chŏu", "yín", "măo", "chén", "sì", "wŭ", "wèi", "shēn", "yŏu", "xū", "hài"]
};
 
def ChineseZodiac($year):
($year - 4) as $y
| ($y % 10) as $s
| ($y % 12) as $b
| ChineseZodiac
| { year : $year,
stem : .stems[$s],
branch : .branches[$b],
sName : .sNames[$s],
bName : .bNames[$b],
element : .elements[($s/2)|floor],
animal : .animals[$b],
aspect : .aspects[$s % 2],
cycle : ($y % 60 + 1)
};
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Input: ChineseZodiac($year)
def toString:
(.sName + "-" + .bName | lpad(9)) as $name
| (.element|lpad(7)) as $elem
| (.animal|lpad(7)) as $anim
| (.aspect|lpad(6)) as $aspt
| ((.cycle|lpad(2)) + "/60") as $cycl
| "\(.year) \(.stem)\(.branch) \($name) \($elem) \($anim) \($aspt) \($cycl)" ;
 
 
"Year Chinese Pinyin Element Animal Aspect Cycle",
"---- ------- --------- ------- ------- ------ -----",
(ChineseZodiac(1935, 1938, 1968, 1972, 1976, 1984, 2017, 2020) | toString)
</syntaxhighlight>
{{output}}
<pre>
Year Chinese Pinyin Element Animal Aspect Cycle
---- ------- --------- ------- ------- ------ -----
1935 乙亥 yĭ-hài Wood Pig Yin 12/60
1938 戊寅 wù-yín Earth Tiger Yang 15/60
1968 戊申 wù-shēn Earth Monkey Yang 45/60
1972 壬子 rén-zĭ Water Rat Yang 49/60
1976 丙辰 bĭng-chén Fire Dragon Yang 53/60
1984 甲子 jiă-zĭ Wood Rat Yang 1/60
2017 丁酉 dīng-yŏu Fire Rooster Yin 34/60
2020 庚子 gēng-zĭ Metal Rat Yang 37/60
</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
Line 2,601 ⟶ 2,776:
1976: 丙辰 (bĭng-chén, Fire Dragon; yang - year 53 of the cycle)
2018: 戊戌 (wù-xū, Earth Dog; yang - year 35 of the cycle)</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.2
Line 3,689 ⟶ 3,865:
|dark
|}
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ dup echo
say " is the year of the "
4 - dup 10 mod 2 /
[ table $ "Wood" $ "Fire" $ "Earth"
$ "Metal" $ "Water" ]
do echo$
sp
dup 12 mod
[ table
$ "Rat" $ "Ox" $ "Tiger" $ "Rabbit"
$ "Dragon" $ "Snake" $ "Horse" $ "Goat"
$ "Monkey" $ "Rooster" $ "Dog" $ "Pig" ]
do echo$
say " ("
2 mod
[ table $ "yang" $ "yin" ]
do echo$
say ")." cr ] is zodiac ( $ --> )
 
' [ 1935 1938 1968 1972 1976 1984 1985 2017 ]
witheach zodiac</syntaxhighlight>
 
{{out}}
 
<pre>1935 is the year of the Wood Pig (yin).
1938 is the year of the Earth Tiger (yang).
1968 is the year of the Earth Monkey (yang).
1972 is the year of the Water Rat (yang).
1976 is the year of the Fire Dragon (yang).
1984 is the year of the Wood Rat (yang).
1985 is the year of the Wood Ox (yin).
2017 is the year of the Fire Rooster (yin).
</pre>
 
=={{header|Racket}}==
{{trans|Common Lisp}}
Line 3,824 ⟶ 4,037:
3 PICK 12 MOD 1 + GET +
SWAP 2 MOD " (yin)" " (yang)" IFTE +
≫ ''''CHZOD'''' STO
 
''''CHZOD'''' STO
2022 '''CHZOD'''
{{out}}
Line 4,207 ⟶ 4,420:
dim @a(12) ' the animals
 
Push Dup("yang"), Dup("yin") ' fill Ying/Yang table
For i = 1 to 0 stepStep -1 : @y(i) = Pop() : Next i
' fill Elements table
Push Dup("Wood"), Dup("Fire"), Dup("Earth"), Dup("Metal"), Dup("Water")
For i = 4 to 0 stepStep -1 : @e(i) = Pop() : Next i
' fill Animals table
Push Dup("Rat"), Dup("Ox"), Dup("Tiger"), Dup( "Rabbit"), Dup( "Dragon"), "Snake"
Push Dup("SnakeHorse"), Dup("HorseGoat"), Dup("GoatMonkey"), "Rooster", Dup("MonkeyDog"), Dup( "RoosterPig")
Push Dup("Dog"), Dup("Pig")
 
For i = 11 to 0 step -1 : @a(i) = Pop() : Next i
Line 4,247 ⟶ 4,459:
0 OK, 0:1047
</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bash|4+}}
Line 4,557 ⟶ 4,770:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
class ChineseZodiac {
Line 4,615 ⟶ 4,828:
2020 庚子 gēng-zĭ Metal Rat Yang 37/60
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
int Animals, Elements;
 
func GetElement(Year);
int Year, Element;
[Element:= rem((Year-4)/10) / 2;
return Elements(Element);
];
 
func GetAnimal(Year);
int Year;
return Animals(rem((Year-4)/12));
 
func GetYY(Year);
int Year;
if rem(Year/2) = 0 then return "yang"
else return "yin";
 
int Year, Years, I;
[Years:= [ 1935, 1938, 1968, 1972, 1976, 2017 ];
Animals:= [ "Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig" ];
Elements:= [ "Wood","Fire","Earth","Metal","Water" ];
for I:= 0 to 6-1 do
[Year:= Years(I);
Print("%d is the year of the %s %s (%s).\n", Year, GetElement(Year), GetAnimal(Year), GetYY(Year));
]
]</syntaxhighlight>
{{out}}
<pre>
1935 is the year of the Wood Pig (yin).
1938 is the year of the Earth Tiger (yang).
1968 is the year of the Earth Monkey (yang).
1972 is the year of the Water Rat (yang).
1976 is the year of the Fire Dragon (yang).
2017 is the year of the Fire Rooster (yin).
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">
1,480

edits