9
\$\begingroup\$

2015 was the year of the goat! In this challenge, you will figure out which animal represents the year. To avoid making this challenge to complex you won't need to calculate Chinese new year.

Because they are only 12 animals in the Zodiac, your code will need to be as short as possible.

Examples

Your program must get the system year, the first line is just for reference

2015
Goat

1924
Rat

1923
Pig

2008
Rat

Specification

The order of the Zodiac is:

Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig

Rat starts at 2008, Pig is 2007, Ox at 2009, etc. at the end this loops back around.

Output must be correctly capitalized.

\$\endgroup\$
  • \$\begingroup\$ Is anything before 2008 is assumed to be Rat? \$\endgroup\$ – Zach Gates Jan 1 '16 at 1:36
  • \$\begingroup\$ @ZachGates no, that's just an example. I'll update \$\endgroup\$ – Downgoat Jan 1 '16 at 1:36
  • \$\begingroup\$ Can it use the year in the user's timezone? \$\endgroup\$ – LegionMammal978 Jan 1 '16 at 1:44
  • 1
    \$\begingroup\$ Related. \$\endgroup\$ – Martin Ender Jan 1 '16 at 2:55
  • 1
    \$\begingroup\$ I am confused by the system year requirement. So there is no input? So in 2015, it would have printed goat, and now, it should print Monkey? \$\endgroup\$ – TanMath Jan 1 '16 at 4:49

15 Answers 15

5
\$\begingroup\$

Pyth, 54 bytes

@rR3c."ayÖÚ©*ÐPk<`eüµÜxþ!¬Ì ¾½hÑðÙBwp"\c.d3

Try it online: Demonstration

You can replace the last 3 chars .d3 with any year number to test the other Zodiac animals.

Explanation:

     ."ayö..."        packed string, decrypts to "monkeycroostercdogcpig..."
    c         \c      split this string at "c"s
 rR3                  make the first letter of each animal uppercase
                .d3   current year
@                     take the correct animal of the list and print
\$\endgroup\$
6
\$\begingroup\$

Pyth - 65 bytes

@c." y=CÅQ¯@îR
°©ÀáëªfÞ­#[õNqÀN8Ô'¼ô,n7˶~fB").d3

Try it online here.

@                     Modular indexing
 c           )        Split string, use default of spaces
  ."ajs&^asd"         Packed string to compress space separated zodicas
 .d3                  Current year
\$\endgroup\$
  • \$\begingroup\$ Can you add an explanation? \$\endgroup\$ – TanMath Jan 1 '16 at 4:43
  • \$\begingroup\$ @TanMath its really simple, but sure \$\endgroup\$ – Maltysen Jan 1 '16 at 6:35
  • \$\begingroup\$ You can save at least 3 bytes by narrowing down the character space. demo Also, you might want to post the unprintable code as a hexdump and include a printable version. \$\endgroup\$ – PurkkaKoodari Jan 1 '16 at 16:22
4
\$\begingroup\$

JavaScript ES6, 106 105 104 103 bytes

Saved 1 byte thanks to Maltysen, 1 byte to insertusernamehere, and 1 byte to edc65!

x=>"RatOxTigerRabbitDragonSnakeHorseGoatMonkeyRoosterDogPig".match(/.[a-z]+/g)[new Date().getYear()%12]

Pretty self explanatory.

