Coding Games and Programming Challenges to Code Better
Send your feedback or ask for help here!
Created by @yoch,validated by @cedricdd,@Timinator and @Lanfeust.
If you have any issues, feel free to ping them.
Coding Games and Programming Challenges to Code Better
Send your feedback or ask for help here!
Created by @yoch,validated by @cedricdd,@Timinator and @Lanfeust.
If you have any issues, feel free to ping them.
Is this even solvable in c? i feel like execution delays are insanely restrictive is it just on that particular language? iāve tryed a lot optimizing tricks bit masking linkchaining and list checking paired with row/column/block counters to auto out singles and nothing seems to works, im runing out of ideas? all other ideas i have litteraly take to me to c# or c++ā¦
C is one of the fatest languages, and I can see that some people made it in python, so you definitively can make it in C. I didnāt make it so I canāt help you about ideas.
I solved it in Python with the mixed integer linear programming module from Scipy, so itās technically Cython not pure Python, but solved the worst case in less than 500ms. You can certainly do better in C. The other Python solutions here are using a very popular algorithm. If you search for sudoku solving algorithms youāll find it.
ātechnically Cā in Python means a benefit in time of about 10x versus really in C, since time limit for C in puzzles is about ten times lower than for Python, right?
From my experiments, itās more like 20x, but the MILP approach is terribly inefficient. I used it because Iām practicing it, not to avoid the time limit. The pure Python solutions using better algorithms still finish well below the time limit.
Anyway, itās certainly possible to solve this in C because thereās one published solution here. Itās not easy, itās a Very Hard puzzle after all, but itās definitely possible.
I am trying to pass this puzzle using Timinator and 5DN1Lās playground (in C++), and I am getting some timeouts.
After benchmarking a bit, the code is consistently slower on codingame by a factor of 3:
Timeouts make sense since from test 3 onwards multiplying by 3 gets me over the 1s barrier.
I compiled without flags (g++ file.cpp).
I donāt have any insight into these timing differences. Just to clarify, I have a couple of Algorithm X questions:
Have you already done the easier Sudoku puzzles with your C++ translation?
Are you breaking out of the search after Algorithm X finds the first solution.
I have a general comment that might help anybody trying to solve this puzzle. If you are running into timing issues, the least painful path might be to do some logical, problem-space reduction before doing any search with backtracking.
Algorithm X Playground Pg 60: Sudoku Challenge
Using the problem-space reduction techniques discussed on the website that I reference in the playground, I am able to reduce the size of the problem for Algorithm X and my slowest times are less than 30% of the Python time limit.
To solve this puzzle in Python without any problem-space reduction was painful for me. I had to play around with the DLX column ordering until I matched the authorās setup which I measured by the number of calls to solve(). This might not be a problem for a Python person because my solver now defaults to this order. In hindsight, I should have made a bigger deal out of this when I was reviewing the puzzle, but I didnāt. So, the bottom line is that column order can make a big difference on this puzzle for us Python folk and it might for you too if you are using Algorithm X.
C++ on CG is compiled in debug mode (= slow). Add #pragma gcc optimize ā-O3ā at the very top of yor code for release mode. I am unsure whether speed is really your issue. I have solved this one with Python without any of the optimisations suggested by Timinator
Thanks for your answers.
I solved the 16x16 version with an average solve time of 50ms.
Yes, as there is no yield equivalent in C++ (as far as I know), I return the first valid solution found.
Makes sense. I didnāt realize there were many more info regarding solving the sudokus on the playground, I will look into it. ^^
Do you know if they use some -fsanitize level of debug ?
Indeed using #pragma GCC optimize(āO3ā) speeds everything up, I only timeout on test5 with it. My goal will be to pass without it as it feels like a cheat code.
Currently I am leaking all over the place, I guess it doesnāt help my case.
I managed to pass it in debug mode, with test 5 taking 225ms.
I doubt this will surprise anyone, but it was indeed a me problem.
For anyone following the AlgorithmX playground and coming in this thread, here are some steps I took to achieve this (still C++), going from ~550ms to ~100ms on my computer on test case 5:
There is still a lot of room for optimization, for now I see mainly: