-2

I've got a hash like this:

Each key represents a row number on a Sudoku board.

Each array in a value represents a possible solution for that row

possibilities = {
  1 => [[1, 1, 1], [1, 1, 1], [1, 1, 1]],
  2 => [[2, 2, 2], [3, 3, 3], [4, 4, 4]],
  3 => [[2, 2, 2], [3, 3, 3], [4, 4, 4]],

  ...

  9 => [[2, 2, 2], [3, 3, 3], [4, 4, 4]]
}

I want to try every single row until we find a combination of rows that solves the puzzle. I'm not quite sure how to do the permutation.

Any ideas?

Dylan Richards
  • 796
  • 3
  • 9
  • 26
  • why are you trying to solve sudoku this way? – Jared Jul 08 '16 at 21:22
  • 2
    This is going to be incredibly inefficient, and there are known algorithms out there to solve Sudoku. I would not solve it this way, take a look here for the number of permutations http://stackoverflow.com/questions/7023071/number-of-possible-sudoku-puzzles – user1875195 Jul 08 '16 at 21:23
  • Why not? It seemed straightforward to me. Each has a finite number of solutions. So I figured I could just generate every possible row and then brute force it. – Dylan Richards Jul 08 '16 at 21:24
  • Because there are way too many possible solutions.... – Erik Godard Jul 08 '16 at 21:25
  • @DylanRichards: This going to be way too many combinations. Think about it: what is the point of checking a row [2, 2, 2], [2, 2, 2], [2, 2, 2] if that row is already invalid? – Jared Jul 08 '16 at 21:26
  • Thanks all. I understand my approach is inefficient. I'm just having a little fun here. It was an interesting exercise to find every possible row solution. I'm inquiring about figuring out a solution with the data I have. – Dylan Richards Jul 08 '16 at 21:28
  • Do we downvote on StackOverflow for inefficiency now? – Dylan Richards Jul 08 '16 at 21:29
  • 2
    Come one, people, the question is not about solving sudoku but about finding all possible permutations. Sudoku is the context. While it is valid and necessary to offer more efficient solution to the context, the question as it stated is valid as well. – Nic Nilov Jul 08 '16 at 21:30
  • As this example will generate 10^27 permutations, the solution needs to be lazily evaluated. – Nic Nilov Jul 08 '16 at 21:39
  • Welcome to Stack Overflow. We'd like to see your effort toward solving the problem. Without that it looks like you have a problem but want us to solve it, which isn't how SO works. Please read "[ask]" including the linked pages. – the Tin Man Jul 08 '16 at 23:42
  • My efforts have led me to this point. I decided to remove other code to avoid confusion. – Dylan Richards Jul 08 '16 at 23:44
  • @Nic, how did you get that number? I believe it should be `9!*9! =~> 131 * 10^9`. – Cary Swoveland Jul 09 '16 at 00:58
  • @CarySwoveland I think I was wrong, but I'm not sure if your approach is correct either. If we flatten the matrix to one-dimensional array it will have the length of 81. Each of the elements can take one of 10 values, which makes it 10^81 permutations. – Nic Nilov Jul 09 '16 at 01:15
  • @Nic, yes, we're both wrong. Here's a way of looking at it. There are 9 1's, 9 2's, etc. Suppose each of the nine 1's is a different colour, each 2 is a different colour, etc. Number the squares 1 to 81. There are 81! ways of filling the 81 squares when colour is a distinguishing characteristic. Now 9! of these permutations differ only by the order of the 1's. Thus, there are 81!/9! permutations that are colour-indifferent for the 1's, but colour-aware for the other 8 digits. It follows that there are 81!/(9*9!) permutations that are colour indifferent, which is what we want. – Cary Swoveland Jul 09 '16 at 03:25
  • @nic, I should have commented on your argument for 10^81 permuations.. Aside from the fact that `10` should be `9` [ :-) ], it's not the case that each of 81 elements can take one of 9 values. For example, all 81 values cannot equal 1. Nor can there be 13 4's, 5 8's and so on. – Cary Swoveland Jul 09 '16 at 06:07
  • @CarySwoveland Sure, my approach was simplistic and actually incorrect again. When taking into account the rules, the calculation becomes more complex. In fact, [here is a link](http://www.math.cornell.edu/~mec/Summer2009/Mahmood/Count.html) with just that. The answer posted there is 6.671×10^21. – Nic Nilov Jul 09 '16 at 10:49

1 Answers1

1

I am not sure this is the best ways to solve sudokus. But I think what you are looking for is:

http://ruby-doc.org/core-2.2.0/Array.html#method-i-permutation

Albin
  • 2,912
  • 1
  • 21
  • 31