Written by

Senior Cloud Architect at InterSystems
Discussion Eduard Lebedyuk 路 Oct 20, 2023

Code Golf: Chinese zodiac cycle

Let's do a round of code golf! Last one was very popular!

The Chinese zodiac is a repeating cycle of 12 years, with each year being represented by an animal and an element that changes every 2 years.
The animals are: Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog and Pig.
The elements are: Wood, Fire, Earth, Metal and Water.
Your task is to write a function that receives a year, and returns the element and the animal that year represent.
Let's use the year 1924 as start point, in the Chinese zodiac the element was Wood and the animal was Rat.

Input

2023

Output

"Water Rabbit"

Note

Rules

  1. The signature of the contest entry MUST be:
    Class codeGolf.ChineseZodiac
    {
    
        ClassMethod Calendar(y As%Integer) As%String
        {
            q" "// Your solution here
        }
    
    }
    
  2. It is forbidden to modify class/signature, including but not limited to:
    • Adding inheritance
    • Setting default argument values
    • Adding class elements (Parameters, Methods, Includes, etc).
  3. It is forbidden to refer to non-system code from your entry. For example, this is not a valid entry:
    ClassMethod Build(f As %Integer)
    {
      W ##class(myPackage.myClass).test(a)
    }
    
  4. The use of $ZWPACK and $ZWBPACK is also discouraged.
  5. You can use this test case:
    Class codeGolf.unittests.ChineseZodiac Extends %UnitTest.TestCase
    {
    
        Method TestSolutions()
        {
            Do $$$AssertEquals(##class(codeGolf.ChineseZodiac).Calendar(1972), "Water Rat")
            Do $$$AssertEquals(##class(codeGolf.ChineseZodiac).Calendar(1934), "Wood Dog")
            Do $$$AssertEquals(##class(codeGolf.ChineseZodiac).Calendar(2023), "Water Rabbit")
            Do $$$AssertEquals(##class(codeGolf.ChineseZodiac).Calendar(2043), "Water Pig")
            Do $$$AssertEquals(##class(codeGolf.ChineseZodiac).Calendar(1961), "Metal Ox")
            Do $$$AssertEquals(##class(codeGolf.ChineseZodiac).Calendar(2002), "Water Horse")
            Do $$$AssertEquals(##class(codeGolf.ChineseZodiac).Calendar(2016), "Fire Monkey")
        }
    
    }
 

Not a valid solution

ClassMethod Calendar(y)
{
 q$we("馃尣馃敟馃實馃馃挧",y-4/2#5+1)_" "_$we("馃悁馃悅馃惎馃悋馃悏馃悕馃悗馃悙馃悞馃悡馃悤馃惙",y-4#12+1)
}

Comments

Robert Cemper 路 Oct 20, 2023

First bid:

  • 145 by code checker
  • 35 real code - the rest is text + packing + "cosmetics"
0
Robert Cemper  Oct 23, 2023 to Robert Cemper
ClassMethod Calendar(y As%Integer) As%String
{
 s a=$lfs("Monkey,Rooster,Dog,Pig,Rat,Ox,Tiger,Rabbit,Dragon,Snake,Horse,Goat,Metal,Water,Wood,Fire,Earth") q$li(a,y\2#5+13)_" "_$li(a,y#12+1)
}
0
Stuart Strickland 路 Oct 23, 2023

132

ClassMethod Calendar(As %Integer) As %String
{
$p("Metal7Water7Wood7Fire7Earth",7,y#10\2+1)_" "_$p("Monkey7Rooster7Dog7Pig7Rat7Ox7Tiger7Rabbit7Dragon7Snake7Horse7Goat",7,y#12+1)
}
 

0
Robert Cemper  Oct 23, 2023 to Stuart Strickland

great idea::  integers as separators!

just realized the separating blank broke the line in display.

0
Alena Krasinskiene 路 Oct 30, 2023
Class codeGolf.ChineseZodiac
{
    ClassMethod Calendar(y As%Integer) As%String
    {
        Set startYear = 1924Set animals = "Rat,Ox,Tiger,Rabbit,Dragon,Snake,Horse,Goat,Monkey,Rooster,Dog,Pig"Set elements = "Wood,Fire,Earth,Metal,Water"Set diff = y - startYear
        
        If diff < 0Quit"Invalid Year"Set animalIndex = (diff + 12) % 12Set elementIndex = (diff / 2) % 5Set animal = $List(animals, animalIndex + 1)
        Set element = $List(elements, elementIndex + 1)
        
        Quit element_" "_animal    
    }
}
0
Robert Cemper  Oct 30, 2023 to Alena Krasinskiene

the code fails already for start year 1924  <NULL VALUE>
and is in principle wrong   馃檨

0