adventOfCode/internal/2023/day_06/in.go

68 lines
1.6 KiB
Go
Raw Normal View History

2022-12-06 21:10:52 +00:00
package day_06
2023-12-06 16:55:51 +00:00
import (
"fmt"
"regexp"
"strings"
"rpjosh.de/adventOfCode/pkg/utils"
)
2022-12-06 21:10:52 +00:00
type Day struct{}
func (d *Day) Part1(in string) string {
2023-12-06 16:55:51 +00:00
// Regex to get all numbers
numberRegex := regexp.MustCompile(`\d+`)
inSplit := strings.Split(in, "\n")
rtc := 1
raceTimes := numberRegex.FindAllString(inSplit[0], -1)
raceDistances := numberRegex.FindAllString(inSplit[1], -1)
// Loop through every race and find possible button hold values
for i := 0; i < len(raceTimes); i++ {
time := utils.ToInt(raceTimes[i])
distance := utils.ToInt(raceDistances[i])
// Brute force every possibility and check if the distance could be bet
possiblities := 0
for holding := time; holding > 0; holding -= 1 {
pDistance := (time - holding) * holding
if pDistance > distance {
possiblities += 1
}
}
rtc *= possiblities
}
return fmt.Sprintf("%d", rtc)
2022-12-06 21:10:52 +00:00
}
func (d *Day) Part2(in string) string {
2023-12-06 16:55:51 +00:00
// Regex to get all numbers
numberRegex := regexp.MustCompile(`\d+`)
inSplit := strings.Split(in, "\n")
raceTimes := strings.Join(numberRegex.FindAllString(inSplit[0], -1), "")
raceDistances := strings.Join(numberRegex.FindAllString(inSplit[1], -1), "")
// Loop through every race and find possible button hold values
time := utils.ToInt(raceTimes)
distance := utils.ToInt(raceDistances)
// Brute force every possibility and check if the distance could be bet
possiblities := 0
for holding := time; holding > 0; holding -= 1 {
pDistance := (time - holding) * holding
if pDistance > distance {
possiblities += 1
}
}
return fmt.Sprintf("%d", possiblities)
2022-12-06 21:10:52 +00:00
}