aoc2023/day-6/two.go
2023-12-06 15:56:20 +00:00

35 lines
728 B
Go

package main
import (
"fmt"
"math"
"strconv"
)
// calculateFaster uses quadratic formula to calculate number of ways to win
func calculateFaster(time, recordDistance int) int {
a := float64(-1)
b := float64(time)
c := float64(-(recordDistance + 1))
x1 := (-b + math.Sqrt(math.Pow(b, 2)-4*a*c)) / (2 * a)
x2 := (-b - math.Sqrt(math.Pow(b, 2)-4*a*c)) / (2 * a)
return int(math.Floor(x2) - math.Ceil(x1) + 1)
}
func two(lines []string) int {
timeStr := ""
distStr := ""
for _, data := range datas {
timeStr += fmt.Sprintf("%d", data.time)
distStr += fmt.Sprintf("%d", data.distance)
}
time, _ := strconv.Atoi(timeStr)
distance, _ := strconv.Atoi(distStr)
n := calculateFaster(time, distance)
return n
}