Advertisement
Guest User

Untitled

a guest
May 6th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. /*
  2. * Google Code Jam
  3. * 2016 Qualification Round
  4. * Problem A. Counting Sheep
  5. * AQ <aq@okaq.com>
  6. */
  7. package main
  8.  
  9. import (
  10. "bufio"
  11. "fmt"
  12. "os"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "time"
  17. )
  18.  
  19. const (
  20. IN = "A-large-practice.in"
  21. OUT = "A-large-practice.out"
  22. )
  23.  
  24. var (
  25. I *os.File
  26. O *os.File
  27. S *bufio.Scanner
  28. W *bufio.Writer
  29. T int
  30. WG sync.WaitGroup
  31. Sh []*Sheep
  32. )
  33.  
  34. type Sheep struct {
  35. Id int // case number
  36. N int // starting number
  37. C int // current count
  38. M []string // accumulator
  39. S int // solution
  40. }
  41.  
  42. func NewSheep() *Sheep {
  43. // return &Sheep{}
  44. // create new
  45. s := &Sheep{}
  46. s.C = 1
  47. s.M = make([]string, 10)
  48. copy(s.M, strings.Split("0123456789", ""))
  49. return s
  50. }
  51.  
  52. func (s *Sheep) Solve() {
  53. // count
  54. if s.N == 0 {
  55. s.S = 0
  56. WG.Done()
  57. return
  58. }
  59. for {
  60. n0 := s.N * s.C
  61. // convert n to string
  62. n1 := strconv.Itoa(n0)
  63. n2 := strings.Split(n1, "")
  64. for i := 0; i < len(n2); i++ {
  65. b0, a0 := Remove(n2[i], s.M)
  66. // copy(s.M, a0)
  67. s.M = a0
  68. if b0 == true {
  69. // fmt.Println(s.Id, s.M, a0, b0)
  70. if len(s.M) == 0 {
  71. // done
  72. s.S = n0
  73. WG.Done()
  74. return
  75. }
  76. }
  77. }
  78. // remove from accumulator
  79. // check size
  80. s.C = s.C + 1
  81. }
  82. // s.S = 0
  83. // s.S = S.C * s.N
  84. // WG.Done()
  85. }
  86.  
  87. func Remove(s string, a []string) (bool, []string) {
  88. // check if s in a
  89. // if so remove and return new and true
  90. // else return original and false
  91. for i := 0; i < len(a); i++ {
  92. if s == a[i] {
  93. b1 := true
  94. a1 := append(a[:i], a[i+1:]...)
  95. return b1, a1
  96. }
  97. }
  98. return false, a
  99. }
  100.  
  101. func Load() {
  102. var err error
  103. I, err = os.Open(IN)
  104. if err != nil {
  105. fmt.Println(err)
  106. }
  107. S = bufio.NewScanner(I)
  108. O, err = os.Create(OUT)
  109. if err != nil {
  110. fmt.Println(err)
  111. }
  112. W = bufio.NewWriter(O)
  113. }
  114.  
  115. func Cases() {
  116. var err error
  117. S.Scan()
  118. T, err = strconv.Atoi(S.Text())
  119. if err != nil {
  120. fmt.Println(err)
  121. }
  122. fmt.Printf("%d test cases. \n", T)
  123. }
  124.  
  125. func Split() {
  126. var err error
  127. Sh = make([]*Sheep, T)
  128. for i := 0; i < T; i++ {
  129. s := NewSheep()
  130. S.Scan()
  131. s.N, err = strconv.Atoi(S.Text())
  132. if err != nil {
  133. fmt.Println(err)
  134. }
  135. s.Id = i + 1
  136. // fmt.Println(s)
  137. Sh[i] = s
  138. WG.Add(1)
  139. go Sh[i].Solve()
  140. }
  141. }
  142.  
  143. func Finish() {
  144. defer I.Close()
  145. defer O.Close()
  146. for i := 0; i < T; i++ {
  147. s0 := ""
  148. if Sh[i].S == 0 {
  149. s0 = fmt.Sprintf("Case #%d: INSOMNIA\n", Sh[i].Id)
  150. } else {
  151. s0 = fmt.Sprintf("Case #%d: %d\n", Sh[i].Id, Sh[i].S)
  152. }
  153. W.WriteString(s0)
  154. }
  155. W.Flush()
  156. }
  157.  
  158. func main() {
  159. begin := time.Now()
  160. Load()
  161. Cases()
  162. Split()
  163. WG.Wait()
  164. Finish()
  165. end := time.Now()
  166. fmt.Printf("total run time: %v.\n", end.Sub(begin))
  167. }
  168.  
  169. // naive simulation with string arrays
  170. // total run time large data set: 2ms
  171. // watch out for slice copy repeating values
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement