code.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type point struct {
  11. min, max int
  12. }
  13. func readInput(file string) []point {
  14. content, err := ioutil.ReadFile(file)
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. var points []point
  19. line := string(content)
  20. initialSplit := strings.Split(line, ", ")
  21. if len(initialSplit) != 2 {
  22. log.Fatal("Invalid input")
  23. }
  24. firstPartSplit := strings.Split(initialSplit[0], "=")
  25. if len(firstPartSplit) != 2 {
  26. log.Fatal("Invalid input")
  27. }
  28. firstPartNumbers := strings.Split(firstPartSplit[1], "..")
  29. if len(firstPartNumbers) != 2 {
  30. log.Fatal("Invalid input")
  31. }
  32. minX, err := strconv.Atoi(firstPartNumbers[0])
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. maxX, err := strconv.Atoi(firstPartNumbers[1])
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. points = append(points, point{minX, maxX})
  41. secondPartNumbers := strings.Split(initialSplit[1], "..")
  42. if len(secondPartNumbers) != 2 {
  43. log.Fatal("Invalid input")
  44. }
  45. minY, err := strconv.Atoi(strings.TrimLeft(secondPartNumbers[0], "y="))
  46. if err != nil {
  47. log.Fatal(err)
  48. }
  49. maxY, err := strconv.Atoi(secondPartNumbers[1])
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. points = append(points, point{minY, maxY})
  54. return points
  55. }
  56. func findMinXVelocity(xPoint point) int {
  57. for i := 1; i < xPoint.min; i++ {
  58. v := i
  59. x := 0
  60. for {
  61. x += v
  62. if v > 0 {
  63. v--
  64. }
  65. if v == 0 {
  66. break
  67. }
  68. }
  69. if x >= xPoint.min {
  70. return i
  71. }
  72. }
  73. return 0
  74. }
  75. func simulateY(v int, yPoint point) int {
  76. y := 0
  77. largestY := y
  78. previousY := y
  79. for {
  80. previousY = y
  81. if y <= yPoint.max {
  82. break
  83. }
  84. y += v
  85. if y > largestY {
  86. largestY = y
  87. }
  88. v--
  89. }
  90. if previousY >= yPoint.min && previousY <= yPoint.max {
  91. return largestY
  92. }
  93. return 0
  94. }
  95. func part2(minXVelocity int, input []point) int {
  96. found := 0
  97. for initialXVelocity := minXVelocity; initialXVelocity <= input[0].max; initialXVelocity++ {
  98. for initialYVelocity := 0 - input[1].min - 1; initialYVelocity >= input[1].min; initialYVelocity-- {
  99. x := 0
  100. y := 0
  101. xV := initialXVelocity
  102. yV := initialYVelocity
  103. for {
  104. x += xV
  105. y += yV
  106. if xV > 0 {
  107. xV--
  108. }
  109. yV--
  110. if x > input[0].max || y < input[1].min {
  111. break
  112. }
  113. if (x >= input[0].min && x <= input[0].max) && (y >= input[1].min && y <= input[1].max) {
  114. found++
  115. break
  116. }
  117. }
  118. }
  119. }
  120. return found
  121. }
  122. func main() {
  123. input := readInput(os.Args[1])
  124. minXVelocity := findMinXVelocity(input[0])
  125. fmt.Println("Part1:", simulateY(0-input[1].min-1, input[1]))
  126. fmt.Println("Part2:", part2(minXVelocity, input))
  127. }