Over The Wire Advent 2019 Battle of the Galaxies
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.0KB

  1. package main
  2. import (
  3. "botg/models"
  4. "botg/strategies"
  5. "bufio"
  6. "fmt"
  7. "math/rand"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. var stars []*models.Star
  14. var flights []models.Flight
  15. var links []models.Link
  16. func main() {
  17. // Write to logfile
  18. s1 := rand.NewSource(time.Now().UnixNano())
  19. r1 := rand.New(s1)
  20. rand.Seed(10000)
  21. filename := fmt.Sprintf("logs/%d.txt", r1.Intn(10000))
  22. f, err := os.Create(filename)
  23. if err != nil {
  24. panic(err)
  25. }
  26. defer f.Close()
  27. // Open stdin for reading
  28. reader := bufio.NewReader(os.Stdin)
  29. var gameComplete bool = false
  30. var roundComplete bool = false
  31. // Run strategy
  32. strategy := strategies.LinkerStrategy{}
  33. //strategy := strategies.NoneStrategy {}
  34. for gameComplete == false {
  35. roundComplete = false
  36. // Empty flights
  37. flights = []models.Flight{}
  38. links = []models.Link{}
  39. for roundComplete == false {
  40. text, err := reader.ReadString('\n')
  41. if err != nil {
  42. gameComplete = true
  43. break
  44. }
  45. //Write to log
  46. w := bufio.NewWriter(f)
  47. w.WriteString(text)
  48. w.Flush()
  49. roundComplete = parseLine(text)
  50. }
  51. strategy.Execute(stars, flights, links)
  52. fmt.Println("done")
  53. }
  54. }
  55. func parseLine(text string) bool {
  56. parts := strings.Split(strings.Trim(text, "\n"), " ")
  57. command := parts[0]
  58. switch command {
  59. case "stars":
  60. // Process stars
  61. idx := 0
  62. for i := 1; i < len(parts); i+=2 {
  63. var x, y int
  64. x, _ = strconv.Atoi(parts[i])
  65. y, _ = strconv.Atoi(parts[i+1])
  66. star := &models.Star{
  67. Idx: idx,
  68. X: x,
  69. Y: y,
  70. Richness: 0,
  71. Owner: -2,
  72. Ships: -2,
  73. Capturing: false,
  74. CapturedBy: -2,
  75. Turns: -2,
  76. }
  77. stars = append(stars, star)
  78. idx++
  79. }
  80. break
  81. case "star":
  82. var idx, _ = strconv.Atoi(parts[1])
  83. var richness, _ = strconv.Atoi(parts[2])
  84. var owner, _ = strconv.Atoi(parts[3])
  85. var ships, _ = strconv.Atoi(parts[4])
  86. var turns, _ = strconv.Atoi(parts[5])
  87. star := stars[idx]
  88. star.Richness = richness
  89. star.Owner = owner
  90. star.Ships = ships
  91. star.Turns = turns
  92. star.FlightsAllowed = 3
  93. if star.Capturing && !star.Unowned() {
  94. star.Capturing = false
  95. if !star.OwnedByMe() {
  96. star.CapturedBy = -2
  97. }
  98. }
  99. break
  100. case "link":
  101. var source_idx, _ = strconv.Atoi(parts[1])
  102. var target_idx, _ = strconv.Atoi(parts[2])
  103. link := models.Link{
  104. SourceStar: source_idx,
  105. TargetStar: target_idx,
  106. }
  107. links = append(links, link)
  108. break
  109. case "flight":
  110. var source_idx, _ = strconv.Atoi(parts[1])
  111. var target_idx, _ = strconv.Atoi(parts[2])
  112. var ship_count, _ = strconv.Atoi(parts[3])
  113. var owner, _ = strconv.Atoi(parts[4])
  114. var turns, _ = strconv.Atoi(parts[5])
  115. flight := models.Flight{
  116. SourceStar: source_idx,
  117. TargetStar: target_idx,
  118. Ships: ship_count,
  119. Owner: owner,
  120. Turns: turns,
  121. }
  122. flights = append(flights, flight)
  123. break
  124. case "done":
  125. return true
  126. }
  127. // Update star flight data
  128. for _, s := range stars {
  129. for _, f := range flights {
  130. if f.SourceStar == s.Idx {
  131. s.FlightsAllowed--
  132. }
  133. }
  134. }
  135. return false
  136. }