2023 Day 6 Complete!

This commit is contained in:
Brian Buller 2023-12-06 06:17:00 -06:00
parent 1c5f960426
commit 32b30b4174
3 changed files with 53 additions and 0 deletions

2
2023/day06/input Normal file
View File

@ -0,0 +1,2 @@
Time: 53 71 78 80
Distance: 275 1181 1215 1524

49
2023/day06/main.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
part1(inp)
fmt.Println()
part2(inp)
}
func part1(input []string) {
var times []int
var distances []int
for _, v := range strings.Fields(input[0])[1:] {
times = append(times, h.Atoi(v))
}
for _, v := range strings.Fields(input[1])[1:] {
distances = append(distances, h.Atoi(v))
}
res := 1
for i := range times {
res *= countWaysToWin(times[i], distances[i])
}
fmt.Println("# Part 1")
fmt.Println(res)
}
func part2(input []string) {
time := h.Atoi(strings.ReplaceAll(strings.TrimPrefix(input[0], "Time:"), " ", ""))
distance := h.Atoi(strings.ReplaceAll(strings.TrimPrefix(input[1], "Distance:"), " ", ""))
fmt.Println("# Part 2")
fmt.Println(countWaysToWin(time, distance))
}
func countWaysToWin(time, distance int) int {
var res int
for i := 1; i <= time; i++ {
if i*(time-i) > distance {
res++
}
}
return res
}

2
2023/day06/testinput Normal file
View File

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200