50 lines
996 B
Go
50 lines
996 B
Go
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
|
|
}
|