12 func Part1(input string) int {
14 ins := parseInput1(input)
15 for _, in := range ins {
16 t1, t2, _ := solveQuadratic(1, float64(in.Time) * -1, float64(in.Distance))
17 fmt.Printf("t1: %v t2: %v ans: %v\n", t1, t2, (t1 - t2 + 1))
18 answer *= (t1 - t2 + 1)
24 func Part2(input string) int {
26 ins := parseInput2(input)
27 for _, in := range ins {
28 t1, t2, _ := solveQuadratic(1, float64(in.Time) * -1, float64(in.Distance))
29 fmt.Printf("t1: %v t2: %v ans: %v\n", t1, t2, (t1 - t2 + 1))
30 answer *= (t1 - t2 + 1)
35 func parseInput1(input string) []Input {
37 in = append(in, Input{46, 214})
38 in = append(in, Input{80, 1177})
39 in = append(in, Input{78, 1402})
40 in = append(in, Input{66, 1024})
44 func parseInput2(input string) []Input {
46 in = append(in, Input{46807866, 214117714021024})
50 func solveQuadratic(a, b, c float64) (float64, float64, error) {
51 // Calculate the discriminant
52 discriminant := b*b - 4*a*c
54 // Check if the discriminant is non-negative
56 return 0, 0, fmt.Errorf("no real roots, discriminant is negative")
59 // Calculate the roots
60 root1 := (-b + math.Sqrt(discriminant)) / (2 * a)
61 root2 := (-b - math.Sqrt(discriminant)) / (2 * a)
63 if isWholeNumber(root1) { root1 = root1 - 1.0 }
64 if isWholeNumber(root2) { root2 = root2 + 1.0 }
65 return math.Floor(root1), math.Ceil(root2), nil
68 func isWholeNumber(x float64) bool {
69 rounded := math.Round(x)