Browse Source

implement day 6 part 1 and 2

master
Alexander Avery 11 months ago
parent
commit
3f7d857bf2
  1. 69
      day06.go

69
day06.go

@ -0,0 +1,69 @@
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)
}
Loading…
Cancel
Save