solutions for the Advent of Code 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.1 KiB

package main
import (
"bufio"
"fmt"
"io"
)
type day06 struct{}
func (d day06) solve1(r io.Reader) string {
buf := bufio.NewReader(r)
values := make([][]int, 2)
total := 1
for i := 0; i < 2; i++ {
_, err := buf.ReadString(':')
if err != nil {
panic(err)
}
str, _ := buf.ReadString('\n')
values[i] = scanints(str)
}
for i := 0; i < len(values[0]); i++ {
time := values[0][i]
record := values[1][i]
wins := 0
for j := 0; j <= time; j++ {
distance := ((time - j /*remaining time*/) * (j /*speed*/))
if distance > record {
wins++
}
}
total *= wins
wins = 0
}
return fmt.Sprintf("%d", total)
}
func (d day06) solve2(r io.Reader) string {
buf := bufio.NewReader(r)
values := make([][]int, 2)
total := 0
for i := 0; i < 2; i++ {
_, err := buf.ReadString(':')
if err != nil {
panic(err)
}
str, _ := buf.ReadString('\n')
values[i] = scanints(spaceReplacer.Replace(str))
}
time := values[0][0]
record := values[1][0]
for i := 0; i < time; i++ {
distance := (time - i) * i
if distance > record {
total++
}
}
return fmt.Sprintf("%d", total)
}