Slashdot Mirror


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.

46 of 190 comments (clear)

  1. Uh-oh by Yvan256 · · Score: 2, Funny

    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.

    1. Re:Uh-oh by Koiu+Lpoi · · Score: 3, Interesting

      Then try this on for size: I believe that "sudoku" is a mass noun, much like all nouns in Japanese. In other words, you can't say "two sudokus" any more than you can say "two softwares" or "two mashed potatoes". You need a counter word, such as "two sudoku puzzles", which you'd have to use (something very similar) in Japanese anyways.

      And besides, let's be honest. "Sudokus" sounds retarded.

    2. Re:Uh-oh by hublan · · Score: 2, Insightful

      But you can count anything you want in English, as long as the context is clear. If you treat the noun as countable, it's countable.

      I'll take four milks and three beefs.

      --
      My spoon is too big.
  2. Re:Cheat code for even Sudoku?? by pizzach · · Score: 5, Funny

    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.
  3. Well, it's ultimately a logic puzzle. by Shag · · Score: 5, Insightful

    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.
    1. Re:Well, it's ultimately a logic puzzle. by tukang · · Score: 3, Informative

      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.

      How did this get modded insightful? Just because numbers don't have to be used doesn't mean it's not math.

      Sudoku is a set theory problem

    2. Re:Well, it's ultimately a logic puzzle. by WiseWeasel · · Score: 2, Insightful

      Keeps your logic skills sharp. It's good mental exercise, in the vein of Brain Age and such... Gotta keep that noggin fit, especially if your day job isn't challenging you enough.

      --
      "I like systems, their application excepted", George Sand (French)
    3. Re:Well, it's ultimately a logic puzzle. by azgard · · Score: 4, Insightful

      Actually, Sudoku is a http://en.wikipedia.org/wiki/Vertex_coloring for a special graph. From mathematics perspective, it is thus pretty boring, although that doesn't mean it cannot be fun.

  4. Re:Cheat code for even Sudoku?? by Anonymous Coward · · Score: 4, Funny

    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.

  5. Re:Cheat code for even Sudoku?? by Anonymous Coward · · Score: 5, Informative

    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.

  6. Re:Cheat code for even Sudoku?? by jlarocco · · Score: 5, Insightful

    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.

  7. Splitting Hairs by Nymz · · Score: 2, Interesting

    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.

    1. Re:Splitting Hairs by whatnotever · · Score: 5, Interesting

      Sudoku can be solved by trying values in cells until a conflict is reached and backtracking to try other assignments. That's the brute-force approach.

      Most sudoku puzzles can be solved via implication, however. There is no need to "try" anything. Certain configurations of values in some cells can imply values in other cells. As a very simple example, consider a row that has all cells filled but one. The value of that unfilled cell is implied and can be filled in without having to try any other values. This is a basic example, but clearly more complex ones exist. This is essentially how people solve the puzzles, and I believe it is what the grandparent was describing.

      However, I do not believe that the grandparent is correct in stating that these methods solve sudokus in a fraction of the time of the brute force method if you allow for standard optimizations of the brute force method as developed for constraint processing (CP) or Boolean satisfiability (SAT) solvers. But then again, many of those optimizations are similar to the "clever logic and elegant methods," especially those that perform propagation and follow implications.

      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.

      Don't mod me down if you disagree. If you disagree, consider writing a retort instead.

      It would have been nice if you had written something backing up your own claim as well.

    2. Re:Splitting Hairs by maxume · · Score: 3, Informative

      The "Propagating Constraints" section of this article is quite a bit less brute force than the "search" section:

      http://norvig.com/sudoku.html

      --
      Nerd rage is the funniest rage.
    3. Re:Splitting Hairs by jonaskoelker · · Score: 2, Insightful

      However, I do not believe that the grandparent is correct in stating that these methods solve sudokus in a fraction of the time of the brute force method if you allow for [...]

      Well, run some tests then ;) Seriously, though: as you point out yourself, if you add some simple heuristics to the brute-forcing it ceases being brute-force. Guess what, the heuristics for solving Sudoku problems (you've pointed one out yourself) eventually run out, and you have to guess and backtrack or brute-force in some other way.

      If we compare the completely vanilla brute force (that's 9^(n^2) tries where n^2 is the number of squares) with the heuristics-plus-brute-force, you may get something better than a constant factor time improvement (that is, time is reduced with more than just a fixed fraction). However, the Sudoku problem (for arbitrary-sized boards) is NP-complete, so you can't get down to polynomial time.

      Although typical Sudoku puzzles (with 9×9 grid and 3×3 regions) can be solved quickly by computer, the generalization to larger grids is known to be NP-complete. Various optimization methods have been proposed for large grids.

      (Source: http://en.wikipedia.org/wiki/Sudoku)

  8. Re:Cheat code for even Sudoku?? by Anonymous Coward · · Score: 4, Funny

    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?

  9. Aptitude rather than Dpkg by Asuranceturix · · Score: 3, Informative

    And he didn't even use Super Cow Powers to do it!

    1. Re:Aptitude rather than Dpkg by entrylevel · · Score: 2

      Finally, someone who GETS it.

      I wanted to comment "Let's see rpm do that!" but before I could click, I realized that dpkg by itself cannot solve puzzles of any kind.

      To the idiot who modded this "overrated," please pull your head out of your ass and type "aptitude --help" when you get a chance.

      To everyone else, please tag this article "badtitle."

      --
      Karma: Incomprehensible (Mostly affected by posting at +5, reading at -1, and metamoderating everything unfair.)
  10. Re:Cheat code for even Sudoku?? by _xeno_ · · Score: 4, Informative

    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.
  11. Re:Cheat code for even Sudoku?? by Drakonik · · Score: 5, Insightful

    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.

  12. Re:Cheat code for even Sudoku?? by Drakonik · · Score: 4, Insightful

    Exactly. This guy doesn't care about the sudoku puzzle, he cares about the programming puzzle.

  13. I Disagree and I Agree by Nymz · · Score: 5, Informative

    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.

    1. Re:I Disagree and I Agree by asc99c · · Score: 2, Informative

      It's very rare that I have found the need to guess in a Sudoku. I've never seen a printed one in a newspaper where that was necessary - even the 'diabolical' ones.

      Certainly I've made mistakes on some, and a couple of times I've run out of ideas and had to make a guess, but on running it through my own program, it's got the solution without taking guesses.

      There is a good online solver at http://www.sudokusolver.co.uk/ that explains how it solves any Sudoku that you enter. This has a couple more methods to try than my own solver program, but a lot less than some. I've seen all kinds of complicated algorithms, but I think many of them are never required if you've properly applied the simpler ones.

      For a real challenge, this page has a number of grids that couldn't be solved using a proper algorithm (other than guess and check):
      http://www.sudokusolver.co.uk/grids_nologic.html

  14. Elegant methods by SeanAhern · · Score: 4, Insightful

    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.

  15. Brute force? by gehrehmee · · Score: 2, Insightful

    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
  16. Proof Positive by fabs64 · · Score: 2, Funny

    Let's seem yum do THAT!

    I kid I kid.

    1. Re:Proof Positive by mabinogi · · Score: 2, Funny

      It probably could, but you'd have to wait ten times longer than it took to solve the problem for it to download all the headers first.

      --
      Advanced users are users too!
  17. Re:Cheat code for even Sudoku?? by omeomi · · Score: 2, Interesting

    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...

  18. Software used for weird purposes? by suck_burners_rice · · Score: 4, Insightful

    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!
  19. Re:Cheat code for even Sudoku?? by hedwards · · Score: 2, Funny

    Hey now, there's a reason why they're called "joysticks."

  20. Re:Cheat code for even Sudoku?? by omeomi · · Score: 3, Insightful

    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...

  21. Re:Cheat code for even Sudoku?? by FireFlie · · Score: 2, Interesting

    Interesting? Writing a SAT program to solve sudoku takes less than five minutes. This is not an interesting puzzle for a computer.

  22. Make by michaelmalak · · Score: 4, Funny

    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.)

  23. Dpkg in NP hard?! by mdmkolbe · · Score: 2, Insightful

    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.

  24. Just permute a valid solution! by Anonymous Coward · · Score: 3, Interesting

    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.

    1. Re:Just permute a valid solution! by FireFlie · · Score: 2, Informative

      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.

  25. Comment removed by account_deleted · · Score: 2, Insightful

    Comment removed based on user account deletion

  26. Re:Cheat code for even Sudoku?? by Z00L00K · · Score: 2, Interesting

    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.
  27. Re:Cheat code for even Sudoku?? by DMUTPeregrine · · Score: 3, Informative

    That's the 2-player code.
    Up-Up-Down-Down-Left-Right-Left-Right-A-B-(Start) is single player contra.

    --
    Not a sentence!
  28. Time to plug myself by gringer · · Score: 2, Interesting

    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
  29. Practice Makes Perfect by Nymz · · Score: 2, Insightful

    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. :-)

  30. Mod up P, GP and P=NP: choose two by jonaskoelker · · Score: 2, Informative

    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 ;)

  31. Re:Cheat code for even Sudoku?? by jacquesm · · Score: 2, Interesting

    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'...

  32. Re:Cheat code for even Sudoku?? by seasunset · · Score: 5, Interesting

    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]) :-
      Cs is ((U-1) mod 3)*3+1, Ce is Cs+2,
      Rs is ((U-1) // 3)*3+1, Re is Rs+2,
      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).

  33. Re:Cheat code for even Sudoku?? by Bozzio · · Score: 4, Funny

    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.
  34. 3x3 sudoku is constant-time by AySz88 · · Score: 2, Informative

    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.