\$\endgroup\$
  • \$\begingroup\$ I feel like that string could use some btoa and atob. (You can also change the byte encoding, if that's allowed). \$\endgroup\$ – Mama Fun Roll Jan 1 '16 at 2:43
  • \$\begingroup\$ @ןnɟuɐɯɹɐןoɯ btoas result looks to be 1.5x or more the length of the hardcoded string. \$\endgroup\$ – Conor O'Brien Jan 1 '16 at 2:44
  • \$\begingroup\$ No, use atob to encode, and btoa to decode. \$\endgroup\$ – Mama Fun Roll Jan 1 '16 at 2:48
  • 5
    \$\begingroup\$ You can save 1 byte: Remove the 0 as separator and use this regex to split the string: /(?=[A-Z])/. Happy New Year. :) \$\endgroup\$ – insertusernamehere Jan 1 '16 at 9:49
  • 1
    \$\begingroup\$ match(/.[a-z]+/g) instead of split (added value: split with capture is not ES6, is a FireFox only feature)(and 1 byte shorter) \$\endgroup\$ – edc65 Jan 2 '16 at 15:51
3
\$\begingroup\$

CJam, 82 80 75 bytes

This is was a direct port of my Milky Way answer.

"Monkey Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat"S/et0==

Saved 7 bytes thanks to NinjaBearMonkey.

\$\endgroup\$
  • \$\begingroup\$ You can use S as a variable for space. \$\endgroup\$ – NinjaBearMonkey Jan 1 '16 at 2:06
  • 1
    \$\begingroup\$ Even shorter: "Monkey Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat"S/et0== \$\endgroup\$ – NinjaBearMonkey Jan 1 '16 at 2:13
3
\$\begingroup\$

Japt, 72 65 bytes

`Mkey RooÐP Dog Pig R Ox Tig Rabb Dg Snake HÆ  Go`¸gÐ i %C

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Umm... this is pretty good, but there's definitely some unprintables in there. Could you give us a link? \$\endgroup\$ – ETHproductions Jan 1 '16 at 20:05
  • \$\begingroup\$ @Eth eh, didn't account that unprintable are stripped. Gonna restore it :D \$\endgroup\$ – nicael Jan 1 '16 at 20:06
  • \$\begingroup\$ BTW, instead of $[$Ð i -4 %12], you can use g(Ð i -4 %C. \$\endgroup\$ – ETHproductions Jan 1 '16 at 20:07
  • \$\begingroup\$ @Eth ok, and i'll dispose of -4 also, by changing the element order. \$\endgroup\$ – nicael Jan 1 '16 at 20:07
  • \$\begingroup\$ @Eth Wait, your fix doesn't appear to work, outputs NaN... \$\endgroup\$ – nicael Jan 1 '16 at 20:09
1
\$\begingroup\$

MATL, 85 bytes

'Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey'YbZ'10H$XOU12X\)

Uses release 6.0.0 of the language/compiler. Works in Octave.

Example

>> matl
 > 'Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey'YbZ'10H$XOU12X\)
 >
Monkey

Explanation

Note that the initial string is cyclically displaced by 3 to avoid a subtraction.

'Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey'
Yb                    % split at spaces
Z'                    % number representing current date and time
10H$XO                % get year as a string
U                     % convert to number
12X\                  % modulo 12, with 0 converted to 12
)                     % index (and implicitly print)
\$\endgroup\$
1
\$\begingroup\$

Milky Way 1.6.1, 101 96 bytes

M" "\=A12n"Goat Horse Snake Dragon Rabbit Tiger Ox Rat Pig Dog Rooster Monkey"" "\;>=<&{~1-;^};!

Saved 5 bytes thanks to a reminder by Luis Mendo.


Explanation

The code modulos the system year by 12 and outputs the proper animal.


Usage

./mw <path-to-code>
\$\endgroup\$
  • 1
    \$\begingroup\$ Maybe you can avoid 2008- by rearranging the string as in my answer? \$\endgroup\$ – Luis Mendo Jan 1 '16 at 3:11
  • \$\begingroup\$ Oh, I think you already do that in your CJam answer \$\endgroup\$ – Luis Mendo Jan 1 '16 at 3:27
  • \$\begingroup\$ You're right! I can use the string from my CJam answer in reverse. Thanks for reminding me @LuisMendo \$\endgroup\$ – Zach Gates Jan 1 '16 at 3:33
1
\$\begingroup\$

