Days 7,8,9

This commit is contained in:
gareth
2023-12-09 21:38:21 +00:00
parent 7e3eae9e60
commit d730b7ff9d
14 changed files with 1586 additions and 119 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/day07/input.txt Normal file

File diff suppressed because it is too large Load Diff