Solving Sudoku With dpkg
Reader Otter points out in his journal a very neat use for the logic contained in Debian's package dependency resolver: solving sudoku puzzles. To me at least, this is much more interesting than the sudoku puzzles themselves. Update: 08/24 02:51 GMT by T : Hackaday just ran a story that might tickle the same parts of your brain on a game played entirely with MySQL database queries.
1. Computers can generate Sudokus
2. Computers can solve Sudokus
3. Skynet determines that humans are useless. It then creates what is called "virtual worlds" in a campaign to exterminate the global human population birth rate.
Because cheats impress babes. left-right-left-right-a-b-start! left-right-left-right-a-b-start! I think I feel tingley.
Once you start despising the jerks, you become one.
Sudoku isn't a math puzzle, it's a logic puzzle - just one where you're filling in digits instead of the man in the blue house smoking Pall Malls and having a goldfish.
The digits 1-9 in Sudoku could be replaced with any 9 other symbols without changing the underlying rules. So yeah, logic can be used to solve it.
Village idiot in some extremely smart villages.
Because cheats impress babes. left-right-left-right-a-b-start! left-right-left-right-a-b-start! I think I feel tingley.
Please hand in your geek card immediately.
Jesus Christ. If you're going to mention the greatest cheat code ever, get it right:
Up-Up-Down-Down-Left-Right-Left-Right-B-A-(Select)-(Start)
Amateur.
First, it's not "cheat codes".
Second, I, and I'm sure I'm not alone on this, would rather write a program to solve sudoku than actually play sudoku. Some people love sudoku; I found it boring. Now writing software to solve a puzzle, that's interesting.
Maybe not
(Darned fast little program, too. Won a trivial little prize with it.)
The World Wide Web is dying. Soon, we shall have only the Internet.
Sodokus are typically solved by using logic and identifying relationships, but the article describes a clumsy brute force method. I will admit that I'm splitting 'mathematical' hairs here, and that it is an amusing thought to readapt Debian's simple dependency resolver, but crude solving functions, that simply guess at every possible solution, are fairly boring when compared to clever logic with elegant methods, that can solve sudokus in a fraction of time.
I just feel sorry for geeks living in Soviet Russia. I've heard horror stories that suggest that over there, the geek cards hand in the geeks. Can you imagine the betrayal of your geek card giving you up like that?
And he didn't even use Super Cow Powers to do it!
RTFA. I know, I know, what am I suggesting, it's Slashdot.
Here's the quick version: Russell Coker remarked that "a package management system that can solve Sudoku based on package dependency rules is not something that I think would be useful or worth having."
Daniel Burrows realized that apt could, in fact, currently be used to solve Sudoku puzzles, and wrote a Python script to automate the process of creating the packages required to do such a thing. That's the linked article, and it gives the background I'm repeating here.
I, personally, think it's pretty damned cool. Useless, but cool.
And, as the article points out, there exist better Sudoku solving algorithms. apt is a rather poor Sudoku solver, mainly because it's designed to come up with the "best" dependency resolution rather than solve Sudoku. It's not to "cheat" at Sudoku, but rather to demonstrate the power of the apt dependency resolver.
You are in a maze of twisty little relative jumps, all alike.
Bah, thats only for two player... Most /.ers play with themselves!
It isn't about beating sudoku. It's about taking one tool, and using it to do something that its creators never imagined.
It's the same reason people use laser cutters to slice their pizza or try to create the shortest possible quine in their language of choice. This guy might not even give a shit about sudoku; he just wanted to see if he was clever enough to solve sudoku using dpkg.
Exactly. This guy doesn't care about the sudoku puzzle, he cares about the programming puzzle.
Sudoku doesn't have clever logic and elegant methods.
Check out the various strategies listed on this Sudoku Solver.
Don't mod me down if you disagree. If you disagree, consider writing a retort instead.
You must be new here. Only posters that take the time to back up conclusions with reasoned responses are moderated down. Conversely, those that write short, unsupported attacks are moderated up... because in reality most people can only be trusted with 2 tags - I agree or I disagree.
Sudoku doesn't have clever logic and elegant methods. There is only one method for solving sudoku puzzles, and it strongly resembles a computer doing brute force.
Sure, there are brute force methods. They are often techniques that dive into deep "consequence" trees to find contradictions. Those are, by their very nature, annoying for people to do and thus attractive for computer solutions. Nishio, tables, all of those just make sudoko boring and feel like you're executing a computer program in your limited-RAM brain.
But those aren't the "clever" or "elegant" methods. Sudoku techniques that I would consider elegant are things like sashimi x-wings, XYZ-wings, the various type of unique rectangles, and such. I enjoy trying to discover patterns like these in really tricky sudoku problems. I expect I'm not the only one, given the popularity of the puzzle over the last few years.
If you want to get really deep, you can use sudoku puzzles to explore mathematical group theory.
All of this (and what you said in your post) are true for other puzzles such as the Rubik's cube. Perfectly suitable for machine automation, but still fun for some of us us lowly humans as well.
I didn't have much luck using apt to handle dependencies either :(
slashdotter@momsbasement:~$ sudo apt-get install girlfriend
[sudo] password for slashdotter:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package girlfriend is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package girlfriend has no installation candidate
slashdotter@momsbasement:~$
You and the knucklehead who modded you insightful have completely missed the point.
Ultimately this approach simply encodes the rules of sudoku, and the state of an unsolved sudoku puzzle in logic statements, and then passes it off to a reasonably general logic solver, namely the dependency/conflict resolver in apt-get/aptitude. That's not really brute force at all.
Whether or not the solution is "brute force" depends on the manner in which debian's dependency/conflict resolver operates. There's many approaches to solving this problem, from gross brute force to reasonably complex machine learning approaches.
The insight in this approach (although it's not an especially new insight to people playing with this sort of idea), is the relationship between puzzles and other day-to-day computing problems purely as constraint satisfaction problems, which essentially means that we describe the problem abstractly without all the trappings of how humans interpret the problem, and let a computer with little to no "general purpose" knowledge or "common sense" come up with the solution on its own (and of course, of us being able to read out the solution once the computer nails it)
"You know, Hobbes, some days even my lucky rocketship underpants don't help" -- Calvin
Let's seem yum do THAT!
I kid I kid.
Also, if you're going to write a Sudoku puzzle generator, you have to write a Sudoku solver first. New puzzles are created by coming up with a random valid solved Sudoku puzzle, removing a bunch of numbers, checking to see if it's still solvable, and then removing some more...rinse, wash, repeat...
ZuluPad, the wiki notepad on crack
This is very cool! Kind of like implementing the Towers of Hanoi in vim or something. I'm going to test it against some puzzles from this here handy dandy Sudoku book. Now if only someone would make a Chess solver out of dpkg. You choose any out of the huge number of possible mate layouts and it will compute the dependencies from the start of the game to that mate layout! Implementing this should be so obvious that only a total fool won't immediately see how to do it, so it is left as an exercise for the reader.
McCain/Palin '08. Now THAT's hope and change!
Hey now, there's a reason why they're called "joysticks."
Not really, while that is one way of doing it, there are other ways of solving the problem.
It's probably less costly to take a square away at a time and check to see if it's still solvable than to populate a random start and see if it's possible to solve it. And probably less complicated as well.
The actual logic involved would be very different, and with this way you'd be able to reuse calculations from before by just checking the rows and box affected by the change to make sure that you can still get that one square without guessing.
It's probably less costly to take a square away at a time and check to see if it's still solvable than to populate a random start and see if it's possible to solve it. And probably less complicated as well.
Isn't that pretty much exactly what I said? Create a random valid solved puzzle, then start removing squares...
ZuluPad, the wiki notepad on crack
There's a reason the game is best played with a pencil, and I've used nothing but pen in every math class I've ever taken.
The paper quality of newspapers and sudoku booklets is thin and brownish, so pencil marks aren't very easy to see, and erasing on cheap paper is a disaster. At first I resisted using a pen, but now I'd never go back.
Interesting? Writing a SAT program to solve sudoku takes less than five minutes. This is not an interesting puzzle for a computer.
Probably also could be done using Make, which is really an expert system in disguise. Ant-heads won't know what I'm talking about. (Mod to +5 Flamebait.)
Since Sudoku is NP complete, doesn't this mean Dpkg is NP complete?!
Oh, the humanity! I'm just waiting for an evil set of dependencies to crop up that makes it go exponential.
Sudoku is boring, like running, or tai chi. But, that's also why I like playing. It keeps one part of my brain engaged and exercising, and the remainder can wonder off into fantasy land, dreaming up the next page of code I want to write.
At least, that's why I like playing Sudoku. I also hate the puzzles that force you to guess; or, well, force you to guess more than 2 or 3 or 4 cycles out (depending on how many squares in each "cycle", which would be shared implicit constraints). I refuse to erase a square once I place a number, so if I can't keep track of a set of guesses in my head, it's a poor puzzle in my opinion.
I don't know if a cheat code could ever turn somebody on but, as a person who has actually played Life Force and Contra, I can say that getting it wrong can definitely make you seem monkey-like.
Just imagine what the average Sudoku player thinks about Slashdot.
Maybe this can be used to stress test the resolve-engine in apt-get, aptitude and synaptic.
Sudoku isn't a math puzzle, it's a logic puzzle...
Well, a logic puzzle is `applied logic' (an application of the study of logic). And, applied logic in a formal system is essentially what mathematics is. In fact, (!) the act of solving a soduko puzzle is an instance of a type of graph coloring problem which appears in combinatorics -- hardcore mathematics. Even more interesting, a program which can solve an n^2 x n^2 soduku type puzzle in polynomial time (in n) would be a miraculous thing: it would show that P=NP, since the question of asking whether such an n^2 x n^2 instance is solvable is NP complete! I'm a graduate student in mathematics and this sort of stuff is my cup of tea -- though, I admit that _doing_ Sodoku puzzles doesn't interest me at all. The AMS Notices published a little article about this a while back, but I don't know the citation.
Hell, there are no rules here-- we're trying to accomplish something. --Thomas A. Edison
You don't need a full solver to create a solved puzzle, I should think. Just start with the most basic puzzle and make legal permutations of it:
123|456|789
456|789|123
789|123|456
---+---+---
231|564|897
564|897|231
897|231|564
---+---+---
312|645|978
645|978|312
978|312|645
For example, you should be able to swap any two numbers everywhere.
Comment removed based on user account deletion
Because cheats impress babes. left-right-left-right-a-b-start! left-right-left-right-a-b-start! I think I feel tingley.
Up up down down left right left right B A B A
Have you never played Contra?!?
It is dangerous to be right when the government is wrong.
The article is really a nerd article, and now we all have a challenge!
What is YOUR software solution to solve Soduku puzzles? Think outside the box, or go for the classic brute force method.
I would think about using languages like Erlang or Prolog to solve the problem, but classic languages with iterating over the alternatives will do also. Pick your poison!
If builders built buildings the way programmers wrote programs, then the first woodpecker would destroy civilization.
The dependency resolver in RPM is just as powerful and can also solve these kinds of puzzle. No promises on how fast it will solve them, however.
That's the 2-player code.
Up-Up-Down-Down-Left-Right-Left-Right-A-B-(Start) is single player contra.
Not a sentence!
Isn't that pretty much exactly what I said?
I'm currently writing a solver for questions from Slashdot using the Debian package dependency resolver. I'll let you know in a few days...
Here's my attempt at a solver / generator (Java backend, with a console frontend, a graphical frontend, and a j2me frontend):
http://cons.org.nz/~gringer/
I don't actually find sudoku puzzle *solvers* all that interesting, because they are able to do the solution by brute-force. Sudoku puzzle *generators*, on the other hand, tend to be more difficult, because one requirement for the traditional puzzles is that the puzzle must only have one solution. For puzzle generators that rely on brute-force for their solvers, this "only one solution" requirement is difficult to enforce.
Just to demonstrate this, see the following bug:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=351043
Ask me about repetitive DNA
I, personally, think it's pretty damned cool. Useless, but cool.
Which is why it is fun.
Now, if somebody makes a sudoku generator out of it, installing Linux will get a bit more fun.
Yeah, I know, Caldera Linux called, they want their install-time Tetris back.
Ignore this signature. By order.
I guess I'm just annoyed by these people who think that they are thinking, and am trying to burst their bubble.
I already learned all the chords on the guitar,
:-)
--and I'm just annoyed by these people who think that practicing will make me any better.
I already learned how to swim,
--and I'm just annoyed by these people who think that practicing will help me win an Olympic Medal.
Steven Hawking was simply born as a fully formed genius,
--and I'm just annoyed by these people who think that his thinking everyday has helped him achieve anything... but bursting your bubble.
Allow me to clarify parent and grand-parent for those of you who don't read articles:
As a proof-of-concept, I have written a hacky Python script, named debsudoku.py, that can convert ksudoku saved games into Packages files suitable for use with apt-get or aptitude.
(Source: TFA, at http://algebraicthunk.net/~dburrows/blog/entry/package-management-sudoku/)
Emphasis added. Note that dpkg doesn't solve the dependency puzzle, but apt-get, aptitude and other package managers do (including synaptic and gnome-app-install [the "Add/Remove" thing]). Hence the suggested badtitle (which I agree with).
The 'aptitude --help' bit and the super cow powers: if you run 'apt-get moo', you'll get a cowsay output (that is, an ascii-art cow saying "Have you mooed today"). Running 'aptitude moo' gets you "There are no Easter Eggs in this program". Running 'apt$GETITUDE --help' gives you "this apt[itude] does [not] have Super Cow Powers".
Just FYI ;)
Let me get that straight, if you can't solve a problem it's a bad problem ?
How would you feel then if someone else did solve the problem that you could not solve ? Is it still a poor puzzle then ?
I read what you are saying as 'I like sudoku, but only the simple ones because I'm not clever enough to hold more than a few permutations of it in my head'...
MP3 Search Engine
This is one of those instances of problems being the same though the fields of application are far apart.
It's exactly why algorithm development is such an interesting field.
Now to get the sudoku programs to be accepted as general dependency solvers for package managers :)
MP3 Search Engine
I think you just encountered a failed slashdot bot/AI.
Maybe it was written in dpkg too.
Jesus Christ. If you're going to be a game-code-nazi, get it right:
The [start] isn't part of the code, nor is the [select]. After the last [a] the code is entered, the code input is done. You can hit [select] multiple times after the code trying to decide between single player and co-op and it will still work.
"A witty saying proves nothing." - Voltaire
Second, I, and I'm sure I'm not alone on this, would rather write a program to solve sudoku than actually play sudoku. Some people love sudoku; I found it boring. Now writing software to solve a puzzle, that's interesting.
Sudoku's only interesting at the very hardest level, where you have to apply real logic to take it on. But then again, I've always found only the hardest problems are ever persistently interesting; if you're bored, do something more difficult and stop wasting both time and braincells.
"Little does he know, but there is no 'I' in 'Idiot'!"
I suppose nothing will beat Prolog with constraint logic programming to concisely solve Sudoku.
Actually, let me put the whole code here from the above blog post:
sudoku(P) :-
fd_domain(P,1,9),
Xs = [1,2,3,4,5,6,7,8,9],
row(P,Xs),
col(P,Xs),
unit(P,Xs),
fd_labeling(P).
row(_,[]). :-
row(P,[X|Xs])
setof(V,[C,I]^(for(C,1,9),I is (X-1)*9+C,nth(I,P,V)),L1),
fd_all_different(L1),
row(P,Xs).
col(_,[]). :-
col(P,[X|Xs])
setof(V,[R,I]^(for(R,1,9),I is (R-1)*9+X,nth(I,P,V)),L2),
fd_all_different(L2),
col(P,Xs).unit(_,[]).
unit(P,[U|Us]) :- // 3)*3+1, Re is Rs+2,
Cs is ((U-1) mod 3)*3+1, Ce is Cs+2,
Rs is ((U-1)
setof(V,[R,C,I]^(for(R,Rs,Re),for(C,Cs,Ce),I is (R-1)*9+C,nth(I,P,V)),L),
fd_all_different(L),
unit(P,Us).
I am sure that Steven Hawking, like the rest of us, gets smarter by thinking. But they do not get smarter by playing sudoku. Do you understand the difference?
Yes, I understand the difference between your opinion, and all the studies done on this subject. Do Brain Age and Sudoku really make you smarter?
Sudoku is NP complete. NP completeness means by definition that the problem can be expressed as a SAT problem (http://en.wikipedia.org/wiki/Boolean_satisfiability_problem). Basically NP complete means that you can see the problem as a black box and only check if a given solution solves the problem.
So the usual solution is *not* to write a new program for each np-complete problem, but to express them as a SAT problem and use one highly optimized SAT solver.
Obviously, dpkg does nothing more than solve a SAT problem (Solve: (( libA-v1 AND libA-v1-depencency) OR libA-v2 OR libA-v3) AND (libB-v1) and so on ), and therefore it can be used for any SAT problem.
WRONG!
You inverted the A and B.
Yes! That's another geek card today. Only 2 more until my geek upgrade.
I just pooped your party.
You do know that the "number of guesses you need to keep in your head" is the primary criterion for judging the difficulty rating for computer generated puzzles, don't you?
Can you be Even More Awesome?!
I wrote a Sudoku Solver in VB two years ago. That code you posted? Well, it's a hell of a lost more concise than mine! :(
Is this a rhetorical question?
All of this reemphasizes that sudoku is interesting as a mathematical solving exercise but pretty dull and boring as a human puzzle amusement. Give me a good crossword puzzle any time. Computers only create bad ones; it takes a human to make, and solve, a clever one.
You never need to guess, you just need to expand the list of patterns that you recognize. Everyone starts out with two:
1) Only one cell in this row, column or square where this value can be placed.
2) Only one value can go in this cell.
There are about twenty more of increasing levels of complexity which apply in fewer situations and are harder to spot. Although its only 10 lines of code to solve a sudoku with brute force, it is possible and much more interesting to solve all but the most insanely difficult ones using only the patterns. The Y-wing for example:
Where the lines indicate cells that "see" each other. "A" can be eliminated from cell x.
> Perhaps I'm misunderstanding you, but your method will still require a validity check. Many swaps will result in illegal puzzles. For instance, swap any two numbers on the same row or column.
No, you can use only those permutations which are guaranteed to create a legal puzzle.
Like I said, if you swap, you have to do it *everywhere* (swap all 4s to 6s and vice-versa, not just swap two of them on some column).
I'm sure there are other ways to permute it which guarantee that the puzzle remains valid, but I haven't proven them.
Assuming you can prove that your algorithm for permuting them always results in a valid puzzle, you don't need any check at all (though you might want one just to check for bugs).
Install a new glibc version and dpkg will faithfully remove all the packages that depended on the old version of glibc. Even if it means removing the linux kernel.
Only one B A is required....
http://en.wikipedia.org/wiki/Konami_Code
Minion is a *much* better too for this than even Prolog.
First rule of optimization: Don't.
NPC problems often exhibit "phase transitions" or pockets of the problem space that are easily solvable one way or the other. When the hard problems never occur in practice, keeping the code simple is the best way to go.
Once someone shows me a real-world, dpkg problem that would take unacceptably long to solve with the current dpkg solver (maybe one already exists), then we can talk about adding an optimized version. Until then, exponential dpkg problems should remain the province of jokes (as my post was originally intended).
Sudoku, in the way that it's being solved here and how most people think of it (with 9 digits and 3x3 boxes), is not NP-complete. Its board size is finite, so there are a bounded number of possibilities to try (fewer than (9!)^9), so there exists a constant-time algorithm (trying every one of the possibilities, of which there must be less than 9!^9). But if you want to generalize to nxn boards, that changes things considerably.
Only one B A is required....
http://en.wikipedia.org/wiki/Konami_Code
Not from my experience with Contra. Maybe in other Konami games, though.
It is dangerous to be right when the government is wrong.
I remember doing prolog at uni. It seemed extremely weird and difficult until gradually you have a Damascus/light-bulb moment(ie stop thinking procedurally) and then all the power is revealed.
You're simply incorrect. *shrug*
Actually playing Suduko, you employ what some would call "cheats", but the rest of us call logical rules. So all the PC does is speed up the solving process as they can perform thousands of logical checks in a fraction of a second, as opposed to us wetware, who tend to go cross-eyed around 3am.
How many of you guys have actually done this?
Sorry, but this is not actually "the best way of solving Sudoku". It's more like "mapping sudoku to a set theory library/tool/language syntax and then calling it to solve the problem". Unless your metric is "the least amount of lines containing clear syntax", your solution is one of the worst ones I have ever seen.
A real, nice way of solving Sudoku must include your own algorithms. Even better: custom tailored ones, instead of just copy-pasting solutions of classic problems.
Sure, mapping is nice and stuff (and some people will like the fact that you used only a few criptic lines to do that), but it's just... mapping. The real fun and optimization resides on doing the algorithm yourself.
Really? I'll take if from you if you insist, it's been over a decade and a half since I've played that game. Maybe I'll start looking for an emulator and a ROM for linux, I'd really like to play it again.
It is dangerous to be right when the government is wrong.
I once had to write a code to solve the 2D Ising model with a Monte Carlo method, in this case basically a simulated annealing algorithm. I eventually realised I could apply it to sudoku solving, but I could only ever get it to solve rows and columns, and then it would get stuck in a fairly ordered solution, but one where more disorder was required to get into a more ordered state. I began to try and add random fluctuations to get it to work, but I eventually abandoned it to work on the actual physics project. It would probably make an interesting exercise for one of you...
xterm -n 8
Maybe you kid, but it is interesting that APT seems to be able to handle more complex dependency graphs than RPM. What I've never understood is why it needs to. What more is there to package dependency resolution than a simple recursive procedure? In pseudocode:
Sounds fun!
Solve this Sudoku before the time runs out or your partitions will be randomly reassigned to different mount points.
Damn PKers.
This year in my Artificial Intelligence class we were asked to right a program in java that would solve an inputed sudoku puzzle, after some internet googling and alot of papers read, me and my group partner discovered that it is considered a really bad way of solving this kind of puzzles, so we started to try to find a way that would make it possible to solve a puzzle in a short period of time. After a lot of hard work and big disapointments, even some teachers started saying that solving a easy puzzle in 20 000 generations would be considered good, but I didnt give up and in the last they I came with a solution that would use the power of constrains and only after it, genetic , what I did was create an individual where in each cell could only be put numbers that weren't on the same row column or sub matrix this made the number of possible individuals lower by a big chunk and we were able to solve the easy puzzle on 10 generations max and the medium/hard difficulty in 100. BUT even with this algorithm we weren't able to solve some of the harder puzzles like this one [url=http://en.wikipedia.org/wiki/Algorithmics_of_sudoku#Exceptionally_difficult_Sudokus_.28Hardest_Sudokus.29]this one[\url](wikipedia) in a constant speed, come times it would solve under 100 gen other only after more than 20000, but this are sudoku puzzle that bypass any kind of logic and are considered impossible to be solved using human logic cause... So I guess I would like to see how dkpg would handle them ;)
...to readapt Debian's... hehe! "re-adapt", get it? ;-)
I am an ACCA student. Got a query on Accountancy/Finance? Maybe I can help!
Yes! That's another geek card today. Only 2 more until my geek upgrade
A frontal lobe?
MMO Vampire Role Playing
This begs the questions...
1) Can you create the next cool game using logic from existing systems?
2) Can you create the next cool system using game logic?
Oh, I almost forgot ... profit
He would've been right for TMNT3 though.
Two points:
Writing an algorithm is "mapping". You are a good programmer when you do this automatically, at least for simple domains. If you're struggling, you need to take another look at the "real" problem, since you are missing something.
Sudoku is a constraint-based arithmetic game. Constraint-based programming is the natural way to solve the problem. (though functional programming might not be a terrible approach -- resolution is "just" beta reduction on truth functions) This is what you were missing.