processing.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.Controller.Row1, &solver.row1s)
  11. solver.findBlocks(&solver.Controller.Row2, &solver.row2s)
  12. solver.findBlocks(&solver.Controller.Row3, &solver.row3s)
  13. solver.findBlocks(&solver.Controller.Row4, &solver.row4s)
  14. solver.findBlocks(&solver.Controller.Row5, &solver.row5s)
  15. solver.findBlocks(&solver.Controller.Row6, &solver.row6s)
  16. solver.findBlocks(&solver.Controller.Row7, &solver.row7s)
  17. solver.findBlocks(&solver.Controller.Row8, &solver.row8s)
  18. solver.findBlocks(&solver.Controller.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.Controller.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.validator(rows1Index, rows2Index, rows3Index, rows4Index, rows5Index, rows6Index, rows7Index, rows8Index, rows9Index)
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. func (solver *Solver) validator(rows1Index int, rows2Index int, rows3Index int, rows4Index int, rows5Index int, rows6Index int, rows7Index int, rows8Index int, rows9Index int) {
  70. solver.counter.Add(1)
  71. 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]) {
  72. solver.Controller.Solutions = append(solver.Controller.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]))
  73. }
  74. }
  75. func (solver *Solver) Tracker() {
  76. // Add time tracking
  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. // While not needed for rateDiff anymore, it makes estimation calculations more accurate. ☹️
  94. time.Sleep(time.Second)
  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 solver.Iter == solver.counter.Load() {
  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. } else {
  121. est_fin = est.String()
  122. }
  123. }
  124. // Printing the progress
  125. 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)
  126. // After we are done printing, exit this for-loop
  127. if percentage == 100 {
  128. break
  129. }
  130. // Wrap up the loop or break
  131. if int(percentage) > track {
  132. track = int(percentage)
  133. } else {
  134. track = track + 1
  135. }
  136. timerStart = time.Now()
  137. }
  138. // Resert the rate counter
  139. rateStart = solver.counter.Load()
  140. // Sleep for a second
  141. if solver.Iter != solver.counter.Load() {
  142. time.Sleep(1 * time.Second)
  143. }
  144. } // End for-loop
  145. }
  146. func (solver *Solver) validateCombination(row1 int, row2 int, row3 int, row4 int, row5 int, row6 int, row7 int, row8 int, row9 int) (retval bool) {
  147. retval = true
  148. row1s := strconv.Itoa(row1)
  149. row2s := strconv.Itoa(row2)
  150. row3s := strconv.Itoa(row3)
  151. row4s := strconv.Itoa(row4)
  152. row5s := strconv.Itoa(row5)
  153. row6s := strconv.Itoa(row6)
  154. row7s := strconv.Itoa(row7)
  155. row8s := strconv.Itoa(row8)
  156. row9s := strconv.Itoa(row9)
  157. for index := range 9 {
  158. 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] {
  159. retval = false
  160. }
  161. 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] {
  162. retval = false
  163. }
  164. 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] {
  165. retval = false
  166. }
  167. 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] {
  168. retval = false
  169. }
  170. 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] {
  171. retval = false
  172. }
  173. 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] {
  174. retval = false
  175. }
  176. 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] {
  177. retval = false
  178. }
  179. 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] {
  180. retval = false
  181. }
  182. 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] {
  183. retval = false
  184. }
  185. }
  186. return retval
  187. }
  188. func (solver *Solver) calcAVG() (avg int64) {
  189. var avgSum int64
  190. for _, value := range solver.rates {
  191. avgSum += value
  192. }
  193. avg = avgSum / int64(len(solver.rates))
  194. return
  195. }