Skip to content

Commit

Permalink
completed day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
devries committed Dec 6, 2023
1 parent 90b39c1 commit 3ba7720
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.
44 changes: 44 additions & 0 deletions day06p1/solution.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions day06p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
42 changes: 42 additions & 0 deletions day06p2/solution.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions day06p2/solution_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
2 changes: 1 addition & 1 deletion inputs

0 comments on commit 3ba7720

Please sign in to comment.