aoc2023/day-6/one.go

40 lines
672 B
Go
Raw Normal View History

2023-12-06 15:56:20 +00:00
package main
type data struct {
time int
distance int
}
var datas = make([]data, 0)
// calculate returns the number of different ways to win a single race
func calculate(time, recordDistance int) int {
distanceCovered := 0
numWins := 0
f := func(x, time int) int {
return x * (time - x)
}
for i := 1; i < time; i++ {
distanceCovered = f(i, time)
if distanceCovered > recordDistance {
numWins++
}
}
return numWins
}
// one returns the product of all race calculations
func one(lines []string) int {
parseLines(lines)
product := 1
for _, data := range datas {
n := calculate(data.time, data.distance)
product *= n
}
return product
}