github.com/agrigoryan/aoc_2023_go@v0.0.0-20231216221323-4ace361ec685/day6/d6p1.go (about) 1 package day6 2 3 import ( 4 "fmt" 5 "log" 6 "math" 7 "strconv" 8 "strings" 9 10 "github.com/agrigoryan/aoc_2023_go/aocutils" 11 ) 12 13 func d6p1(input string) int { 14 lines := strings.Split(input, "\n") 15 16 filterEmpty := aocutils.Filterer(func(str string) bool { return len(str) > 0 }) 17 toNumbersMapper := aocutils.Mapper(func(str string) int { 18 res, err := strconv.Atoi(str) 19 if err != nil { 20 log.Fatal("failed to map to numbers") 21 } 22 return res 23 }) 24 25 times := toNumbersMapper(filterEmpty(strings.Split(strings.Split(lines[0], ":")[1], " "))) 26 distances := toNumbersMapper(filterEmpty(strings.Split(strings.Split(lines[1], ":")[1], " "))) 27 28 result := 1 29 for i := 0; i < len(times); i++ { 30 t := times[i] 31 d := distances[i] 32 x := t - 1 - 2*int((float64(t)-math.Sqrt(float64(t*t-4*d)))/2) 33 result *= x 34 } 35 fmt.Println(result) 36 return result 37 }