code.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 main() {
  76. if len(os.Args) < 2 {
  77. log.Fatal("Please provide a file name as argument")
  78. }
  79. input := readInput(os.Args[1])
  80. fmt.Println(findMinXVelocity(input[0]))
  81. }