processing.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package solver
  2. import (
  3. "log"
  4. "strconv"
  5. "time"
  6. )
  7. func (solver *Solver) populate_blocks() {
  8. defer solver.timeTrack(time.Now(), "Populated blocks")
  9. log.Println("Populating blocks")
  10. solver.find_blocks(&solver.row1, &solver.row1s)
  11. solver.find_blocks(&solver.row2, &solver.row2s)
  12. solver.find_blocks(&solver.row3, &solver.row3s)
  13. solver.find_blocks(&solver.row4, &solver.row4s)
  14. solver.find_blocks(&solver.row5, &solver.row5s)
  15. solver.find_blocks(&solver.row6, &solver.row6s)
  16. solver.find_blocks(&solver.row7, &solver.row7s)
  17. solver.find_blocks(&solver.row8, &solver.row8s)
  18. solver.find_blocks(&solver.row9, &solver.row9s)
  19. // This calculates and stores the total number of solutions to validate.
  20. solver.iter = int64(len(solver.row1s)) * int64(len(solver.row2s)) * int64(len(solver.row3s)) * int64(len(solver.row4s)) * int64(len(solver.row5s)) * int64(len(solver.row6s)) * int64(len(solver.row7s)) * int64(len(solver.row8s)) * int64(len(solver.row9s))
  21. }
  22. func (solver *Solver) find_blocks(row *string, rows *[]int) {
  23. // Declare selection
  24. var selection []int
  25. var curr_blocks []int
  26. func_row := *row
  27. for letter := range func_row {
  28. if len(selection) == 0 {
  29. curr_blocks = solver.blocks
  30. } else {
  31. curr_blocks = selection
  32. selection = nil
  33. }
  34. for _, block := range curr_blocks {
  35. curr_row := strconv.Itoa(block)
  36. if func_row[letter] == curr_row[letter] {
  37. found_row, _ := strconv.Atoi(curr_row)
  38. selection = append(selection, found_row)
  39. }
  40. if func_row[letter] == '0' {
  41. found_row, _ := strconv.Atoi(curr_row)
  42. selection = append(selection, found_row)
  43. }
  44. } // End for-loop
  45. } // End for-loop
  46. *rows = selection
  47. }
  48. func (solver *Solver) check_combinations() {
  49. for rows1_index := range solver.row1s {
  50. for rows2_index := range solver.row2s {
  51. for rows3_index := range solver.row3s {
  52. for rows4_index := range solver.row4s {
  53. for rows5_index := range solver.row5s {
  54. for rows6_index := range solver.row6s {
  55. for rows7_index := range solver.row7s {
  56. for rows8_index := range solver.row8s {
  57. for rows9_index := range solver.row9s {
  58. go solver.routine_validator(rows1_index, rows2_index, rows3_index, rows4_index, rows5_index, rows6_index, rows7_index, rows8_index, rows9_index)
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. 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) {
  70. // solver.counter = solver.counter + 1
  71. solver.counter.Add(1)
  72. 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]) {
  73. 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]))
  74. }
  75. }
  76. func (solver *Solver) tracker() {
  77. defer solver.timeTrack(time.Now(), "Validated solutions")
  78. log.Println("Validating solutions")
  79. // Determine if the main-loop is done
  80. var done bool
  81. // Tracking progress in percentages
  82. var percentage float32
  83. // Tracking progress in validated solutions
  84. var track int
  85. // Tracking the rate, starting point
  86. var rate_start int64
  87. // Tracking the rate, difference between previous iterations
  88. var rate_diff int64
  89. // Tracking duration
  90. var timer_start = time.Now()
  91. // Prevent division-by-zero error when establishing `rate_diff`
  92. time.Sleep(time.Second)
  93. // Estimation how long it will take
  94. var est_fin string
  95. // for solver.iter != solver.counter { // Start for-loop
  96. for !done {
  97. // Determine how far we are.
  98. percentage = (float32(solver.counter.Load()) / (float32(solver.iter) / 100))
  99. // Reset the loop
  100. rate_diff = solver.counter.Load() - rate_start
  101. if track <= int(percentage) || rate_diff == 0 { // Start if-statement
  102. // Make sure something happened, making rate_start the only reliable variable
  103. if rate_diff == 0 {
  104. percentage = 100
  105. solver.counter.Store(solver.iter)
  106. done = true
  107. }
  108. timer_elapsed := time.Since(timer_start)
  109. solver.rates = append(solver.rates, rate_diff)
  110. rate_avg := solver.calc_avg()
  111. // Estimate when this is finished
  112. if rate_diff == 0 {
  113. est_fin = "N/A"
  114. } else {
  115. est_fin = solver.secondsToHuman((solver.iter - solver.counter.Load()) / rate_avg)
  116. }
  117. // Printing the progress
  118. log.Println("Processing: " + strconv.Itoa(int(percentage)) + "% (" + strconv.FormatInt(solver.counter.Load(), 10) + "/" + strconv.Itoa(int(solver.iter)) + "); Rate: " + strconv.FormatInt(rate_diff, 10) + "/sec for " + timer_elapsed.String() + "; Time left (est.): " + est_fin)
  119. // After we are done printing, exit this for-loop
  120. if percentage == 100 {
  121. break
  122. }
  123. // Wrap up the loop or break
  124. if int(percentage) > track {
  125. track = int(percentage)
  126. } else {
  127. track = track + 1
  128. }
  129. timer_start = time.Now()
  130. }
  131. // Resert the rate counter
  132. rate_start = solver.counter.Load()
  133. // Sleep for a second
  134. time.Sleep(1 * time.Second)
  135. } // End for-loop
  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. }
  180. func (solver *Solver) calc_avg() (avg int64) {
  181. var avg_sum int64
  182. for _, value := range solver.rates {
  183. avg_sum += value
  184. }
  185. avg = avg_sum / int64(len(solver.rates))
  186. return
  187. }