feat: use min() and get rid of Reindeer struct
This commit is contained in:
@@ -12,13 +12,6 @@ func init() {
|
|||||||
registry.Register("2015D14", ParseInput, PartOne, PartTwo)
|
registry.Register("2015D14", ParseInput, PartOne, PartTwo)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Reindeer struct {
|
|
||||||
Speed int
|
|
||||||
FlyTime int
|
|
||||||
RestTime int
|
|
||||||
CycleTime int
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseInput(filepath string) []string {
|
func ParseInput(filepath string) []string {
|
||||||
content, _ := os.ReadFile(filepath)
|
content, _ := os.ReadFile(filepath)
|
||||||
return strings.Split(string(content), "\n")
|
return strings.Split(string(content), "\n")
|
||||||
@@ -30,21 +23,17 @@ func calculateMaxDistance(data []string, time int) int {
|
|||||||
|
|
||||||
for _, line := range data {
|
for _, line := range data {
|
||||||
matches := pattern.FindStringSubmatch(line)
|
matches := pattern.FindStringSubmatch(line)
|
||||||
var reindeer Reindeer
|
|
||||||
reindeer.Speed, _ = strconv.Atoi(matches[1])
|
|
||||||
reindeer.FlyTime, _ = strconv.Atoi(matches[2])
|
|
||||||
reindeer.RestTime, _ = strconv.Atoi(matches[3])
|
|
||||||
reindeer.CycleTime = reindeer.FlyTime + reindeer.RestTime
|
|
||||||
|
|
||||||
fullCycles := time / reindeer.CycleTime
|
speed, _ := strconv.Atoi(matches[1])
|
||||||
distance := fullCycles * reindeer.Speed * reindeer.FlyTime
|
flyTime, _ := strconv.Atoi(matches[2])
|
||||||
|
restTime, _ := strconv.Atoi(matches[3])
|
||||||
|
cycleTime := flyTime + restTime
|
||||||
|
|
||||||
remainingTime := time % reindeer.CycleTime
|
fullCycles := time / cycleTime
|
||||||
if remainingTime > reindeer.FlyTime {
|
distance := fullCycles * speed * flyTime
|
||||||
distance += reindeer.Speed * reindeer.FlyTime
|
|
||||||
} else {
|
remainingTime := time % cycleTime
|
||||||
distance += reindeer.Speed * remainingTime
|
distance += speed * min(remainingTime, flyTime)
|
||||||
}
|
|
||||||
|
|
||||||
if distance > maxDistance {
|
if distance > maxDistance {
|
||||||
maxDistance = distance
|
maxDistance = distance
|
||||||
|
|||||||
Reference in New Issue
Block a user