Compare commits
No commits in common. "85c20005918e348ea0b998d1a09cdea6bee830b3" and "7e6a54379011723141b99e1d71652ab0400eb37b" have entirely different histories.
85c2000591
...
7e6a543790
105
day6/daySix.go
105
day6/daySix.go
|
@ -1,105 +0,0 @@
|
||||||
package day6
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"math"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Run() int {
|
|
||||||
race := ReadInput2("day6/input")
|
|
||||||
|
|
||||||
log.Printf("input %+v\n", race)
|
|
||||||
return race.countWinningMoves()
|
|
||||||
}
|
|
||||||
|
|
||||||
type Race struct {
|
|
||||||
Time, Target int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r Race) countWinningMoves() int {
|
|
||||||
// oh and it's simmetrical
|
|
||||||
leftPad := 0
|
|
||||||
rightPad := 0
|
|
||||||
for i := 0; i < (r.Time / 2 + 1); i++ {
|
|
||||||
if i * (r.Time - i) > r.Target {
|
|
||||||
leftPad = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for i := 0; i < (r.Time / 2 + 1); i++ {
|
|
||||||
time := r.Time - i
|
|
||||||
if time * (r.Time - time) > r.Target {
|
|
||||||
rightPad = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result := r.Time + 1 - leftPad - rightPad
|
|
||||||
log.Printf("for race %+v got left %d and right %d for total wind %d\n", r, leftPad, rightPad, result)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadInput2(filename string) Race {
|
|
||||||
runningTime := 0
|
|
||||||
runningDistance := 0
|
|
||||||
|
|
||||||
bytes, err := os.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintln("problem readin input : ", filename))
|
|
||||||
}
|
|
||||||
text := strings.Split( string(bytes), "\n")
|
|
||||||
times := strings.Fields(text[0])
|
|
||||||
distances := strings.Fields(text[1])
|
|
||||||
|
|
||||||
for i := 1; i < len(times); i++ {
|
|
||||||
timeStr := times[i]
|
|
||||||
distanceStr := distances[i]
|
|
||||||
time, err1 := strconv.Atoi(timeStr)
|
|
||||||
distance, err2 := strconv.Atoi(distanceStr)
|
|
||||||
if err1 != nil || err2 != nil {
|
|
||||||
panic(fmt.Sprintf("error reading race %d from %s %s\n", i, times[i], distances[i]))
|
|
||||||
}
|
|
||||||
|
|
||||||
runningTime = runningTime * int(math.Pow10(len(timeStr))) + time
|
|
||||||
runningDistance = runningDistance * int(math.Pow10(len(distanceStr))) + distance
|
|
||||||
}
|
|
||||||
|
|
||||||
return Race{ runningTime, runningDistance }
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadInput(filename string) []Race {
|
|
||||||
result := make([]Race, 0)
|
|
||||||
|
|
||||||
bytes, err := os.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintln("problem readin input : ", filename))
|
|
||||||
}
|
|
||||||
text := strings.Split( string(bytes), "\n")
|
|
||||||
times := strings.Fields(text[0])
|
|
||||||
distances := strings.Fields(text[1])
|
|
||||||
|
|
||||||
for i := 1; i < len(times); i++ {
|
|
||||||
time, err1 := strconv.Atoi(times[i])
|
|
||||||
target, err2 := strconv.Atoi(distances[i])
|
|
||||||
if err1 != nil || err2 != nil {
|
|
||||||
panic(fmt.Sprintf("error reading race %d from %s %s\n", i, times[i], distances[i]))
|
|
||||||
}
|
|
||||||
race := Race{
|
|
||||||
Time: time,
|
|
||||||
Target: target,
|
|
||||||
}
|
|
||||||
result = append(result, race)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// my guess is that i can search from start and end into middle and stop when i find solution
|
|
||||||
// from the way i think derivation of x*y works
|
|
||||||
// but first - read in the input. with strings.Fields should be easy
|
|
|
@ -1,2 +0,0 @@
|
||||||
Time: 7 15 30
|
|
||||||
Distance: 9 40 200
|
|
|
@ -1,2 +0,0 @@
|
||||||
Time: 40 82 91 66
|
|
||||||
Distance: 277 1338 1349 1063
|
|
4
main.go
4
main.go
|
@ -3,11 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"sunshine.industries/aoc2023/day6"
|
"sunshine.industries/aoc2023/day5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Print("> starting run:")
|
log.Print("> starting run:")
|
||||||
result := day6.Run()
|
result := day5.Run()
|
||||||
log.Printf("day4 result: %d\n****\n", result)
|
log.Printf("day4 result: %d\n****\n", result)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue