From a7d13ce84f8759986397bcee382beb30fb716375 Mon Sep 17 00:00:00 2001 From: efim Date: Wed, 6 Dec 2023 12:46:02 +0000 Subject: [PATCH] day6, example --- day6/daySix.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ day6/example | 2 ++ main.go | 4 +-- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 day6/daySix.go create mode 100644 day6/example diff --git a/day6/daySix.go b/day6/daySix.go new file mode 100644 index 0000000..1c722a4 --- /dev/null +++ b/day6/daySix.go @@ -0,0 +1,79 @@ +package day6 + +import ( + "fmt" + "log" + "os" + "strconv" + "strings" +) + +func Run() int { + input := ReadInput("day6/example") + result := 1 + for _, race := range input { + result *= race.countWinningMoves() + } + log.Printf("input %+v\n", input) + return result +} + +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 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 diff --git a/day6/example b/day6/example new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/day6/example @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 diff --git a/main.go b/main.go index 27b7a92..786760e 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,11 @@ package main import ( "log" - "sunshine.industries/aoc2023/day5" + "sunshine.industries/aoc2023/day6" ) func main() { log.Print("> starting run:") - result := day5.Run() + result := day6.Run() log.Printf("day4 result: %d\n****\n", result) }