summaryrefslogtreecommitdiff
path: root/algorithms/sudoku.py
blob: 1fa18243cbcdf27d27fe7714b97dc02b00e12fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python2


block = [ [1, 2, 3],
          [1, 2, 3],
          [1, 2, 3]
]

print "bs"
sums = []
for i in [0,1,2]:
  sumh = 0
  sumv = 0
  for j in [0,1,2]:
     sumh += block[i][j]
     sumv += block[j][i]
  print "horiz %d, vert %d"%(sumh, sumv)
  sums += [sumv, sumh]

if sum(sums) != 6*9: print "ohoh"

# ---

#                                           0          1          2
s = [ [1, 2, 3,   4, 5 ,6,   7, 8, 9], #00 01 02   03 04 05   06 07 08
      [1, 2, 3,   4, 5, 6,   7, 8, 9], #10 11 12   13 14 15   16 17 18
      [1, 2, 3,   4, 5, 6,   7, 8, 9], #20 21 22   23 24 25   26 27 28

                                       #    3          4          5
      [1, 2, 3,   4, 5, 6,   7, 8, 9], #30 31 32   33 34 35   36 37 38
      [1, 2, 3,   4, 5, 6,   7, 8, 9], #40 41 42   43 44 45   46 47 48
      [1, 2, 3,   4, 5, 6,   7, 8, 9], #50 51 52   53 54 55   56 57 58

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

"""
0 1 2
2 3 4
4 5 6


0 1 2  3 4 5  6 7 8
1 2 3  4 5 6  7 8 9
2 3 4  5 6 7  8 9 10

3 4 5  6 7 8  9 10 11
4 5 6  7 8 9 10 11 12


i = 0 1 2 => 0 1 2
i = 3 4 5 => 3 4 5
i = 6 7 8 => 6 7 8

j = 0 1 2 => 0 3 6
j = 3 4 5 => 1 4 7
j = 6 7 8 => 2 5 8


i=0
 j=0 1 2 3 4 5 6 7 8 9
i=1
 j=1 2 3 4 5 6 7 8 8 9
"""

blocksums = [0 for i in range(0,9)]
third = 0
block = 0
for i in range(0,9):
  sumh = 0
  sumv = 0
  if i > 0 and i%3 == 0:
    third += 1
  block = third*3
  for j in range(0,9):
    if j > 0 and j%3 == 0:
      block += 1
    print "%d %d, %d"%(i,j,block)
    sumh += s[i][j]
    sumv += s[j][i]
    blocksums[block] = s[i][j]
  sums += [sumh, sumv]
  print "---"

sums += blocksums

if sum(sums) != 45*len(sums): print "ohoh"