This commit is contained in:
gareth
2023-12-06 21:56:51 +00:00
parent 245e7858fa
commit 011e162b3c
5 changed files with 1055 additions and 1085 deletions

View File

@@ -0,0 +1,40 @@
package day06
type Race struct {
Time int
Distance int
}
func Part1(input string) int {
var Races []Race
Races = append(Races, Race{56, 546})
Races = append(Races, Race{97, 1927})
Races = append(Races, Race{78, 1131})
Races = append(Races, Race{75, 1139})
total := 1
for _, r := range Races {
halfTime := r.Time / 2
for i := 0; i <= halfTime; i++ {
raceDis := i * (r.Time - i)
if raceDis > r.Distance {
total = total * (r.Time - (i * 2) + 1)
break
}
}
}
return total
}
func Part2(input string) int {
r := Race{56977875, 546192711311139}
halfTime := r.Time / 2
for i := 0; i <= halfTime; i++ {
raceDis := i * (r.Time - i)
if raceDis > r.Distance {
return r.Time - (i * 2) + 1
}
}
return -1
}

View File

@@ -0,0 +1,16 @@
package day06
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestPart1(t *testing.T) {
r := Part1(``)
assert.Equal(t, 1624896, r)
}
func TestPart2(t *testing.T) {
r := Part2(``)
assert.Equal(t, 32583852, r)
}

1000
2023/gareth/day06/input.txt Normal file

File diff suppressed because it is too large Load Diff