day13.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type bus struct {
  11. id int64
  12. index int64
  13. }
  14. type schedule struct {
  15. timestamp int64
  16. buses []bus
  17. }
  18. func getIDs(busesString string) ([]bus, error) {
  19. buses := []bus{}
  20. var index int64 = 0
  21. for _, item := range strings.Split(busesString, ",") {
  22. if item != "x" {
  23. id, err := strconv.ParseInt(item, 10, 32)
  24. if err != nil {
  25. return buses, fmt.Errorf("Error parsing busID %s: %s", item, err)
  26. }
  27. newBus := bus{id: id, index: index}
  28. buses = append(buses, newBus)
  29. }
  30. index++
  31. }
  32. return buses, nil
  33. }
  34. func readData(file *os.File) (schedule, error) {
  35. scanner := bufio.NewScanner(file)
  36. data := schedule{}
  37. if !scanner.Scan() {
  38. return data, fmt.Errorf("Error reading timestamp!")
  39. }
  40. timestampString := scanner.Text()
  41. timestamp, err := strconv.ParseInt(timestampString, 10, 32)
  42. if err != nil {
  43. return data, fmt.Errorf("Error parsing timestamp %s: %s", timestampString, err)
  44. }
  45. data.timestamp = timestamp
  46. if !scanner.Scan() {
  47. return data, fmt.Errorf("Error reading buses!")
  48. }
  49. busesString := scanner.Text()
  50. if err := scanner.Err(); err != nil {
  51. return data, err
  52. }
  53. data.buses, err = getIDs(busesString)
  54. if err != nil {
  55. return data, err
  56. }
  57. return data, nil
  58. }
  59. func findEarliestBus(data schedule) int64 {
  60. var earliest int64 = data.timestamp
  61. var earliestID int64 = 0
  62. for _, item := range data.buses {
  63. value := item.id - (data.timestamp % item.id)
  64. if value < earliest {
  65. earliest = value
  66. earliestID = item.id
  67. }
  68. }
  69. return earliest * earliestID
  70. }
  71. func alignBuses(data schedule) int64 {
  72. var advance int64 = 1
  73. var next int64 = 1
  74. var current int64
  75. for _, item := range data.buses {
  76. current = next
  77. for {
  78. current += advance
  79. if (current+item.index)%item.id == 0 {
  80. next = current
  81. break
  82. }
  83. }
  84. advance *= item.id
  85. }
  86. return current
  87. }
  88. func main() {
  89. if len(os.Args) < 2 {
  90. log.Fatal("You need to specify a file!")
  91. }
  92. file, err := os.Open(os.Args[1])
  93. if err != nil {
  94. log.Fatalf("Failed to open %s!\n", os.Args[1])
  95. }
  96. data, err := readData(file)
  97. if err != nil {
  98. log.Fatalf("Failed to read data: %s\n", err)
  99. }
  100. if err := file.Close(); err != nil {
  101. log.Fatalf("Failed to close file: %s\n", err)
  102. }
  103. fmt.Println("Part1:", findEarliestBus(data))
  104. fmt.Println("Part2:", alignBuses(data))
  105. }