processing.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package solver
  2. import (
  3. "fmt"
  4. "runtime"
  5. "strconv"
  6. )
  7. func (solver *Solver) find_blocks(row *string, rows *[]int) {
  8. // Declare selection
  9. var selection []int
  10. var curr_blocks []int
  11. func_row := *row
  12. // fmt.Println(row)
  13. for letter := range func_row {
  14. if len(selection) == 0 {
  15. curr_blocks = solver.blocks
  16. } else {
  17. curr_blocks = selection
  18. selection = nil
  19. }
  20. for block := range curr_blocks {
  21. curr_row := strconv.Itoa(curr_blocks[block])
  22. if func_row[letter] == curr_row[letter] {
  23. found_row, _ := strconv.Atoi(curr_row)
  24. selection = append(selection, found_row)
  25. }
  26. if func_row[letter] == '0' {
  27. found_row, _ := strconv.Atoi(curr_row)
  28. selection = append(selection, found_row)
  29. }
  30. } // End for-loop
  31. } // End for-loop
  32. *rows = selection
  33. }
  34. func (solver *Solver) check_combinations() {
  35. // for rows1_index := range solver.row1s {
  36. // solver.wg.Add(1)
  37. // go solver.routine_row1(rows1_index)
  38. // }
  39. // solver.wg.Wait()
  40. for rows1_index := range solver.row1s {
  41. for rows2_index := range solver.row2s {
  42. for rows3_index := range solver.row3s {
  43. for rows4_index := range solver.row4s {
  44. for rows5_index := range solver.row5s {
  45. for rows6_index := range solver.row6s {
  46. for rows7_index := range solver.row7s {
  47. for rows8_index := range solver.row8s {
  48. defer solver.wg.Done()
  49. solver.wg.Add(1)
  50. go solver.routine_row8(rows1_index, rows2_index, rows3_index, rows4_index, rows5_index, rows6_index, rows7_index, rows8_index)
  51. // for rows9_index := range solver.row9s {
  52. // go solver.routine_validator(rows1_index, rows2_index, rows3_index, rows4_index, rows5_index, rows6_index, rows7_index, rows8_index, rows9_index)
  53. // // }
  54. // }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. func (solver *Solver) routine_row1(index1 int) {
  65. defer solver.wg.Done()
  66. for index2 := range solver.row2s {
  67. solver.wg.Add(1)
  68. go solver.routine_row2(index1, index2)
  69. }
  70. }
  71. func (solver *Solver) routine_row2(index1 int, index2 int) {
  72. defer solver.wg.Done()
  73. for index3 := range solver.row3s {
  74. solver.wg.Add(1)
  75. go solver.routine_row3(index1, index2, index3)
  76. }
  77. }
  78. func (solver *Solver) routine_row3(index1 int, index2 int, index3 int) {
  79. defer solver.wg.Done()
  80. for index4 := range solver.row4s {
  81. solver.wg.Add(1)
  82. go solver.routine_row4(index1, index2, index3, index4)
  83. }
  84. }
  85. func (solver *Solver) routine_row4(index1 int, index2 int, index3 int, index4 int) {
  86. defer solver.wg.Done()
  87. for index5 := range solver.row5s {
  88. solver.wg.Add(1)
  89. go solver.routine_row5(index1, index2, index3, index4, index5)
  90. }
  91. }
  92. func (solver *Solver) routine_row5(index1 int, index2 int, index3 int, index4 int, index5 int) {
  93. defer solver.wg.Done()
  94. for index6 := range solver.row6s {
  95. solver.wg.Add(1)
  96. go solver.routine_row6(index1, index2, index3, index4, index5, index6)
  97. }
  98. }
  99. func (solver *Solver) routine_row6(index1 int, index2 int, index3 int, index4 int, index5 int, index6 int) {
  100. defer solver.wg.Done()
  101. for index7 := range solver.row7s {
  102. solver.wg.Add(1)
  103. go solver.routine_row7(index1, index2, index3, index4, index5, index6, index7)
  104. }
  105. }
  106. func (solver *Solver) routine_row7(index1 int, index2 int, index3 int, index4 int, index5 int, index6 int, index7 int) {
  107. defer solver.wg.Done()
  108. for index8 := range solver.row8s {
  109. solver.wg.Add(1)
  110. go solver.routine_row8(index1, index2, index3, index4, index5, index6, index7, index8)
  111. }
  112. }
  113. func (solver *Solver) routine_row8(index1 int, index2 int, index3 int, index4 int, index5 int, index6 int, index7 int, index8 int) {
  114. defer solver.wg.Done()
  115. for index9 := range solver.row9s {
  116. solver.wg.Add(1)
  117. go solver.routine_row9(index1, index2, index3, index4, index5, index6, index7, index8, index9)
  118. }
  119. }
  120. func (solver *Solver) routine_row9(index1 int, index2 int, index3 int, index4 int, index5 int, index6 int, index7 int, index8 int, index9 int) {
  121. defer solver.wg.Done()
  122. solver.wg.Add(1)
  123. go solver.routine_validator(index1, index2, index3, index4, index5, index6, index7, index8, index9)
  124. }
  125. func (solver *Solver) routine_validator(rows1_index int, rows2_index int, rows3_index int, rows4_index int, rows5_index int, rows6_index int, rows7_index int, rows8_index int, rows9_index int) {
  126. var percentage float32
  127. if solver.validate_combination(solver.row1s[rows1_index], solver.row2s[rows2_index], solver.row3s[rows3_index], solver.row4s[rows4_index], solver.row5s[rows5_index], solver.row6s[rows6_index], solver.row7s[rows7_index], solver.row8s[rows8_index], solver.row9s[rows9_index]) {
  128. solver.solutions = append(solver.solutions, solver.render_combination(solver.row1s[rows1_index], solver.row2s[rows2_index], solver.row3s[rows3_index], solver.row4s[rows4_index], solver.row5s[rows5_index], solver.row6s[rows6_index], solver.row7s[rows7_index], solver.row8s[rows8_index], solver.row9s[rows9_index]))
  129. }
  130. solver.counter = solver.counter + 1
  131. if solver.counter%1000000 == 0 {
  132. percentage = (float32(solver.counter) / (float32(solver.iter) / 100))
  133. fmt.Println("Processing:", percentage, "%; Procs:", runtime.NumGoroutine())
  134. }
  135. solver.wg.Done()
  136. }
  137. func (solver *Solver) validate_combination(row1 int, row2 int, row3 int, row4 int, row5 int, row6 int, row7 int, row8 int, row9 int) bool {
  138. var retval bool
  139. retval = true
  140. row1s := strconv.Itoa(row1)
  141. row2s := strconv.Itoa(row2)
  142. row3s := strconv.Itoa(row3)
  143. row4s := strconv.Itoa(row4)
  144. row5s := strconv.Itoa(row5)
  145. row6s := strconv.Itoa(row6)
  146. row7s := strconv.Itoa(row7)
  147. row8s := strconv.Itoa(row8)
  148. row9s := strconv.Itoa(row9)
  149. for index := range 9 {
  150. if row1s[index] == row2s[index] || row1s[index] == row3s[index] || row1s[index] == row4s[index] || row1s[index] == row5s[index] || row1s[index] == row6s[index] || row1s[index] == row7s[index] || row1s[index] == row8s[index] || row1s[index] == row9s[index] {
  151. retval = false
  152. }
  153. if row2s[index] == row1s[index] || row2s[index] == row3s[index] || row2s[index] == row4s[index] || row2s[index] == row5s[index] || row2s[index] == row6s[index] || row2s[index] == row7s[index] || row2s[index] == row8s[index] || row2s[index] == row9s[index] {
  154. retval = false
  155. }
  156. if row3s[index] == row1s[index] || row3s[index] == row2s[index] || row3s[index] == row4s[index] || row3s[index] == row5s[index] || row3s[index] == row6s[index] || row3s[index] == row7s[index] || row3s[index] == row8s[index] || row3s[index] == row9s[index] {
  157. retval = false
  158. }
  159. if row4s[index] == row1s[index] || row4s[index] == row2s[index] || row4s[index] == row3s[index] || row4s[index] == row5s[index] || row4s[index] == row6s[index] || row4s[index] == row7s[index] || row4s[index] == row8s[index] || row4s[index] == row9s[index] {
  160. retval = false
  161. }
  162. if row5s[index] == row1s[index] || row5s[index] == row2s[index] || row5s[index] == row3s[index] || row5s[index] == row4s[index] || row5s[index] == row6s[index] || row5s[index] == row7s[index] || row5s[index] == row8s[index] || row5s[index] == row9s[index] {
  163. retval = false
  164. }
  165. if row6s[index] == row1s[index] || row6s[index] == row2s[index] || row6s[index] == row3s[index] || row6s[index] == row4s[index] || row6s[index] == row5s[index] || row6s[index] == row7s[index] || row6s[index] == row8s[index] || row6s[index] == row9s[index] {
  166. retval = false
  167. }
  168. if row7s[index] == row1s[index] || row7s[index] == row2s[index] || row7s[index] == row3s[index] || row7s[index] == row4s[index] || row5s[index] == row6s[index] || row7s[index] == row6s[index] || row7s[index] == row8s[index] || row7s[index] == row9s[index] {
  169. retval = false
  170. }
  171. if row8s[index] == row1s[index] || row8s[index] == row2s[index] || row8s[index] == row3s[index] || row8s[index] == row4s[index] || row8s[index] == row5s[index] || row8s[index] == row6s[index] || row8s[index] == row7s[index] || row8s[index] == row9s[index] {
  172. retval = false
  173. }
  174. if row9s[index] == row1s[index] || row9s[index] == row2s[index] || row9s[index] == row3s[index] || row9s[index] == row4s[index] || row9s[index] == row5s[index] || row9s[index] == row6s[index] || row9s[index] == row7s[index] || row9s[index] == row8s[index] {
  175. retval = false
  176. }
  177. }
  178. return retval
  179. }