2 kyu

Hard Sudoku Solver

692 of 900f.rodrigues
Description
Loading description...
Games
Algorithms
Game Solvers
Puzzles
  • Please sign in or sign up to leave a comment.
  • seleemdz Avatar

    Nice kata, thanks!

  • PinkMappy Avatar

    When attempting the sample test, my solution succeeds. When pressing the attempt button, my solution succeeds for all of the 9 tests, but I still get a timed out error. There is also a question mark next to the amount of tests I failed. Has this something to do with the way I raise an exception/just inefficient code on my part? Thanks for the help.

  • Yoshyn Avatar

    Hello,

    I've got two issues. Timeout and random tests that fail. Timeout is the fun part. Random tests is the strange part and i need some help. If I believe the output of the attempt :

    Input:
    [3, 0, 0, 0, 0, 6, 0, 0, 0]
    [4, 1, 0, 0, 0, 0, 7, 8, 5]
    [7, 0, 0, 0, 0, 0, 0, 0, 0]
    [9, 8, 0, 5, 4, 0, 0, 0, 0]
    [0, 0, 0, 0, 0, 0, 0, 0, 0]
    [0, 0, 0, 0, 8, 3, 0, 5, 4]
    [0, 0, 0, 0, 0, 0, 0, 0, 2]
    [8, 5, 1, 0, 0, 0, 0, 3, 7]
    [0, 0, 0, 8, 0, 0, 0, 0, 9]
    Value is not what was expected
    

    My solution is wrong but I have no idea of the expected.

    My solver found this solution :

    [
      [ 3,9,8,7,5,6,4,2,1 ]
      [ 4,1,6,3,2,9,7,8,5 ]
      [ 7,2,5,4,1,8,9,6,3 ]
      [ 9,8,3,5,4,2,1,7,6 ]
      [ 5,4,2,6,7,1,3,9,8 ]
      [ 1,6,7,9,8,3,2,5,4 ]
      [ 6,7,9,1,3,5,8,4,2 ]
      [ 8,5,1,2,9,4,6,3,7 ]
      [ 2,3,4,8,6,7,5,1,9 ]
    ]
    

    What should be the expected output ?

  • yurec_26 Avatar

    i cant even describe how i struggled during this f#kin kata, it is almaust destroed my life. few mounths passed i did it, it was terrible but i like this pain. thank you hel yeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  • AlexShein Avatar

    I really liked solving this kata, but in the end of the day it was super frustrating that time after time optimizations did not result into all tests passing within the time limit! I'd suggest to add a hint to the description about the desired solving speed in the description, so that one may estimate how far is he or she from "getting there".

    For me, the solution that passed solved the basic test case in ±30-50ms.

    Small hint regarding optimization in Python: try using cProfile to find out which part of the program takes the most time to execute.

  • JuliusGS Avatar

    I seem to have a problem where the program asks me to mark a grid that seems valid as invalid, like this one:

    Input:
    [4, 8, 3, 5, 7, 1, 9, 2, 6]
    [2, 7, 9, 4, 8, 6, 3, 5, 1]
    [5, 1, 6, 9, 2, 3, 8, 7, 4]
    [1, 9, 4, 3, 6, 7, 5, 8, 2]
    [3, 6, 7, 2, 5, 8, 1, 4, 9]
    [8, 2, 5, 1, 9, 4, 7, 6, 3]
    [6, 5, 1, 7, 4, 9, 2, 3, 8]
    [9, 4, 2, 8, 3, 5, 6, 1, 7]
    [7, 3, 8, 6, 1, 2, 4, 9, 5]
    --------------------------
    [[4, 8, 3, 5, 7, 1, 9, 2, 6], [2, 7, 9, 4, 8, 6, 3, 5, 1], [5, 1, 6, 9, 2, 3, 8, 7, 4], [1, 9, 4, 3, 6, 7, 5, 8, 2], [3, 6, 7, 2, 5, 8, 1, 4, 9], [8, 2, 5, 1, 9, 4, 7, 6, 3], [6, 5, 1, 7, 4, 9, 2, 3, 8], [9, 4, 2, 8, 3, 5, 6, 1, 7], [7, 3, 8, 6, 1, 2, 4, 9, 5]]
    [[4, 8, 3, 5, 7, 1, 9, 2, 6], [2, 7, 9, 4, 8, 6, 3, 5, 1], [5, 1, 6, 9, 2, 3, 8, 7, 4], [1, 9, 4, 3, 6, 7, 5, 8, 2], [3, 6, 7, 2, 5, 8, 1, 4, 9], [8, 2, 5, 1, 9, 4, 7, 6, 3], [6, 5, 1, 7, 4, 9, 2, 3, 8], [9, 4, 2, 8, 3, 5, 6, 1, 7], [7, 3, 8, 6, 1, 2, 4, 9, 5]]
    there are 1 solutions
    

    I am sure this is simple oversight on my part, as many people have solved this in Python and I don't think that everyone has made a mistake but me. Still, I can't find a reason for why this grid should be invalid. If someone could enlighten me I would be very grateful as I have been working on my solution for a week and would love to finally submit it :).

  • Erhu Avatar

    It could have been a good test but proving sudoku has single solution requires backtracking, which make the job time out, mainly because of the 200 random tests.

  • Vulpes_Inculta Avatar

    According to your tests, this grid is invalid:

    [1, 0, 0, 0, 0, 0, 4, 7, 0] [0, 8, 0, 2, 0, 4, 0, 1, 0] [0, 6, 5, 0, 0, 0, 0, 0, 9] [0, 0, 0, 0, 0, 1, 0, 9, 0] [0, 0, 0, 3, 5, 0, 0, 0, 0] [0, 7, 6, 0, 0, 9, 0, 0, 8] [2, 0, 0, 5, 0, 0, 7, 4, 0] [0, 0, 0, 0, 1, 8, 0, 0, 0] [0, 3, 0, 7, 0, 0, 0, 0, 0]

    Edit: OK, scratch that, apparently the problem was caused by another grid, not this one. I updated my solution to deal with those scenarios. Is there a way to delete the previous solution if it turned out to be not 100% correct?

  • Gretgor Avatar

    In the "single solutions" list, there is the following problem:

    Input [6, 0, 0, 0, 0, 0, 0, 0, 2] [0, 0, 3, 6, 0, 1, 7, 0, 0] [0, 7, 0, 0, 4, 0, 0, 1, 0] [0, 5, 0, 9, 0, 4, 0, 3, 0] [0, 0, 9, 0, 0, 0, 1, 0, 0] [0, 6, 0, 7, 0, 8, 0, 2, 0] [0, 3, 0, 0, 6, 0, 0, 5, 0] [0, 0, 5, 3, 0, 9, 4, 0, 0] [7, 0, 0, 0, 0, 0, 0, 0, 3]

    to which my program provides the following output:

    Output [6, 1, 8, 5, 9, 7, 3, 4, 2] [4, 9, 3, 6, 2, 1, 7, 8, 5] [5, 7, 2, 8, 4, 3, 9, 1, 6] [2, 5, 7, 9, 1, 4, 6, 3, 8] [3, 8, 9, 2, 5, 6, 1, 7, 4] [1, 6, 4, 7, 3, 8, 5, 2, 9] [9, 3, 1, 4, 6, 2, 8, 5, 7] [8, 2, 5, 3, 7, 9, 4, 6, 1] [7, 4, 6, 1, 8, 5, 2, 9, 3]

    And yet... it says the solution is invalid. What is going on?

  • yangchenyun Avatar

    Potential an error in the checker script:

    The following input generates a false error message "invalid should raise error": I have (possibly) replicated how to the two solutions are found on different paths, but they are the same. Below is my results without early exit; with givens == 27, the question cannot have multiple solutions.

    givens =  29
    Puzzle:
    [9, 0, 0, 5, 0, 0, 0, 0, 4]
    [0, 0, 7, 0, 4, 0, 3, 0, 0]
    [6, 0, 0, 0, 0, 3, 0, 7, 5]
    [0, 4, 0, 0, 2, 0, 7, 0, 0]
    [7, 0, 2, 0, 0, 0, 0, 0, 1]
    [0, 0, 3, 0, 8, 0, 0, 5, 0]
    [4, 2, 0, 1, 0, 0, 0, 0, 7]
    [0, 0, 1, 0, 6, 0, 5, 0, 0]
    [3, 0, 0, 0, 0, 5, 0, 0, 8]
    
    Solutions:
    [9, 3, 8, 5, 1, 7, 2, 6, 4]
    [2, 5, 7, 8, 4, 6, 3, 1, 9]
    [6, 1, 4, 2, 9, 3, 8, 7, 5]
    [5, 4, 9, 3, 2, 1, 7, 8, 6]
    [7, 8, 2, 6, 5, 4, 9, 3, 1]
    [1, 6, 3, 7, 8, 9, 4, 5, 2]
    [4, 2, 5, 1, 3, 8, 6, 9, 7]
    [8, 7, 1, 9, 6, 2, 5, 4, 3]
    [3, 9, 6, 4, 7, 5, 1, 2, 8]
    
    [9, 3, 8, 5, 1, 7, 2, 6, 4]
    [2, 5, 7, 8, 4, 6, 3, 1, 9]
    [6, 1, 4, 2, 9, 3, 8, 7, 5]
    [5, 4, 9, 3, 2, 1, 7, 8, 6]
    [7, 8, 2, 6, 5, 9, 4, 3, 1]
    [1, 6, 3, 7, 8, 4, 9, 5, 2]
    [4, 2, 5, 1, 3, 8, 6, 9, 7]
    [8, 7, 1, 9, 6, 2, 5, 4, 3]
    [3, 9, 6, 4, 7, 5, 1, 2, 8]
    
  • Quark Fox Avatar

    cannot cross more than 172 tests should i quite? or keep trying?

  • Quark Fox Avatar

    can anyone help this nub? what are the criteria of an in valid sudoku grid(unsolved)

    if values out of range (1-9) if given clue < 17 if given clue digit set < 8 ie ((n**2) -1) if cules are other than int if grid dimension is distorted if clues in row col or blocks are repeated if have multiple solution

  • Quark Fox Avatar

    how may i ask following is an invalid sudoku grid?

    [0, 0, 0, 0, 0, 6, 4, 0, 0], [0, 4, 5, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 9, 0, 5, 0, 3],

    [8, 0, 3, 0, 7, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 1, 8, 0], [0, 0, 1, 8, 0, 0, 0, 0, 0],

    [0, 0, 0, 5, 0, 0, 2, 0, 0], [0, 1, 0, 9, 8, 4, 0, 6, 0], [0, 0, 9, 0, 0, 2, 0, 0, 0]

  • Quark Fox Avatar

    can it flactuate from 152-23? while testing? can i know what is the number i need to cross?

  • Quark Fox Avatar

    while testing can same algorithem flactuate between completing as high as 125 then drop to as low as 25-26???

  • Quark Fox Avatar

    can any one help? what is in valid grid here?

  • Quark Fox Avatar

    after 6 long and hard days, trying to write in code how i solve sudoku, learning many new tricks of sudoku, trying and failing and trying.... and a thing or two about my limits and wierdness of how computers work, i mean i tried back tracking with while loop, but i knew there is something called backtracking!!! i have been trying to submit my sudoku soloution for last few hours, but getting timeout after completing 23-60 then exiting..... little bit sad!! though no point asking, but i am asking anyway, are the tests okay? it was hard for me ... but i knew i can make it .... but now .... i don't know what to feel or think? how many i 've to pass for a pass? isnt it good enough that it can work out the puzzle anyway!! come on guys!!

  • Aliaksandr-Borsuk Avatar

    Thank you for this kata! It was great!

  • clamper1800 Avatar

    Thanks. For some reason with my code in its current form the following happens:

    • I pass all 10 of the single solution tests. Then I time out and get no further (even though these get solved in about 50ms). It seems to freeze before going to ivnalid grids test and then it times out.
    • The weird thing is if I deliberately force and error during the single solutions test (I do this by saying if puzzle == one of the single test puzzles, then raise an error). What then happens is that it moves on from the single tests and goes onto the invalid and random puzzle tests, all of which I pass.

    This makes no sense to me. For some reason unless I force it to skip the single solution tests, it gets stuck there. Does anyone know why this might be please?

  • clamper1800 Avatar

    It is frustrating that my code keeps timing out without any indication as to when it was that I timed out. By using older code I am able to see the 10 invalid tests but for some reason I don't even get that far with my new, better code. I know it is better because I ran the invalid tests in Spyder and pass them much much quicker than the older code.

    Does anyone know where I can access the random tests please? Also, is the 12000ms limit the total time limit or the time limit per test section? Thanks

  • Vilin97 Avatar

    I am frustrated at the fact that perfectly fine grids are labeled invalid in the test set. This prevents me from complete this kata despite pouring a day of work in it.

  • potzko Avatar

    what kind of error exactly should I raise in python? I tried exceptions errors runtimeErrors and the test fails me showing me my own error....

  • KuziaTihii Avatar

    I got an error for this input: Input grid: [4, 7, 0, 3, 0, 2, 0, 6, 0] [0, 0, 9, 0, 0, 0, 2, 0, 0] [0, 8, 0, 0, 0, 0, 7, 0, 0] [0, 5, 0, 0, 1, 9, 0, 0, 0] [0, 0, 0, 6, 0, 5, 0, 0, 0] [0, 0, 0, 2, 8, 0, 0, 5, 0] [0, 0, 3, 0, 0, 0, 0, 9, 0] [0, 0, 2, 0, 0, 0, 8, 0, 0] [0, 4, 0, 8, 0, 6, 0, 7, 2] this is correct puzzle but i have "Invalid grid should raise an error"

  • Bazz9000 Avatar

    I got an error for this input: [0, 0, 0, 0, 0, 0, 3, 1, 0] [0, 8, 0, 0, 6, 0, 0, 0, 0] [0, 1, 0, 0, 0, 2, 5, 0, 4] [0, 0, 0, 2, 4, 0, 0, 0, 6] [0, 0, 7, 0, 8, 0, 9, 0, 0] [8, 0, 0, 0, 9, 5, 0, 0, 0] [1, 0, 3, 9, 0, 0, 0, 5, 0] [0, 0, 0, 0, 1, 0, 0, 4, 0] [0, 2, 8, 0, 0, 0, 0, 0, 0] "Invalid grid should raise an error"

    However, this is correct puzzle and it has single solution. I guess the test data is not very good?

  • rodonoah Avatar

    My code is raising an error for the following input:

    [0, 0, 0, 7, 0, 0, 0, 0, 0] [0, 0, 8, 0, 4, 9, 0, 1, 0] [0, 0, 5, 8, 0, 0, 0, 6, 0] [8, 0, 9, 0, 0, 0, 6, 0, 0] [0, 0, 0, 9, 0, 3, 0, 0, 0] [0, 0, 2, 0, 0, 0, 4, 0, 9] [0, 6, 0, 0, 0, 2, 7, 0, 0] [0, 2, 0, 5, 1, 0, 9, 0, 0] [0, 0, 0, 0, 0, 4, 0, 0, 0]

    However it seems the input is lacking commas between the elements (And if this was on purpose my code would pass the test since it raises a ValueError).

    If I manually add the commas back and run the code on my editor I get the right single solution:

    [2, 1, 4, 7, 5, 6, 3, 9, 8], [6, 3, 8, 2, 4, 9, 5, 1, 7], [7, 9, 5, 8, 3, 1, 2, 6, 4], [8, 7, 9, 4, 2, 5, 6, 3, 1], [1, 4, 6, 9, 7, 3, 8, 5, 2], [3, 5, 2, 1, 6, 8, 4, 7, 9], [9, 6, 1, 3, 8, 2, 7, 4, 5], [4, 2, 3, 5, 1, 7, 9, 8, 6], [5, 8, 7, 6, 9, 4, 1, 2, 3]

    Any thoughts?

  • hebozhe Avatar

    This comment has been hidden.

  • hebozhe Avatar

    This comment has been hidden.

  • Arastad Avatar

    Java version has an issue with random tests reporting puzzles as having invalid grid, even though they are valid and meet all criteria. Recieved a fail for the below puzzle, even though it meets all criteria as a valid puzzle.

    randomTests Log Input grid: [0, 0, 8, 0, 0, 9, 0, 0, 0] [9, 0, 0, 7, 1, 0, 4, 0, 0] [0, 0, 0, 4, 0, 0, 7, 0, 0] [1, 0, 9, 0, 4, 0, 0, 8, 0] [0, 5, 0, 0, 0, 0, 0, 1, 0] [0, 3, 0, 0, 2, 0, 5, 0, 7] [0, 0, 6, 0, 0, 5, 0, 0, 0] [0, 0, 7, 0, 8, 6, 0, 0, 3] [0, 0, 0, 1, 0, 0, 2, 0, 0]


    Your result: [7, 4, 8, 6, 3, 9, 1, 2, 5] [9, 6, 5, 7, 1, 2, 4, 3, 8] [3, 2, 1, 4, 5, 8, 7, 6, 9] [1, 7, 9, 5, 4, 3, 6, 8, 2] [8, 5, 2, 9, 6, 7, 3, 1, 4] [6, 3, 4, 8, 2, 1, 5, 9, 7] [2, 9, 6, 3, 7, 5, 8, 4, 1] [4, 1, 7, 2, 8, 6, 9, 5, 3] [5, 8, 3, 1, 9, 4, 2, 7, 6]


    Invalid grid, should have thrown an IllegalArgumentException.

  • hebozhe Avatar

    Raising an error in Python is not catching the error. Also, it's not stated what kind of error to throw.

  • IceGlance Avatar

    Something wrong with Python test cases.

    "Invalid grid should raise an error" appears for

    board = [ [0, 0, 2, 0, 0, 1, 0, 0, 0], [3, 0, 0, 6, 8, 0, 7, 0, 0], [0, 0, 0, 5, 0, 0, 6, 0, 0], [7, 0, 5, 0, 2, 0, 0, 3, 0], [0, 1, 0, 0, 0, 0, 0, 5, 0], [0, 8, 0, 0, 4, 0, 9, 0, 1], [0, 0, 7, 0, 0, 4, 0, 0, 0], [0, 0, 4, 0, 1, 7, 0, 0, 9], [0, 0, 0, 9, 0, 0, 8, 0, 0]]

    This board has only one solution, I tested it with my script and a lot of other submitted scripts. Can anyone suggest what the problem is?

  • fmallaba Avatar

    Solved using optimised backtracking algorithm. But sometimes it fails with 'Invalid grid should raise exception' at random tests. I check for multiple solutions. Fixed test with multiple solution passed. What can be wrong with grid that I don't check? P.S. I've managed to submit solutions since it passes sometimes :)

  • Twilight_Sun Avatar

    This comment has been hidden.

  • Husoski Avatar

    Can anybody tell me what the "Log" boxes are for in the Output window (Python version, if that matters)? I'm trying to debug a problem that only seems to show up in the random tests where it complains that an "Invalid grid should raise an error."

    Occasionally it shows a log box immediately below that showing an input grid and my solution; but I can't tell if that log applies to the red line above it or the green line below. However, the ones I've looked at are all perfectly-valid single-solution cases so far. (Tested on my own graphical solver from 15+ years ago, plus a couple of web solvers just in case.) This, of course, means nothing if the logged input/output grids don't go with the failed test case.

    Or, does anyone have an idea how I can find what input grid failed the test? From there I can go offline and find the problem.

  • PurpleMyst Avatar

    I'm not sure I understand the assignment here. How am I supposed to figure out if a puzzle is unsolvable without searching.. literally every sudoku board that could generate from the seed I'm given?

  • Persa Avatar

    what is the maxiumum text ms to pass

  • Ping He Avatar

    My code (Python) have checked invalid puzzles and raised errors, something just like below. It throws correct RuntimeErrors on my own Python IDE(3.7.3), but fails Invalid grids test with red info "Invalid grid should raise an error.". Would some experts tell me what is the right way to raise errors for this Kata?

    if len([y for x in puzzle for y in x]) != 81: raise RuntimeError("Invalid grid: row/col numbers")

  • mrPantograf Avatar

    This comment has been hidden.

  • sadegh-moayedizadeh Avatar

    my code times out, but the problem is np-complete, wonder what the problem might be...

  • Samathingamajig Avatar

    This comment has been hidden.

  • doooom Avatar

    Did not figure out some invalid puzzles. Better to add more explanations. Just passed the test by luck.

  • SplashyMelon Avatar
     int[][] p1 = {{9,0,6,0,7,0,4,0,3},
                {0,0,0,4,0,0,2,0,0},
                {0,7,0,0,2,3,0,1,0},
                {5,0,0,0,0,0,1,0,0},
                {0,4,0,2,0,8,0,6,0},
                {0,0,3,0,0,0,0,0,5},
                {0,3,0,7,0,0,0,5,0},
                {0,0,7,0,0,5,0,0,0},
                {4,0,5,0,1,0,7,0,8}};
    
     int[][] p2 = {{3,4,7,9,0,0,0,8,0},
                {0,1,0,8,0,0,0,5,0},
                {0,0,0,0,0,0,0,1,0},
                {0,0,0,5,0,0,0,0,8},
                {0,0,0,4,0,8,0,0,0},
                {6,0,0,0,0,3,0,0,0},
                {0,7,0,0,0,0,0,0,0},
                {0,8,0,0,0,5,0,3,0},
                {0,5,0,0,0,4,2,7,9}};
    

    I found one solution for each of them.
    Why should they throw an IllegalArgumentException??
    There are more than 16 Given Numbers and also 8 different numbers given.
    The Grid size is valid.

  • bulgarian-beast Avatar

    Hello I don't understand what should be return in case of error detected

  • Rekkon Avatar

    Somebody please open this for C#.

  • user3029010 Avatar

    nice problem :)

  • user3029010 Avatar

    my favorite 2 kyu problem :)

  • user3029010 Avatar

    so hard

  • yangrui Avatar

    Failed on "Invalid grid, should have thrown an IllegalArgumentException.". Why I can see the failed puzzle? Looks like others could do that

  • vadim2404 Avatar

    What kind of exception must be thrown on Python? It's not covered in the description and tests

  • aropan Avatar

    I get "should be raise error" on puzzle:

    [
        [2, 7, 0, 6, 0, 8, 0, 4, 0],
        [0, 0, 8, 0, 0, 0, 2, 0, 0],
        [0, 9, 0, 0, 0, 0, 3, 0, 0],
        [0, 5, 0, 0, 8, 2, 0, 0, 0],
        [0, 0, 0, 5, 0, 6, 0, 0, 0],
        [0, 0, 0, 9, 1, 0, 0, 5, 0],
        [0, 0, 7, 0, 0, 0, 0, 8, 0],
        [0, 0, 2, 0, 0, 0, 9, 0, 0],
        [0, 6, 0, 2, 0, 3, 0, 7, 4],
    ]
    

    But I found only one solution:

    [[2, 7, 3, 6, 9, 8, 5, 4, 1],
     [6, 1, 8, 4, 3, 5, 2, 9, 7],
     [4, 9, 5, 7, 2, 1, 3, 6, 8],
     [9, 5, 4, 3, 8, 2, 7, 1, 6],
     [3, 8, 1, 5, 7, 6, 4, 2, 9],
     [7, 2, 6, 9, 1, 4, 8, 5, 3],
     [5, 3, 7, 1, 4, 9, 6, 8, 2],
     [1, 4, 2, 8, 6, 7, 9, 3, 5],
     [8, 6, 9, 2, 5, 3, 1, 7, 4]]
    

    I checked four puzzle (rotated 90 degrees) and all solved puzzles are equal.

    There are no errors in random tests?

  • janmalovec Avatar

    Hello,

    How many tests can I expect? Right now my result looks like this: ingle solutions (11 of 11 Assertions) Invalid grids (10 of 10 Assertions) Unsolvable ones (2 of 2 Assertions) Random tests (52 of 53 Assertions) Execution Timed Out (12000 ms)

  • VladimirB Avatar

    This test failed with error: "Invalid grid, should have thrown an IllegalArgumentException."

    Grid: [5, 0, 0, 0, 0, 0, 1, 0, 0] [0, 4, 0, 2, 0, 8, 0, 6, 0] [0, 0, 3, 0, 0, 0, 0, 0, 5] [9, 0, 6, 0, 7, 0, 4, 0, 3] [0, 0, 0, 4, 0, 0, 2, 0, 0] [0, 7, 0, 0, 2, 3, 0, 1, 0] [0, 3, 0, 7, 0, 0, 0, 5, 0] [0, 0, 7, 0, 0, 5, 0, 0, 0] [4, 0, 5, 0, 1, 0, 7, 0, 8]

    But it has solution!


    |5 8 2|3 6 7|1 9 4| |1 4 9|2 5 8|3 6 7|

    |7 6 3|1 4 9|8 2 5|

    |9 2 6|5 7 1|4 8 3| |3 5 1|4 8 6|2 7 9|

    |8 7 4|9 2 3|5 1 6|

    |2 3 8|7 9 4|6 5 1| |6 1 7|8 3 5|9 4 2|

    |4 9 5|6 1 2|7 3 8|

  • germankrause Avatar

    Somebody translate to Javascript please

  • bastiendev Avatar

    This comment has been hidden.

  • grizzlyzen Avatar

    This comment has been hidden.

  • 舍命为江山 Avatar

    ruby random test: puzzle: [[8, 0, 0, 5, 0, 0, 0, 0, 3], [0, 0, 5, 0, 6, 0, 1, 0, 0], [7, 0, 0, 0, 0, 1, 0, 2, 4], [0, 5, 0, 0, 8, 0, 3, 0, 0], [1, 0, 0, 0, 0, 0, 2, 0, 7], [0, 0, 7, 0, 2, 0, 0, 4, 0], [5, 7, 0, 3, 0, 0, 0, 0, 6], [0, 0, 3, 0, 4, 0, 7, 0, 0], [4, 0, 0, 0, 0, 5, 0, 0, 9]] my solved: [[8, 2, 1, 5, 7, 4, 6, 9, 3], [3, 4, 5, 2, 6, 9, 1, 7, 8], [7, 9, 6, 8, 3, 1, 5, 2, 4], [2, 5, 4, 9, 8, 7, 3, 6, 1], [1, 3, 9, 4, 5, 6, 2, 8, 7], [6, 8, 7, 1, 2, 3, 9, 4, 5], [5, 7, 8, 3, 9, 2, 4, 1, 6], [9, 1, 3, 6, 4, 8, 7, 5, 2], [4, 6, 2, 7, 1, 5, 8, 3, 9]] why Value is not what was expected??? many random test in ruby seems uncorrect.

    [[3, 0, 0, 0, 0, 5, 0, 0, 8], [0, 0, 1, 0, 6, 0, 5, 0, 0], [4, 2, 0, 1, 0, 0, 0, 0, 7], [0, 0, 3, 0, 8, 0, 0, 5, 0], [7, 0, 2, 0, 0, 0, 0, 0, 1], [0, 4, 0, 0, 2, 0, 7, 0, 0], [6, 0, 0, 0, 0, 3, 0, 7, 5], [0, 0, 7, 0, 4, 0, 3, 0, 0], [9, 0, 0, 5, 0, 0, 0, 0, 4]] [[3, 9, 6, 4, 7, 5, 1, 2, 8], [8, 7, 1, 9, 6, 2, 5, 4, 3], [4, 2, 5, 1, 3, 8, 6, 9, 7], [1, 6, 3, 7, 8, 4, 9, 5, 2], [7, 8, 2, 6, 5, 9, 4, 3, 1], [5, 4, 9, 3, 2, 1, 7, 8, 6], [6, 1, 4, 2, 9, 3, 8, 7, 5], [2, 5, 7, 8, 4, 6, 3, 1, 9], [9, 3, 8, 5, 1, 7, 2, 6, 4]] Value is not what was expected

    thanks for notice where i am wrong

  • EAirPeter Avatar

    Nice kata, a good exercise for me.

  • grifmoney Avatar

    My solution occasionally solves "invalid" boards generated randomly.

  • Blind4Basics Avatar

    Python version updated with random tests and more tests about validation of the grids too.

  • Blind4Basics Avatar

    Java translation

    Consistent with the ruby version. Please, someone, review and approve.

  • Blind4Basics Avatar

    Ruby translation!

    • several edge cases added (invalid puzzles, unsolvable ones)
    • random tests added, with valid grids, unsolvable ones and grids with multiple solutions.
  • user6793616 Avatar

    This comment has been hidden.

  • StrongerXi Avatar

    On my computer I was able to pass the given test case within a second, but somehow when I copy the code and attempt the testcase here I get a timeout error. The only reason I can think of is I'm using python 3.6, but here only 3.4 is available. But still, that should not make such a huge efficiency difference right?

  • theDarkBright Avatar

    This comment has been hidden.

  • theDarkBright Avatar

    Is around 3500 ms a good speed? Has anyone done better?

  • sheinz Avatar

    Thanks for the nice kata. I have a question. Does this kata requires some sort of optimized algorithm? Because my solution works but fails due to the long run. My algorithm is a straigh forward approach. At first I eliminate the obviously wrong candidates for each cell and then I try all the remaining combinations. I would appriciate a hint ;)

  • ecolban Avatar

    Nice kata. However, it lets solutions pass that do not raise an error when a puzzle has multiple solutions. I have submitted two solutions to this kata, one that does not check for multiple solutions and just returns the first it finds, and a second that raises an error when the puzzle has two or more solutions. Both pass the tests, although the first shouldn't have.

    The only difference between the two solutions is that the first calls some() where the second calls unique().

  • TamarAmir Avatar

    This comment has been hidden.

  • anter69 Avatar

    This comment has been hidden.

  • anter69 Avatar

    Need a hint on how to detect if a puzzle has multiple solutions?

    edit: it looks like backtracking cannot detect this kind of issues, and it's not fast enough either...

  • joedrake1983 Avatar

    Hi f.rodrigues,

    My solution works on the simple test case then when I try to submit I get:

    Passes on Invalid puzzles Passes on Unsolvable puzzle Passes on More than one answer Fails (I think) on Should solve an easy puzzle - not sure, if this is the same as the first test case then I should pass

    This is as far as the output goes but I get a StdError suggesting that my code does not handle this last test case.

    Are you able to share this last test case?

    Thanks

  • ChristianECooper Avatar

    This comment has been hidden.

  • f.rodrigues Avatar

    Feel free to provided test cases that would fail invalid solutions.

    Since I'm not a native english speaker it may contain some errors, please provide the correct way and I'll change it!

    Thanks.