5 kyu

Don't Eat the Last Cake!

590 of 1,780SagePtr
Description
Loading description...
Puzzles
  • Please sign in or sign up to leave a comment.
  • Awesome A.D. Avatar

    Rust translation ready for review.

  • MaxxZahar Avatar

    Could it be pointed in the task you are going to play yourself? As now it causes some dissatisfaction to return and write its behaviour in losing positions after failure of playing with yourself.

  • user9526662 Avatar

    do you guys think these are actually toast instead of cakes?

  • mariothedog Avatar

    This took me 5 days!!! I really want to say this is far too difficult to be 5 kyu but the solution is really simple once you get it. Thanks for the great kata :)

  • ozichukwu Avatar

    Should've just used functions or static functions since the methods is supplied with the needed parameters each time.

    Well, it's this way now.

  • JohanWiltink Avatar
  • SolitudeRain Avatar

    It's a reallllly cooool kata!

    Enjoy it very much, have so much fun~

    thanks~

  • DSchwettmann Avatar

    This is a good kata. You can solve it by basic logic, or you can go and turn it into an example for (basic) AI and write paths&decisiontrees.

    Which, given the shortest solution for this, complete overkill, but still fun in and on itself.

  • DorianGray2 Avatar

    There seems to be a bug in the example cases: I have the following output:

    You ate 1 cake, 1 cake still left ✘ Game over: stalemate

    Problem is that it doesn't follow it's own rule 4: "If one of the players has no valid moves (e.g one cake left and previous move was one cake), that player will lose his turn and be skipped. Then the other player will be forced to eat the last cake. This is the ONLY case of turn skipping."

    Problem is in example tests, line 38, where 0 must be added in the list of allowed moves in the case above.

  • galaxy233 Avatar

    This comment has been hidden.

  • Demki Avatar

    This comment has been hidden.

  • jakub__adamczyk Avatar

    Love it. This riddle made me think a bit.

  • pablosaya Avatar

    there is a problem with this kata, i just submitted a solution that doesnt make sense and it passed.

  • caomai Avatar

    There seem to be a problem with submitting this kata. Run Tests works fine but submit gives a null pointer exception stacktrace. Looks like its coming from the test framework.

    java.lang.NullPointerException at java.util.regex.Matcher.getTextLength(Matcher.java:1283) at java.util.regex.Matcher.reset(Matcher.java:309) at java.util.regex.Matcher.(Matcher.java:229) at java.util.regex.Pattern.matcher(Pattern.java:1093) at clojure.core$re_matcher.invokeStatic(core.clj:4674) at clojure.core$re_find.invokeStatic(core.clj:4716) at clojure.core$re_find.invoke(core.clj:4716) at codewars.runners.java$class_name.invokeStatic(java.clj:14) at codewars.runners.java$class_name.invoke(java.clj:11) at codewars.runners.java$source_code_file.invokeStatic(java.clj:35) at codewars.runners.java$source_code_file.invoke(java.clj:30) at clojure.core$map$fn__4785.invoke(core.clj:2646) at clojure.lang.LazySeq.sval(LazySeq.java:40) at clojure.lang.LazySeq.seq(LazySeq.java:49) at clojure.lang.RT.seq(RT.java:521) at clojure.lang.SeqIterator.hasNext(SeqIterator.java:38) at com.sun.tools.javac.api.ClientCodeWrapper.wrapJavaFileObjects(ClientCodeWrapper.java:140) at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:132) at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:107) at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:64) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:93) at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28) at codewars.runners.java$compile_and_load.invokeStatic(java.clj:83) at codewars.runners.java$compile_and_load.doInvoke(java.clj:79) at clojure.lang.RestFn.invoke(RestFn.java:460) at codewars.runners.java$fn__599.invokeStatic(java.clj:111) at codewars.runners.java$fn__599.invoke(java.clj:105) at clojure.lang.MultiFn.invoke(MultiFn.java:229) at codewars.runners$run.invokeStatic(runners.clj:22) at codewars.runners$run.invoke(runners.clj:17) at codewars.core$_main$fn__664.invoke(core.clj:40) at clojure.lang.AFn.call(AFn.java:18) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.lang.Thread.run(Thread.java:745)

  • feng619 Avatar

    I really enjoyed this kata ~ vary interesting, thank you for sharing!

  • Guile Avatar

    I think there may be a problem with the win conditions for the case where you start with 2 cakes. As first round the opponent can choose the same number as you, it would make sense to go first and eat 1 cake. The opponent can then also eat 1 cake as it is the first round and the move is open to him. When I put that in, it said my solution was incorrect but it worked if I assumed the stalemate condition still applied (even though it was round 1).

  • Deconstrained Avatar

    490 cakes? (Actual number encountered during random tests)

    We'd both die of overeating.

    This is definitely a challenging problem that requires above-average skill in algorithm design, if the solution is required to scale like this and still run all tests in under 6 seconds.

    I thought I had a decent solution that scaled a lot better than O(2^N) (my first working solution before improvements) until it hit random testing and all hell broke loose.

  • TheJamesD Avatar

    Java: My solution works fine for the first test cases but when I submit it it apears to add the numbers I return rather than subtract them.

  • thevjm Avatar

    This comment has been hidden.

  • lihongchun2007 Avatar

    Very interesting. I love this kata.

  • lupan123 Avatar

    这道题第一次让我无从下手,看上去并不难,但是却找不到突破口。。。good kata!

  • Azeezah Avatar

    Please clarify what is meant by rule 3 "doesn't effect the first move, only to subsequent."

    If I eat 2 cakes on the first move, can my opponent eat 2 on the second move?

  • sarahcodez Avatar

    That took me a long time to figure out, but was a fun puzzle and helped me see the value of pattern recognition (and starting slow and simple when abstract mathematical problem-solving isn't your forte). I slowly plotted out my system, going through each number of cakes from 1 to 14, trying out all the possibilities for each move, until I noticed clear patterns. Then when I tried out what I had come up with it worked :D

  • fabulon Avatar

    According the rule number 3: "This doesn't effect the first move, only to subsequent." If I am the starter and eat 3 cake at first the opponent can eat 3 cakes too. I could not find a nice solution therefore I checked a solution and run some test for example: there are 12 cakes: Using [3,3,0,1][cakes % 4] rules for moving the starter is a winner but if i keep the rule the opponet should be a winner

    1. I eat 3 cakes left 9
    2. Opponent eats 3 cake left 6 (it is the first move of the opponent so according the rule it is OK)
    3. From that point i cannot be a winner. If I eat 1 cake left 5 the opponent can eat 3 cakes left 2 I can eat 1 or 2. So I eat just 1 left 1. The opponent cannot eat 1 cake (using the rule number 3) therefore i have to eat again so I lost a game.

    Maybe I misunderstand the rules or there is a mistake in my workflow. Could you explain what is wrong ? Thanks

  • kars7e Avatar

    This comment has been hidden.

  • BambooCZ Avatar

    LOL. I've bruteforced all game possibilities. At least I am caching previously computed solutions. Very nice kata!

  • JindrichGorner Avatar

    Is there some limit in terms of a minimum number of cakes on the table?

  • GaurangTandon Avatar

    This comment has been hidden.

  • Nekon Avatar

    Really fun Kata!

  • sebastian-nowakowski Avatar

    Still struggling with this one, won't give up easily. Great food for thoughts!

  • KIRA_13 Avatar

    May be I got the description wrong. Because according to the output:

    5 cakes on the table. You decided to move last I ate 3 cakes , 2 cakes still left You ate 1 cake , 1 cake still left

    One poisoned cake still left for the opponent. I should win. But it is written - GameOver. Where is the mistake?

  • madmaurice Avatar

    This kata can only be solved by luck... i mean as soon as there are 2 cakes you either lose or it's a stalemate and that's god knows why your fault. And you can't fully avoid the 2 because your opponent chooses his number of cakes by random...

    Fix your game, SagePtr.

  • Duc Nguyen Hong Avatar

    I don't quite understand the rule "You cannot copy your opponent's previous move, likewise they cannot copy yours. If your opponent takes one cake, next move you can only choose between two or three. If you take three cakes, your opponent can only choose one or two. This doesn't effect the first move, only to subsequent." for example, i move first with 2, then can enemy move 2? b/c "This doesn't effect the first move, only to subsequent" So you cannot copy after first move of anyone or first move of both?

  • feenr Avatar

    Is a stalemate supposed to count as a failure? According to the instructions your turn is skipped, and the opponent loses. Consider the simple example of a game starting with 5 cakes

    I go first 1 > 3 > 2 - I lose 2 > 1 > 2 - I lose 3 > 1 > 0 > 1 - Stalemate in my favor You go first 1 > 3 > 1 - I win (So you would not choose this) 2 > 1 > 2 - I win (So you would not choose this) 3 > 1 > 0 > 1 - Stalemate in your favor

    In this sceenario I can't win 100% of the time. Even if I choose who goes first my best case scenario is a stalemate in my favor

  • gottheguts Avatar

    Loved this one!

  • tianshuo Avatar

    You should definitely check that the player can play with itself... Or it will end up with answers such as mine...

  • apendua Avatar

    This comment has been hidden.

  • Odomontois Avatar

    Python tests contain 1 as starting count. And failed due to lose. Is this reasonable?

  • Pinwheeler Avatar

    I've solved this kata, ending in victory but it's not passing me due to a stalemate exception. Any info on this?

  • BillOTei Avatar

    I feel sorry for asking but I really can't get my head around this one. And even dumber as it is apparently an easy one... I thought about multiple approaches:

    • starting and playing always the same nb in order to "guess" the result
    • trying to end up in the skipping position for me My feeling is that I should always start as that allows me to predict the opponent play except in case of really low start cakes amount. I feel that the key lies of course in the last steps of the game, could I have some hints pls? Apologies if I gave too much information
  • computerguy103 Avatar

    For an added challenge, write your solution so that you can still play the game even if your opponent gets to decide who goes first (and thus, can beat you).

    I.e. your Player should still be able to follow all of the rules for legal plays, even when it is going to lose.

  • Arheus Avatar

    Now THAT was a really fun kata!

  • davidwlewis Avatar

    Nice when a problem simplifies down to ~nothing. =D

  • user9935269 Avatar

    I agree with the other comments regarding this kata's simplicity, yet it will soon be rated around 4 kyu when it leaves beta. The programming knowledge required to solve this does not extend beyond if/else statements and basic OOP. I feel this is around 6 kyu, but are others raising their rating due to the pattern recognition involved? What is the community's approach to mentally challenging yet programmatically simple kata?

  • user6509908 Avatar

    My solution passes every test case, repeatedly. When I submit, the gameplay shows that I win. However, the "easy test" says I ate the last cookie. Mathmatically the gameplay shows otherwise. Disparity between test case and submit test?

  • mortonfox Avatar

    This is a very interesting problem because it looks harder than it actually is. On paper, I worked through a bunch of games with small numbers of cakes until I saw how bigger games can be reduced to smaller games. That led to a simple strategy.

  • SteveRuble Avatar

    Thanks, this was really fun. The extra rule (no repeating opponent's moves) made this a lot more challenging than the version of this problem I've come across before. I had to build a solver for the whole game (which turned out to be too slow to pass the submission test cases) and use that to find a pattern I could use to build a simple solution. Great kata.

  • jacobb Avatar

    This was a fun kata. It has a very simple solution that takes a bit of exploration to come to.