fork download
  1. package main
  2.  
  3. import (
  4. "bytes"
  5. "fmt"
  6. )
  7.  
  8. func main() {
  9. CountdownSolver(Pop([]int{1, 3, 7, 6, 8, 3, 250}))
  10. CountdownSolver(Pop([]int{25, 100, 9, 7, 3, 7, 881}))
  11. CountdownSolver(Pop([]int{6, 75, 3, 25, 50, 100, 952}))
  12. }
  13.  
  14. func CountdownSolver(s int, a []int) {
  15. c := make([]int, len(a))
  16. m := make(map[string]bool)
  17.  
  18. for i := 0; i < len(a); {
  19. if i == 0 {
  20. if _, ok := m[AToS(a)]; !ok {
  21. m[AToS(a)] = true
  22. if eq, ok := Solve(s, a); ok {
  23. fmt.Println(eq, "=", s)
  24. }
  25. }
  26. }
  27.  
  28. if c[i] < i {
  29. if i%2 == 0 {
  30. a[0], a[i] = a[i], a[0]
  31. } else {
  32. a[i], a[c[i]] = a[c[i]], a[i]
  33. }
  34.  
  35. c[i]++
  36. i = 0
  37. } else {
  38. c[i] = 0
  39. i++
  40. }
  41. }
  42. }
  43.  
  44. func AToS(v []int) string {
  45. var buffer bytes.Buffer
  46.  
  47. for _, e := range v {
  48. buffer.WriteString(fmt.Sprint(e, ","))
  49. }
  50.  
  51. return buffer.String()
  52. }
  53.  
  54. func Solve(s int, p []int) (string, bool) {
  55. ops := GetOperators()
  56.  
  57. for _, op := range ops {
  58. if ApplyOps(p, op) == s {
  59. return GetEq(p, op), true
  60. }
  61. }
  62. return "", false
  63. }
  64.  
  65. func GetOperators() [][]int {
  66. var res [][]int
  67. for i := 0; i < 4; i++ {
  68. for j := 0; j < 4; j++ {
  69. for k := 0; k < 4; k++ {
  70. for l := 0; l < 4; l++ {
  71. for m := 0; m < 4; m++ {
  72. res = append(res, []int{i, j, k, l, m})
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return res
  79. }
  80.  
  81. func ApplyOps(p []int, op []int) int {
  82. s := p[0]
  83. for i := 1; i < len(p); i++ {
  84. s = ApplyOp(s, p[i], op[i-1])
  85. }
  86. return s
  87. }
  88.  
  89. func ApplyOp(a int, b int, op int) int {
  90. switch op {
  91. case 0:
  92. return a + b
  93. case 1:
  94. return a - b
  95. case 2:
  96. return a * b
  97. case 3:
  98. return a / b
  99. }
  100. return 0
  101. }
  102.  
  103. func GetEq(p []int, op []int) string {
  104. var buffer bytes.Buffer
  105.  
  106. buffer.WriteString(fmt.Sprint(p[0]))
  107.  
  108. for i := 1; i < len(p); i++ {
  109. buffer.WriteString(fmt.Sprint(" ", GetOp(op[i-1]), " ", p[i]))
  110. }
  111.  
  112. return buffer.String()
  113. }
  114.  
  115. func GetOp(op int) string {
  116. switch op {
  117. case 0:
  118. return "+"
  119. case 1:
  120. return "-"
  121. case 2:
  122. return "*"
  123. case 3:
  124. return "/"
  125. }
  126. return ""
  127.  
  128. }
  129.  
  130. func Pop(a []int) (int, []int) {
  131. return a[len(a)-1], a[:len(a)-1]
  132. }
Success #stdin #stdout 0.12s 8848KB
stdin
Standard input is empty
stdout
3 + 3 * 7 + 1 * 6 - 8 = 250
3 + 8 * 7 + 6 * 3 + 1 = 250
8 + 3 * 7 + 6 * 3 + 1 = 250
7 + 100 * 25 - 9 / 3 - 7 = 881
100 + 7 * 25 - 9 / 3 - 7 = 881
7 / 7 + 100 * 9 - 25 - 3 = 881
25 - 9 * 7 * 7 + 100 - 3 = 881
25 - 9 * 7 * 7 - 3 + 100 = 881
7 * 3 + 100 * 7 + 25 + 9 = 881
3 * 7 + 100 * 7 + 25 + 9 = 881
7 / 7 + 100 * 9 - 3 - 25 = 881
3 * 7 + 100 * 7 + 9 + 25 = 881
7 * 3 + 100 * 7 + 9 + 25 = 881
25 * 100 / 3 + 75 - 6 + 50 = 952
100 * 25 / 3 + 75 - 6 + 50 = 952
100 * 25 / 3 - 6 + 75 + 50 = 952
25 * 100 / 3 - 6 + 75 + 50 = 952
25 * 100 / 3 - 6 + 50 + 75 = 952
100 * 25 / 3 - 6 + 50 + 75 = 952
100 * 25 / 3 + 50 - 6 + 75 = 952
25 * 100 / 3 + 50 - 6 + 75 = 952
3 + 100 * 75 * 6 / 50 + 25 = 952
100 + 3 * 75 * 6 / 50 + 25 = 952
100 + 6 * 75 * 3 - 50 / 25 = 952
6 + 100 * 75 * 3 - 50 / 25 = 952
100 + 3 * 6 * 75 / 50 + 25 = 952
3 + 100 * 6 * 75 / 50 + 25 = 952
6 + 100 * 3 * 75 - 50 / 25 = 952
100 + 6 * 3 * 75 - 50 / 25 = 952
100 * 25 / 3 + 75 + 50 - 6 = 952
25 * 100 / 3 + 75 + 50 - 6 = 952
25 * 100 / 3 + 50 + 75 - 6 = 952
100 * 25 / 3 + 50 + 75 - 6 = 952