Hacker News new | past | comments | ask | show | jobs | submit login
Cheat at Rock-Paper-Scissors-Lizard-Spock (codegolf.stackexchange.com)
85 points by HeinZawHtet on Dec 15, 2019 | hide | past | favorite | 11 comments



    &/"ki"?
It's incredible how seven little characters can pack so much power, but this level of cuteness also demands some explanation as to why it's correct (given the input domain).

The first thing we might observe is that every input has two possible outputs, and the output domain is the same as the input domain. That means that if we select our outputs carefully, we may be able to find a property of the input that differentiates them.

    q){x!x in/:\:k}distinct raze k:string`Rock`Spock`Paper`Lizard`Scissors
    R| 10000b
    o| 11001b
    c| 11001b
    k| 11000b
    S| 01001b
    p| 01100b
    P| 00100b
    a| 00110b
    e| 00100b
    r| 00111b
    L| 00010b
    i| 00011b
    z| 00010b
    d| 00010b
    s| 00001b
We can see several options: "k" gets us Paper, "i" gets us Rock, "a" gets us Scissors, "p" gets us "Lizard". There are a few red herrings though: "S" doesn't get us any single output (Scissors and Spock don't have a shared winning response). The pairs I can see that differentiate all states are "ki", "ka", "ip". Let us use "ki":

    Roc<k>    | Spock   <Paper>   
    Spoc<k>   |<Paper>   Lizard  
    Paper     | Lizard   Scissors
    L<i>zard  | Scissors<Rock>    
    Sc<i>ssors|<Rock>    Spock   
The only remaining item we can choose (Lizard or Scissors), and if we choose the latter then our game can play traditional Rock-Paper-Scissors as well!

    `Paper`Rock`Scissors@&/"ki"?
Of course if you're not using q, you could use a different mapping function. Many of the responses looked at the lengths or a critical bit of a hash (like md5), all with the aims of producing this kind of compression function, and this isn't uncommon in high performance systems, but if you're going to depend on such a function, you should explain carefully how you came up with these magic characters so if someone wants to extend the game in the future they know how to reconstruct your function.


If nothing else, that was a very satisfying read because it shows the incredible diversity of programming languages available today.


I am still wondering how the two Perl ones work with two missing words in the code.


Since each hand beats two other hands, you only need three possible outputs to beat any opposing hand.


The conclusion does not follow...

(the missing condition is that every hand is beatable)


There are 5 possible hands

Each hand beats 2 distinct hands

With 3 distinct hands you can cover up to 6 distinct hands which is a superset of all possible hands

3 * 2 > 5


And yet...

  A --> D | E
  B --> D | E
  C --> D | E
  D --> C | E
  E --> D | C
QED


Um, this isn't a general math problem; We're not solving for all possible sets. We're solving this problem in particular:

  A --> B | C
  B --> C | D
  C --> D | E
  D --> E | A
  E --> A | B


The question is not clear. You don’t need to output the string “Rock or Spock”, you need to output either the string “Rock” or the string “Spock”.


Rock: 4-letters (Mod4 == 0)

Paper: 5-letters (Mod4 == 1)

Spock: 5-letters (Mod4 == 1)

Lizard: 6-letters (Mod 4 == 2)

Scissors: 8-letters (Mod 4 == 0)

----------

There are no 3-letter or 7-letter words (aka: Blah % 4 != 3 given the inputs). So they just left the 3rd index (4th word) empty.


Apparently you don't actually need to print "Rock or Spock" although it sounded like that's a requirement.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: