15
\$\begingroup\$

One of the reasons I've always loved Pokemon is because for such a simple-seeming game, it has so many layers of complexity. Let's consider the move Hidden Power. In game, the type and power (at least before Generation VI) of Hidden Power is different for every Pokemon that uses it! That's pretty cool, right? Now, would you be surprised if I told you that the type and power of Hidden Power isn't generated randomly?

In all Pokemon games, all Pokemon (not just ones in your party, ALL POKEMON) have six internally-stored integers (one for the HP stat, one for the attack stat, one for the defense stat, one for the special attack stat, one for the special defense stat and one for the speed stat) called their individual values, or IVs. These values range between 0 and 31, and they essentially are one of a few factors that influence a Pokemon's overall stats. HOWEVER, they also determine the type and power of Hidden Power!

In Generation III to V (the generations whose algorithm we'll be implementing), the type of Hidden Power is determined by the following formula (note the floor brackets, that means you need to round down the result):

where a, b, c, d, e and f are the least significant bits of the HP, Attack, Defense, Speed, Sp. Attack, and Sp. Defense IVs respectively. (Least significant bit is IV mod 2.) The number produced here then can be converted to the actual type using this chart:

0 Fighting
1 Flying
2 Poison
3 Ground
4 Rock
5 Bug
6 Ghost
7 Steel
8 Fire
9 Water
10 Grass
11 Electric
12 Psychic
13 Ice
14 Dragon
15 Dark

For power, a similar formula is used:

Here, however, u, v, w, x, y and z represent the second least significant bit of the HP, Attack, Defense, Speed, Sp. Attack and Sp. Defense IVs (in that order again). (Second least significant bit is more complicated then least significant bit. If IV mod 4 is 2 or 3, then the bit is 1, otherwise it is 0. If your language has some sort of built-in or at least a more clever way to do this, you should probably use it.)


So, as you probably figured out already, the challenge here is to write a program that takes in six integers separated by spaces via STDIN that represent the HP, Attack, Defense, Speed, Sp. Attack and Sp. Defense IVs of a Pokemon (in that order) and output the type and power of that Pokemon's Hidden Power.

Sample input:

30 31 31 31 30 31

Sample output:

Grass 70

Sample input:

16 18 25 13 30 22

Sample output:

Poison 61

This is code-golf, so shortest code wins. Good luck!

(And before people ask, I used the Generation V algorithm here because Generation VI gets rid of the power randomization and makes it always 60. Not only do I think this is incredibly lame, I think it makes the challenge a LOT LESS INTERESTING. So for the purposes of the challenge, we're running a Gen V game.)

\$\endgroup\$
6
  • \$\begingroup\$ Sorry, I left out the sample input and output by mistake. Here you go. \$\endgroup\$ Commented Sep 28, 2015 at 14:16
  • \$\begingroup\$ Some additional test samples would be nice :). \$\endgroup\$
    – Blackhole
    Commented Sep 28, 2015 at 14:23
  • \$\begingroup\$ Yes, my bad. Fixed. \$\endgroup\$ Commented Sep 28, 2015 at 14:53
  • 3
    \$\begingroup\$ I'd like to see a challenge about MissingNo. \$\endgroup\$
    – mbomb007
    Commented Sep 28, 2015 at 15:07
  • \$\begingroup\$ That might happen next ;) \$\endgroup\$ Commented Sep 28, 2015 at 15:07

5 Answers 5

5
\$\begingroup\$

Ruby, 210

a=$*.map.with_index{|a,i|[a.to_i%2<<i,a.to_i[1]<<i]}.transpose.map{|a|a.inject &:+}
$><<"#{%w(Fighting
Flying
Poison
Ground
Rock
Bug
Ghost
Steel
Fire
Water
Grass
Electric
Psychic
Ice
Dragon
Dark)[a[0]*15/63]} #{a[1]*40/63+30}"

First time golfing, so I guess this is pretty obvious solution.

\$\endgroup\$
3
\$\begingroup\$

Pyth, 110 bytes

