processing.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // Prevent division-by-zero error when establishing `rateDiff`
  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. rateDiff = solver.counter.Load() - rateStart
  101. if track <= int(percentage) || rateDiff == 0 { // Start if-statement
  102. // Make sure something happened, making rateStart the only reliable variable
  103. if rateDiff == 0 {
  104. percentage = 100
  105. solver.counter.Store(solver.iter)
  106. done = true
  107. }
  108. timer_elapsed := time.Since(timerStart)
  109. solver.rates = append(solver.rates, rateDiff)
  110. rate_avg := solver.calcAVG()
  111. // Estimate when this is finished
  112. if rateDiff == 0 {
  113. est_fin = "N/A"
  114. } else {
  115. duration_int := (solver.iter - solver.counter.Load()) / rate_avg
  116. duration_string := strconv.Itoa(int(duration_int)) + "s"
  117. est, err := time.ParseDuration(duration_string)
  118. if err != nil {
  119. est_fin = "parse error"
  120. }
  121. est_fin = est.String()
  122. }
  123. // Printing the progress
  124. 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)
  125. // After we are done printing, exit this for-loop
  126. if percentage == 100 {
  127. break
  128. }
  129. // Wrap up the loop or break
  130. if int(percentage) > track {
  131. track = int(percentage)
  132. } else {
  133. track = track + 1
  134. }
  135. timerStart = time.Now()
  136. }
  137. // Resert the rate counter
  138. rateStart = solver.counter.Load()
  139. // Sleep for a second
  140. if solver.iter != solver.counter.Load() {
  141. time.Sleep(1 * time.Second)
  142. }
  143. } // End for-loop
  144. }
  145. func (solver *Solver) validateCombination(row1 int, row2 int, row3 int, row4 int, row5 int, row6 int, row7 int, row8 int, row9 int) (retval bool) {
  146. retval = true
  147. row1s := strconv.Itoa(row1)
  148. row2s := strconv.Itoa(row2)
  149. row3s := strconv.Itoa(row3)
  150. row4s := strconv.Itoa(row4)
  151. row5s := strconv.Itoa(row5)
  152. row6s := strconv.Itoa(row6)
  153. row7s := strconv.Itoa(row7)
  154. row8s := strconv.Itoa(row8)
  155. row9s := strconv.Itoa(row9)
  156. for index := range 9 {
  157. 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] {
  158. retval = false
  159. }
  160. 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] {
  161. retval = false
  162. }
  163. 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] {
  164. retval = false
  165. }
  166. 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] {
  167. retval = false
  168. }
  169. 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] {
  170. retval = false
  171. }
  172. 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] {
  173. retval = false
  174. }
  175. 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] {
  176. retval = false
  177. }
  178. 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] {
  179. retval = false
  180. }
  181. 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] {
  182. retval = false
  183. }
  184. }
  185. return retval
  186. }
  187. func (solver *Solver) calcAVG() (avg int64) {
  188. var avgSum int64
  189. for _, value := range solver.rates {
  190. avgSum += value
  191. }
  192. avg = avgSum / int64(len(solver.rates))
  193. return
  194. }