You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
847 B
Go
41 lines
847 B
Go
package day6
|
|
|
|
import (
|
|
"math"
|
|
"strings"
|
|
|
|
"gitea.paas.celticinfo.fr/oabrivard/aoc2023/utils"
|
|
)
|
|
|
|
func RaceWinsCount(lines []string) int64 {
|
|
result := int64(1)
|
|
times := utils.ParseInt64Array(strings.TrimPrefix(lines[0], "Time: "), " ")
|
|
dists := utils.ParseInt64Array(strings.TrimPrefix(lines[1], "Distance: "), " ")
|
|
|
|
for i, t := range times {
|
|
d := dists[i]
|
|
|
|
// Solve -x^2 + tx - d > 0
|
|
|
|
// delta = b^2 - 4ac
|
|
delta := utils.Int64Pow(t, 2) - 4*d
|
|
|
|
// x1 = (-t + sqrt(delta)) / -2
|
|
// x2 = (-t - sqrt(delta)) / -2
|
|
x1 := (-float64(t) + math.Sqrt(float64(delta))) / -2.0
|
|
x2 := (-float64(t) - math.Sqrt(float64(delta))) / -2.0
|
|
x1_int := math.Ceil(x1)
|
|
x2_int := math.Floor(x2)
|
|
if x1_int == x1 {
|
|
x1_int++
|
|
}
|
|
if x2_int == x2 {
|
|
x2_int--
|
|
}
|
|
count := int64(x2_int) - int64(x1_int) + 1
|
|
result *= count
|
|
}
|
|
|
|
return result
|
|
}
|