From 3ba7720d6aa29381dd2568ee6d34b08a0f80eb42 Mon Sep 17 00:00:00 2001 From: Christopher De Vries Date: Wed, 6 Dec 2023 06:53:58 -0500 Subject: [PATCH] completed day 6 --- README.md | 8 +++++++- day06p1/solution.go | 44 ++++++++++++++++++++++++++++++++++++++++ day06p1/solution_test.go | 34 +++++++++++++++++++++++++++++++ day06p2/solution.go | 42 ++++++++++++++++++++++++++++++++++++++ day06p2/solution_test.go | 34 +++++++++++++++++++++++++++++++ inputs | 2 +- 6 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 day06p1/solution.go create mode 100644 day06p1/solution_test.go create mode 100644 day06p2/solution.go create mode 100644 day06p2/solution_test.go diff --git a/README.md b/README.md index a0a5a70..a5b6dce 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Advent of Code 2023 [![Tests](https://github.com/devries/advent_of_code_2023/actions/workflows/main.yml/badge.svg)](https://github.com/devries/advent_of_code_2023/actions/workflows/main.yml) -[![Stars: 10](https://img.shields.io/badge/⭐_Stars-10-yellow)](https://adventofcode.com/2023) +[![Stars: 12](https://img.shields.io/badge/⭐_Stars-12-yellow)](https://adventofcode.com/2023) ## Plan for This Year @@ -81,3 +81,9 @@ opportunity to experiment a bit with code generation. do this the number of ranges grows, but it will always be far fewer calculations than tracking how each individual element is handled. +- [Day 6: Wait For It](https://adventofcode.com/2023/day/6) - [part 1](day06p1/solution.go), [part 2](day06p2/solution.go) + + This was just solving the quadratic equation for the times when the distance + was equal to the time of the race. There was a bit of fiddling with checking + if the distance was greater than or equal to the winning distance, but all in + all that was the gist of it. diff --git a/day06p1/solution.go b/day06p1/solution.go new file mode 100644 index 0000000..2319d24 --- /dev/null +++ b/day06p1/solution.go @@ -0,0 +1,44 @@ +package day06p1 + +import ( + "io" + "math" + "strconv" + "strings" + + "aoc/utils" +) + +// 17729182 is too high + +func Solve(r io.Reader) any { + lines := utils.ReadLines(r) + + timeStrings := strings.Fields(lines[0]) + distanceStrings := strings.Fields(lines[1]) + + sum := 1 + + for i := 1; i < len(timeStrings); i++ { + time, err := strconv.ParseFloat(timeStrings[i], 64) + utils.Check(err, "Unable to parse %s into float", timeStrings[i]) + + distance, err := strconv.ParseFloat(distanceStrings[i], 64) + + tPressMin := 0.5*time - 0.5*math.Sqrt(time*time-4.0*distance) + + tPressMax := 0.5*time + 0.5*math.Sqrt(time*time-4.0*distance) + + winways := math.Floor(tPressMax) - math.Ceil(tPressMin) + if math.Abs(tPressMin-math.Ceil(tPressMin)) < 1.0e-8 { + winways -= 1.0 + } + + if math.Abs(tPressMax-math.Floor(tPressMax)) < 1.0e-8 { + winways -= 1.0 + } + + sum *= int(winways) + 1 + } + return sum +} diff --git a/day06p1/solution_test.go b/day06p1/solution_test.go new file mode 100644 index 0000000..cad27c0 --- /dev/null +++ b/day06p1/solution_test.go @@ -0,0 +1,34 @@ +package day06p1 + +import ( + "strings" + "testing" + + "aoc/utils" +) + +var testInput = `Time: 7 15 30 +Distance: 9 40 200` + +func TestSolve(t *testing.T) { + tests := []struct { + input string + answer int + }{ + {testInput, 288}, + } + + if testing.Verbose() { + utils.Verbose = true + } + + for _, test := range tests { + r := strings.NewReader(test.input) + + result := Solve(r).(int) + + if result != test.answer { + t.Errorf("Expected %d, got %d", test.answer, result) + } + } +} diff --git a/day06p2/solution.go b/day06p2/solution.go new file mode 100644 index 0000000..aeb2b47 --- /dev/null +++ b/day06p2/solution.go @@ -0,0 +1,42 @@ +package day06p2 + +import ( + "io" + "math" + "strconv" + "strings" + + "aoc/utils" +) + +// 17729182 is too high + +func Solve(r io.Reader) any { + lines := utils.ReadLines(r) + + timeStrings := strings.Fields(lines[0]) + distanceStrings := strings.Fields(lines[1]) + + timeString := strings.Join(timeStrings[1:], "") + distanceString := strings.Join(distanceStrings[1:], "") + + time, err := strconv.ParseFloat(timeString, 64) + utils.Check(err, "Unable to parse %s into float", timeString) + + distance, err := strconv.ParseFloat(distanceString, 64) + + tPressMin := 0.5*time - 0.5*math.Sqrt(time*time-4.0*distance) + + tPressMax := 0.5*time + 0.5*math.Sqrt(time*time-4.0*distance) + + winways := math.Floor(tPressMax) - math.Ceil(tPressMin) + if math.Abs(tPressMin-math.Ceil(tPressMin)) < 1.0e-8 { + winways -= 1.0 + } + + if math.Abs(tPressMax-math.Floor(tPressMax)) < 1.0e-8 { + winways -= 1.0 + } + + return int(winways) + 1 +} diff --git a/day06p2/solution_test.go b/day06p2/solution_test.go new file mode 100644 index 0000000..ca727e0 --- /dev/null +++ b/day06p2/solution_test.go @@ -0,0 +1,34 @@ +package day06p2 + +import ( + "strings" + "testing" + + "aoc/utils" +) + +var testInput = `Time: 7 15 30 +Distance: 9 40 200` + +func TestSolve(t *testing.T) { + tests := []struct { + input string + answer int + }{ + {testInput, 71503}, + } + + if testing.Verbose() { + utils.Verbose = true + } + + for _, test := range tests { + r := strings.NewReader(test.input) + + result := Solve(r).(int) + + if result != test.answer { + t.Errorf("Expected %d, got %d", test.answer, result) + } + } +} diff --git a/inputs b/inputs index b37e9ca..7802f21 160000 --- a/inputs +++ b/inputs @@ -1 +1 @@ -Subproject commit b37e9ca763ead1f95a396065c5ae8ae53c8ef07f +Subproject commit 7802f21993f05a90ffe839775e5aa86452fc6daa