mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 23:20:49 +00:00
40 lines
672 B
Go
40 lines
672 B
Go
|
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
|
||
|
}
|