CJam, 69 bytes

"SKiN<lEz=2)aFBY8H[$h-0|4=K`T2Cp%-a)o6"132b26b'`f+'`/et0==(eu\

Contains a bunch of unprintables. Try it here.

\$\endgroup\$
  • 4
    \$\begingroup\$ You can't take a year as input, it has to use the current system time. \$\endgroup\$ – Maltysen Jan 1 '16 at 6:39
  • \$\begingroup\$ Ah, fixed. @Maltysen \$\endgroup\$ – Lynn Jan 1 '16 at 13:09
1
\$\begingroup\$

Python 3, 126 116 112 bytes

Edit: I'm keeping the other answer because it's cool, but this is shorter.

from time import*
"Monkey Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Goat".split()[localtime().tm_year%12]

If trailing whitespace is ok, then it's 126 bytes. If not, it's 134 bytes after adding .strip() to the end of the code.

from datetime import*
"MRDPROTRDSHGoooiaxiarnoonoggt gbaaraks    ebgkstet    rioee ye     tn    r"[datetime.now().year%12::12]
\$\endgroup\$
0
\$\begingroup\$

R, 119 bytes

scan(t="Monkey Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat",w="")[(format(Sys.Date(),"%Y"):1)[1]%%12+1]

format(Sys.Date(),"%Y") returns the year as a character string, (format(Sys.Date(),"%Y"):1)[1] is the shortest way i could think of to coerce it into an integer so that we can apply %% to it (the sequence generator : indeed coerces automatically to integer). +1 because R indices are 1-based and not 0-based.

\$\endgroup\$
0
\$\begingroup\$

PHP >= 5.6, 84 bytes

Very straight forward:

<?=[Monkey,Rooster,Dog,Pig,Rat,Ox,Tiger,Rabbit,Dragon,Snake,Horse,Goat][date(Y)%12];
\$\endgroup\$
0
\$\begingroup\$

Python 129 126 bytes

EDIT: I just learnt s.split(' ')=s.split()

Although this isn't the shortest method, here is an alternative answer to @Sherlock9's one:

from datetime import*
print 'Monkey Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat'.split()[date.today().year%12]

Try it here

\$\endgroup\$
  • \$\begingroup\$ Wow. I just edited my answer and that was pretty close to what I edited it to. I'm going to try something with time next. \$\endgroup\$ – Sherlock9 Jan 2 '16 at 5:42
0
\$\begingroup\$

PowerShell, 92 bytes

(-split"Monkey Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat")[(date).Year%12]

Similar in spirit to other answers, added here only for sake of cataloging. This takes the string "Monkey ... Goat" and -splits it on spaces to create an array, which is cheaper than specifying the elements individually with ("Monkey","Rooster",...). We index into that array with the current year mod 12, which outputs the result. Nothing too fancy.

\$\endgroup\$
0
\$\begingroup\$

C# 159

using System;class P{static void Main(){Console.Write("Monkey;Rooster;Dog;Pig;Rat;Ox;Tiger;Rabbit;Dragon;Snake;Horse;Goat".Split(';')[DateTime.Now.Year%12]);}}

Readable / ungolfed version

using System;
class Program
{
    static void Main(string[] args)
    {
        Console.Write("Monkey;Rooster;Dog;Pig;Rat;Ox;Tiger;Rabbit;Dragon;Snake;Horse;Goat".Split(';')[DateTime.Now.Year % 12]);
    }
}
\$\endgroup\$
0
\$\begingroup\$

J, 78 bytes

(>;:'Monkey Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat'){~12|

Usage:

    (>;:'Monkey Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat'){~12| 2015
 Goat

Explanation:

;:      separates the string into boxed array according to the spaces
>       opens the boxed array so that it becomes a normal array
x {~ y  is choose the y-th element from x.
        the ~ reverses the argument, so x { y is choose the x-th element from y.
12|     is the residue when the year is divided by 12
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.