generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.go
41 lines (29 loc) · 940 Bytes
/
solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package day06p2
import (
"io"
"math"
"strconv"
"strings"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
timeStrings := strings.Fields(lines[0])
distanceStrings := strings.Fields(lines[1])
timeString := strings.Join(timeStrings[1:], "")
distanceString := strings.Join(distanceStrings[1:], "")
time, err := strconv.ParseFloat(timeString, 64)
utils.Check(err, "Unable to parse %s into float64", timeString)
distance, err := strconv.ParseFloat(distanceString, 64)
utils.Check(err, "Unable to parse %s to float64", distanceString)
tPressMin := 0.5*time - 0.5*math.Sqrt(time*time-4.0*distance)
tPressMax := 0.5*time + 0.5*math.Sqrt(time*time-4.0*distance)
winways := math.Floor(tPressMax) - math.Ceil(tPressMin)
if math.Abs(tPressMin-math.Ceil(tPressMin)) < 1.0e-8 {
winways -= 1.0
}
if math.Abs(tPressMax-math.Floor(tPressMax)) < 1.0e-8 {
winways -= 1.0
}
return int(winways) + 1
}