J+dGA.b/*iN2CY63Cm_+0jd2_Q"("r@cs@LJjC"!�W��Zm�����A�zB0i��ȏ\"���?wC�ǀ�-#ך
?ЫܦO@�J/m���#"26)G3+30H

This contains unprintable chars. So here's a hexdump:

00000000: 4a 2b 64 47 41 2e 62 2f 2a 69 4e 32 43 59 36 33  J+dGA.b/*iN2CY63
00000010: 43 6d 5f 2b 30 6a 64 32 5f 51 22 0f 28 22 72 40  Cm_+0jd2_Q".("r@
00000020: 63 73 40 4c 4a 6a 43 22 10 21 de 57 ad c8 5a 1c  cs@LJjC".!.W..Z.
00000030: 10 6d e0 d6 12 f6 80 bc 41 85 7a 42 30 69 ae 80  .m......A.zB0i..
00000040: c8 8f 5c 22 a0 84 ab 3f 77 43 01 ca c7 80 d0 1d  ..\"...?wC......
00000050: 2d 23 d7 9a 0a 3f d0 ab dc a6 4f 40 b9 4a 2f 6d  -#[email protected]/m
00000060: d2 ca c6 23 22 32 36 29 47 33 2b 33 30 48        ...#"26)G3+30H

You can also download the file pokemon.pyth and run it with python3 pyth.py pokemon.pyth

The input 30, 31, 31, 31, 30, 31 prints

Grass
70

Explanation:

J+dGA.b/*iN2CY63Cm_+0jd2_Q".("
J+dG                            store the string " abc...xyz" in J
                 m      _Q      map each number d in reverse(input list) to:
                     jd2          convert d to base 2
                   +0             add a zero (list must have >= 2 items)
                  _               reverse the list
                C               zip
                          ".("  string with the ascii values 15 and 40
     .b                         map each N of ^^ and Y of ^ to:
         iN2                       convert N from base 2 to base 10
        *   CY                     multiply with the ascii value of Y
       /      63                   and divide by 63
    A                           G, H = ^

r@cs@LJjC"longstring"26)G3+30H
        C"longstring"           interpret the string as bytes and convert 
                                from base 256 to base 10
       j             26         convert to base 26
   s@LJ                         lookup their value in J and create a string
                                this gives "fighting flying ... dark"
  c                    )        split by spaces
 @                      G       take the Gth element
r                        3      make the first letter upper-case and print
                          +30H  print 30 + H
\$\endgroup\$
1
  • 3
    \$\begingroup\$ I find it rather amusing that your file extension for pyth files is more verbose than python's file extension :^) \$\endgroup\$ Commented Sep 28, 2015 at 17:06
3
\$\begingroup\$

CJam, 140 115 bytes

q~]W%_1f&2bF*63/"GMÿD>BÙl½}YÛöí6P¶;óKs¯¿/·dǯã®Å[YÑÌÞ%HJ9¹G4Àv"256b25b'af+'j/=(euooSo2f/1f&2b40*63/30+

Note that the code contains unprintable characters.

Try it online in the CJam interpreter: Chrome | Firefox

\$\endgroup\$
1
  • \$\begingroup\$ Wow, that was fast. Nice job! \$\endgroup\$ Commented Sep 28, 2015 at 14:54
2
\$\begingroup\$

Javascript (ES6), 251 bytes

Kinda long, at least for now. The list of types and the complex maths take up about the same amount of space. I'm looking for ways to shorten either/both of them.

x=>([a,b,c,d,e,f]=x.split` `,`Fighting
Flying
Poison
Ground
Rock
Bug
Ghost
Steel
Fire
Water
Grass
Electric
Psychic
Ice
Dragon
Dark`.split`
`[(a%2+b%2*2+c%2*4+d%2*8+e%2*16+f%2*32)*5/21|0]+' '+((a/2%2+(b&2)+(c&2)*2+(d&2)*4+(e&2)*8+(f&2)*16)*40/63+30|0))

As usual, suggestions welcome!

\$\endgroup\$
1
\$\begingroup\$

Javascript (ES6), 203 bytes

f=(...l)=>(q=(b,m)=>~~(l.reduce((p,c,x)=>p+(!!(c&b)<<x),0)*m/63),'Fighting0Flying0Poison0Ground0Rock0Bug0Ghost0Steel0Fire0Water0Grass0Electric0Psychic0Ice0Dragon0Dark'.split(0)[q(1,15)]+' '+(q(2,40)+30))

Example runs:

f(30,31,31,31,30,31)
> "Grass 70"

f(16,18,25,13,30,22)
> "Poison 61"
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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