mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-22 10:20:48 +00:00
35 lines
728 B
Go
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
|
||
|
}
|