processing.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package solver
  2. import (
  3. "log"
  4. "strconv"
  5. "time"
  6. )
  7. func (solver *Solver) populateBlocks() {
  8. defer solver.timeTrack(time.Now(), "Populated blocks")
  9. log.Println("Populating blocks")
  10. solver.findBlocks(&solver.row1, &solver.row1s)
  11. solver.findBlocks(&solver.row2, &solver.row2s)
  12. solver.findBlocks(&solver.row3, &solver.row3s)
  13. solver.findBlocks(&solver.row4, &solver.row4s)
  14. solver.findBlocks(&solver.row5, &solver.row5s)
  15. solver.findBlocks(&solver.row6, &solver.row6s)
  16. solver.findBlocks(&solver.row7, &solver.row7s)
  17. solver.findBlocks(&solver.row8, &solver.row8s)
  18. solver.findBlocks(&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) findBlocks(row *string, rows *[]int) {
  23. // Declare selection
  24. var selection []int
  25. var currBlocks []int
  26. funcRow := *row
  27. for letter := range funcRow {
  28. if len(selection) == 0 {
  29. currBlocks = solver.blocks
  30. } else {
  31. currBlocks = selection
  32. selection = nil
  33. }
  34. for _, block := range currBlocks {
  35. currRow := strconv.Itoa(block)
  36. if funcRow[letter] == currRow[letter] {
  37. foundRow, _ := strconv.Atoi(currRow)
  38. selection = append(selection, foundRow)
  39. }
  40. if funcRow[letter] == '0' {
  41. foundRow, _ := strconv.Atoi(currRow)
  42. selection = append(selection, foundRow)
  43. }
  44. } // End for-loop
  45. } // End for-loop
  46. *rows = selection
  47. }
  48. func (solver *Solver) checkCombinations() {
  49. for rows1Index := range solver.row1s {
  50. for rows2Index := range solver.row2s {
  51. for rows3Index := range solver.row3s {
  52. for rows4Index := range solver.row4s {
  53. for rows5Index := range solver.row5s {
  54. for rows6Index := range solver.row6s {
  55. for rows7Index := range solver.row7s {
  56. for rows8Index := range solver.row8s {
  57. for rows9Index := range solver.row9s {
  58. go solver.routineValidator(rows1Index, rows2Index, rows3Index, rows4Index, rows5Index, rows6Index, rows7Index, rows8Index, rows9Index)
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. func (solver *Solver) routineValidator(rows1Index int, rows2Index int, rows3Index int, rows4Index int, rows5Index int, rows6Index int, rows7Index int, rows8Index int, rows9Index int) {
  70. // solver.counter = solver.counter + 1
  71. solver.counter.Add(1)
  72. if solver.validateCombination(solver.row1s[rows1Index], solver.row2s[rows2Index], solver.row3s[rows3Index], solver.row4s[rows4Index], solver.row5s[rows5Index], solver.row6s[rows6Index], solver.row7s[rows7Index], solver.row8s[rows8Index], solver.row9s[rows9Index]) {
  73. solver.solutions = append(solver.solutions, solver.renderCombination(solver.row1s[rows1Index], solver.row2s[rows2Index], solver.row3s[rows3Index], solver.row4s[rows4Index], solver.row5s[rows5Index], solver.row6s[rows6Index], solver.row7s[rows7Index], solver.row8s[rows8Index], solver.row9s[rows9Index]))
  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 rateStart int64
  87. // Tracking the rate, difference between previous iterations
  88. var rateDiff int64
  89. // Tracking duration
  90. var timerStart = time.Now()
  91. // Estimation how long it will take
  92. var est_fin string
  93. // for solver.iter != solver.counter { // Start for-loop
  94. for !done {
  95. // Determine how far we are.
  96. percentage = (float32(solver.counter.Load()) / (float32(solver.iter) / 100))
  97. // Reset the loop
  98. rateDiff = solver.counter.Load() - rateStart
  99. if track <= int(percentage) || rateDiff == 0 { // Start if-statement
  100. // Make sure something happened, making rateStart the only reliable variable
  101. if solver.iter == solver.counter.Load() {
  102. percentage = 100
  103. solver.counter.Store(solver.iter)
  104. done = true
  105. }
  106. timer_elapsed := time.Since(timerStart)
  107. solver.rates = append(solver.rates, rateDiff)
  108. rate_avg := solver.calcAVG()
  109. // Estimate when this is finished
  110. if rateDiff == 0 {
  111. est_fin = "N/A"
  112. } else {
  113. duration_int := (solver.iter - solver.counter.Load()) / rate_avg
  114. duration_string := strconv.Itoa(int(duration_int)) + "s"
  115. est, err := time.ParseDuration(duration_string)
  116. if err != nil {
  117. est_fin = "parse error"
  118. }
  119. est_fin = est.String()
  120. }
  121. // Printing the progress
  122. log.Println("Processing: " + strconv.Itoa(int(percentage)) + "% (" + strconv.FormatInt(solver.counter.Load(), 10) + "/" + strconv.Itoa(int(solver.iter)) + "); Rate: " + strconv.FormatInt(rateDiff, 10) + "/sec for " + timer_elapsed.String() + "; Time left (est.): " + est_fin)
  123. // After we are done printing, exit this for-loop
  124. if percentage == 100 {
  125. break
  126. }
  127. // Wrap up the loop or break
  128. if int(percentage) > track {
  129. track = int(percentage)
  130. } else {
  131. track = track + 1
  132. }
  133. timerStart = time.Now()
  134. }
  135. // Resert the rate counter
  136. rateStart = solver.counter.Load()
  137. // Sleep for a second
  138. if solver.iter != solver.counter.Load() {
  139. time.Sleep(1 * time.Second)
  140. }
  141. } // End for-loop
  142. }
  143. func (solver *Solver) validateCombination(row1 int, row2 int, row3 int, row4 int, row5 int, row6 int, row7 int, row8 int, row9 int) (retval bool) {
  144. retval = true
  145. row1s := strconv.Itoa(row1)
  146. row2s := strconv.Itoa(row2)
  147. row3s := strconv.Itoa(row3)
  148. row4s := strconv.Itoa(row4)
  149. row5s := strconv.Itoa(row5)
  150. row6s := strconv.Itoa(row6)
  151. row7s := strconv.Itoa(row7)
  152. row8s := strconv.Itoa(row8)
  153. row9s := strconv.Itoa(row9)
  154. for index := range 9 {
  155. 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] {
  156. retval = false
  157. }
  158. 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] {
  159. retval = false
  160. }
  161. 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] {
  162. retval = false
  163. }
  164. 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] {
  165. retval = false
  166. }
  167. 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] {
  168. retval = false
  169. }
  170. 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] {
  171. retval = false
  172. }
  173. 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] {
  174. retval = false
  175. }
  176. 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] {
  177. retval = false
  178. }
  179. 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] {
  180. retval = false
  181. }
  182. }
  183. return retval
  184. }
  185. func (solver *Solver) calcAVG() (avg int64) {
  186. var avgSum int64
  187. for _, value := range solver.rates {
  188. avgSum += value
  189. }
  190. avg = avgSum / int64(len(solver.rates))
  191. return
  192. }