generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
43 lines (31 loc) · 932 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
42
43
package day06p1
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])
sum := 1
for i := 1; i < len(timeStrings); i++ {
time, err := strconv.ParseFloat(timeStrings[i], 64)
utils.Check(err, "Unable to parse %s into float64", timeStrings[i])
distance, err := strconv.ParseFloat(distanceStrings[i], 64)
utils.Check(err, "unable to convert %s to float64", distanceStrings[i])
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
}
sum *= int(winways) + 1
}
return sum
}