3 kyu

Hard Sudoku Solver

1,008 of 1,057Marx314
Description
Loading description...
Algorithms
Games
Puzzles
Game Solvers
  • Please sign in or sign up to leave a comment.
  • Terekhov.I.A. Avatar

    It was very difficult for me. I even gave up CodeWars for a year because I couldn't solve this problem in 2023. I don't like my solution because it's clumsy. But it worked! I'm glad I was able to solve this!

  • charger1ng Avatar

    This was a very challenging and fun kata! It required a lot of thought about which data structure and which algorithm to use to minimize the number of computations. In the end it was worth it and I managed to pass it marginally.

  • absfera Avatar

    A correctly composed Sudoku must have a unique solution !

  • oasbnfioansfoin12 Avatar

    timing out from backtrack :(

  • neerajgopal Avatar

    How many assertions per test? In my 100 random tests I've got 255 assertions passed but still timing out.

  • grigr Avatar

    Very interest Kata :) Thank you luckily i found the right method. my algorithm solved everything in 6 seconds, only on one Sudoku from your set it was required to take 1 step back. also my algorithm solved in 2 seconds "the most difficult sudoku in the world" by taking 14 steps back. I'm glad that my program coped with it!

  • janlos111 Avatar

    why is backtracking alorithm not efficient enough?? wat isnt it supposed to be the solution here

  • profarvin Avatar

    This comment has been hidden.

  • amir650 Avatar

    Even with a print statement at the top of your method it's impossible to debug time outs on this platform. My solution is lightning fast on my box, but of course submitting it here sends it into an infinite loop tailspin without access to the damn test suite.

  • IshaanKapoor Avatar

    I have tried everything I could, but it always times out. I think they are just too strict on time limit in this kata.

  • cuteufo Avatar

    This comment has been hidden.

  • KovDimaY Avatar

    This comment has been hidden.

  • petrrohr Avatar

    I'm consistently getting to 267 assertions in 100 random tests. This seems a bit surprising, since the randomness should introduce some variability in terms of how many I complete in time?

    I tried putting time.sleep(0.05) into my solve() function - which should introduce enough delay over time, to make me complete a smaller number random tests. But it has no impact at all, either I get time-out straight away with no test results shown, or I get to 267. There were couple instances where I got 266 instead, but other than that it always seems to be 267.

    Any thoughts? I understand that 267 might simply show how fast (or rather slow) my code is, I'm just surprised by the consistency, especially when time.sleep() gets thrown in the mix.

  • bsz Avatar

    Hi! Thanks for posting this kata.

    What are the rules for determining a valid sudoko? From reading the article, it could be that each digit from 1 to 9 should appear once in each of the 9 3x3 subgrids - or maybe each row and column should also contain each digit exactly 1 time, as suggested ny the sentence "For example, the same single integer may not appear twice in the same row, column, or any of the nine 3×3 subregions of the 9×9 playing board"? Maybe there are other constraints (why else starting the sentence with 'For example'?)

    Please consider including the rules in the kata description.

  • Ypc49 Avatar

    Hi. Great Kata, thanks ! Do we need to use numpy or is it not necessary ?

  • raptarych Avatar

    "The solution only need to give one valid solution" - which one? For example, problem case from "Fixed tests":

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

    My solutions:

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

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

    Each solution seems work, 487 tests passed with strategy "return first calculated solution". Ironically, all tests "Now testing multiple solution sudoku!" were passed but fails on fixed and random tests. How to decide which one should be returned to pass?

  • artsR Avatar

    How to check how many tests were passed before Time Out?

  • CloudClique Avatar

    Passed: 304 Failed: 0 Exit Code: 1

    Am I close?

  • Valentyn465 Avatar

    Tried lots of 'evil' task and everything going well on home computer with time less than 100 ms... but when tried to Attempt it's steady giving time out?

  • YOEL311 Avatar

    The examiner appears to keep cash between tests I fail at tables that my code resolves well

  • nandaherrryk Avatar

    Nice kata! But, I still need to optimize my code to avoid time out.

  • wickedposh Avatar

    This comment has been hidden.

  • ollieh-m Avatar

    Have enjoyed working on this! Thanks. Unfortunately my solution - a backtracking algorithm - is too slow. It gets through approx. 120 tests. Could you indicate how many tests are there in total?

  • Blind4Basics Avatar

    Python version updated with random tests.

  • Blind4Basics Avatar

    Ruby translation

    • updated the description, removing the useless information (naked pairs, hidden pairs,...)
    • added 50 random tests
  • esotericpig Avatar

    My solution passes all tests but the last one of the multi-solution. It just says Incorrect solution: False should equal True.

    When I try to do a print or sys.stdout.write to see what the board is, nothing is logged for it, so I assume you are supressing the log for the last solution? But, I get a log for the other solutions.

    It's weird that my solution passes all multi-solutions except for the last one. Does the last one check all multi solutions? Or does it just check if valid?

  • wenima Avatar

    This comment has been hidden.

  • giraffe Avatar

    I got the following test error message: ✘ You're solution shound only contains int: False should equal True Could anyone explain what happend? It can be easily checked that the solution is a valid one(if my understanding about this game is right.) The problem is [[0, 8, 0, 0, 0, 9, 7, 4, 3], [0, 5, 0, 0, 0, 8, 0, 1, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [8, 0, 0, 0, 0, 5, 0, 0, 0], [0, 0, 0, 8, 0, 4, 0, 0, 0], [0, 0, 0, 3, 0, 0, 0, 0, 6], [0, 0, 0, 0, 0, 0, 0, 7, 0], [0, 3, 0, 5, 0, 0, 0, 8, 0], [9, 7, 2, 4, 0, 0, 0, 5, 0]]

    The solution I get was: [[2, 8, 6, 1, 5, 9, 7, 4, 3], [3, 5, 4, 7, 6, 8, 9, 1, 2], [7, 1, 9, 2, 4, 3, 5, 6, 8], [8, 9, 3, 6, 1, 5, 4, 2, 7], [6, 2, 7, 8, 9, 4, 1, 3, 5], [1, 4, 5, 3, 2, 7, 8, 9, 6], [5, 6, 8, 9, 3, 1, 2, 7, 4], [4, 3, 1, 5, 7, 2, 6, 8, 9], [9, 7, 2, 4, 8, 6, 3, 5, 1]] ✘ You're solution shound only contains int: False should equal True

  • hhummel Avatar

    I justed used my solution for the "easy" sudoku. I rigged it up so it can search for multiple solutions, (in a list of solutions), but it wasn't clear to me from how the problem is stated if you are looking for that.

  • Marx314 Avatar

    Hi,

    Thanks for taking the time to ask, maybe the solution given contains a string of codewars changed their version of python. I've added an assertion to make sure that solution only contains int Test.assert_equals(only_int, True, "You're solution shound only contains int")

  • anter69 Avatar

    I never thought I could do this! But after several sessions of struggle, I've done it! Thanks for the kata! :-)

  • pjz2 Avatar

    "Run Suite' writes: [[7, 0, 5, 6, 2, 0, 8, 0, 0], [0, 2, 0, 8, 0, 9, 0, 7, 5], [3, 0, 8, 7, 4, 5, 0, 2, 1], [5, 3, 0, 2, 0, 6, 0, 1, 0], [0, 0, 2, 0, 0, 0, 5, 0, 0], [0, 7, 0, 5, 0, 4, 0, 6, 2], [2, 5, 0, 0, 6, 7, 0, 8, 4], [0, 8, 0, 4, 5, 2, 0, 9, 0], [0, 0, 7, 0, 0, 0, 2, 5, 0]] should equal [[7, 1, 5, 6, 2, 3, 8, 4, 9], [6, 2, 4, 8, 1, 9, 3, 7, 5], [3, 9, 8, 7, 4, 5, 6, 2, 1], [5, 3, 9, 2, 7, 6, 4, 1, 8], [4, 6, 2, 1, 9, 8, 5, 3, 7], [8, 7, 1, 5, 3, 4, 9, 6, 2], [2, 5, 3, 9, 6, 7, 1, 8, 4], [1, 8, 6, 4, 5, 2, 7, 9, 3], [9, 4, 7, 3, 8, 1, 2, 5, 6]]

    but if i enter the first matrix into the program at home, it returns the second matrix, correctly. so it looks like the testing mechanism bug to me because why program which solved 20 sudokus including this one should just do nothing with input out of the sudden... am i wrong?

    UPDATE: i beg pardon, the testing mechanism is right. now my problem is:

    solve(problem ); solve(problem2) does not return the same mixed result as solve(problem2); solve(problem )

    in both cases the solve function properly finishes the first board and do nothing with the second. that's kinda weird to me and i have a feeling i got smarter once i find out that. if anyone have a basic idea of why that's suppose to happen, i will gladly read the comments

  • varunbpatil Avatar

    This comment has been hidden.

  • f.rodrigues Avatar

    A sudoku game should have only one solution. In case it have more solution the function should be expected to raise an error like IndexError("Puzzle answer is not unique.").

  • jolaf Avatar

    Whoof, this one is really tough! I've just spent a few hours on it, and still all my solutions are way too slow.

    Don't you want to provide some hint on practical algorithms to solve it?

  • jolaf Avatar

    In pre-set tests, the following construction is used:

    Test.expect(solve(problem) == solution, "You won")

    that has two problems:

    1. You won is printed if the solution is NOT correct.

    2. The expected and received solutions are not printed.

    Please consider using Test.assert_equals(solve(problem), solution, "Incorrect solution") instead.