|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "runtime/pprof" |
| 10 | + "strconv" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func gcd(a, b int) int { |
| 15 | + for b != 0 { |
| 16 | + a, b = b, a%b |
| 17 | + } |
| 18 | + return a |
| 19 | +} |
| 20 | + |
| 21 | +// Function to calculate the determinant of a 2x2 matrix |
| 22 | +func det(a, b, c, d int) int { |
| 23 | + return a*d - b*c |
| 24 | +} |
| 25 | + |
| 26 | +func day13(file string) (part1, part2 int) { |
| 27 | + f, err := os.Open(file) |
| 28 | + if err != nil { |
| 29 | + log.Fatal(err) |
| 30 | + } |
| 31 | + defer f.Close() |
| 32 | + scanner := bufio.NewScanner(f) |
| 33 | + regex := regexp.MustCompile(`Button (\w+): X\+([0-9]+), Y\+([0-9]+)`) |
| 34 | + regex2 := regexp.MustCompile(`Prize: X=([0-9]+), Y=([0-9]+)`) |
| 35 | + |
| 36 | + for scanner.Scan() { |
| 37 | + l1 := scanner.Text() |
| 38 | + scanner.Scan() |
| 39 | + |
| 40 | + l2 := scanner.Text() |
| 41 | + scanner.Scan() |
| 42 | + |
| 43 | + l3 := scanner.Text() |
| 44 | + scanner.Scan() |
| 45 | + buttons := [][2]int{} |
| 46 | + b1, c1 := 0, 0 |
| 47 | + |
| 48 | + // Parse: Button A: X+31, Y+16 |
| 49 | + |
| 50 | + m := regex.FindStringSubmatch(l1) |
| 51 | + // log.Printf("%+v", m) |
| 52 | + b1, _ = strconv.Atoi(m[2]) |
| 53 | + c1, _ = strconv.Atoi(m[3]) |
| 54 | + |
| 55 | + buttons = append(buttons, [2]int{b1, c1}) |
| 56 | + |
| 57 | + m = regex.FindStringSubmatch(l2) |
| 58 | + // a = m[1] |
| 59 | + b1, _ = strconv.Atoi(m[2]) |
| 60 | + c1, _ = strconv.Atoi(m[3]) |
| 61 | + buttons = append(buttons, [2]int{b1, c1}) |
| 62 | + |
| 63 | + m = regex2.FindStringSubmatch(l3) |
| 64 | + b1, _ = strconv.Atoi(m[1]) |
| 65 | + c1, _ = strconv.Atoi(m[2]) |
| 66 | + |
| 67 | + prize := [2]int{b1, c1} |
| 68 | + |
| 69 | + log.Println(buttons) |
| 70 | + log.Println("Prize", prize) |
| 71 | + |
| 72 | + log.Println(gcd(buttons[0][0], prize[0])) |
| 73 | + buttona := buttons[0] |
| 74 | + buttonb := buttons[1] |
| 75 | + |
| 76 | + // sum := 0 |
| 77 | + x1, x2 := buttona[0], buttonb[0] |
| 78 | + y1, y2 := buttona[1], buttonb[1] |
| 79 | + x3, y3 := prize[0], prize[1] |
| 80 | + // Calculate the determinant of the coefficient matrix |
| 81 | + D := det(x1, x2, y1, y2) |
| 82 | + if D == 0 { |
| 83 | + fmt.Println("No unique solution exists.") |
| 84 | + } else { |
| 85 | + // Calculate the determinants for a and b |
| 86 | + Da := det(x3, x2, y3, y2) |
| 87 | + Db := det(x1, x3, y1, y3) |
| 88 | + // Solve for a and b using Cramer's rule |
| 89 | + a := Da / D |
| 90 | + b := Db / D |
| 91 | + |
| 92 | + if a*x1+b*x2 == x3 && a*y1+b*y2 == y3 { |
| 93 | + fmt.Printf("The solution is: a = %d, b = %d\n", a, b) |
| 94 | + part1 += a*3 + b |
| 95 | + } else { |
| 96 | + fmt.Println("The solution is incorrect.") |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + x3, y3 = x3+10000000000000, y3+10000000000000 |
| 101 | + // Calculate the determinants for a and b |
| 102 | + Da := det(x3, x2, y3, y2) |
| 103 | + Db := det(x1, x3, y1, y3) |
| 104 | + // Solve for a and b using Cramer's rule |
| 105 | + a := Da / D |
| 106 | + b := Db / D |
| 107 | + |
| 108 | + if a*x1+b*x2 == x3 && a*y1+b*y2 == y3 { |
| 109 | + fmt.Printf("The solution is: a = %d, b = %d\n", a, b) |
| 110 | + part2 += a*3 + b |
| 111 | + } else { |
| 112 | + fmt.Println("The solution is incorrect.") |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return |
| 117 | +} |
| 118 | + |
| 119 | +func main() { |
| 120 | + log.SetFlags(log.Lshortfile | log.LstdFlags) |
| 121 | + logperf := false |
| 122 | + if logperf { |
| 123 | + pf, _ := os.Create("default.pgo") |
| 124 | + err := pprof.StartCPUProfile(pf) |
| 125 | + if err != nil { |
| 126 | + log.Fatal(err) |
| 127 | + } |
| 128 | + defer pf.Close() |
| 129 | + } |
| 130 | + t1 := time.Now() |
| 131 | + part1, part2 := day13("test.txt") |
| 132 | + fmt.Println(part1, part2) |
| 133 | + if part1 != 480 || part2 != 875318608908 { |
| 134 | + log.Fatal("Test failed ", part1, part2) |
| 135 | + } |
| 136 | + fmt.Println(day13("input.txt")) |
| 137 | + if logperf { |
| 138 | + pprof.StopCPUProfile() |
| 139 | + } |
| 140 | + |
| 141 | + fmt.Println("Elapsed time:", time.Since(t1)) |
| 142 | +} |
0 commit comments