mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-12-22 13:51:55 +01:00
solve day 6 (with maths)
This commit is contained in:
parent
fa66368b91
commit
976d3b5e01
6 changed files with 115 additions and 14 deletions
|
@ -4,7 +4,6 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"io"
|
"io"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -105,19 +104,6 @@ type day05SeedRange struct {
|
||||||
start, end int
|
start, end int
|
||||||
}
|
}
|
||||||
|
|
||||||
func lineToInts(line string) []int {
|
|
||||||
values := strings.Split(line, " ")
|
|
||||||
ints := make([]int, len(values))
|
|
||||||
for i, value := range values {
|
|
||||||
number, err := strconv.Atoi(value)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
ints[i] = number
|
|
||||||
}
|
|
||||||
return ints
|
|
||||||
}
|
|
||||||
|
|
||||||
type day05Range struct {
|
type day05Range struct {
|
||||||
destStart, sourceStart, length int
|
destStart, sourceStart, length int
|
||||||
}
|
}
|
||||||
|
|
63
2023/day06_boats.go
Normal file
63
2023/day06_boats.go
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
package _023
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Day06Part1(input io.Reader) (int, error) {
|
||||||
|
scanner := bufio.NewScanner(input)
|
||||||
|
|
||||||
|
scanner.Scan()
|
||||||
|
times := lineToInts(strings.TrimPrefix(scanner.Text(), "Time:"))
|
||||||
|
scanner.Scan()
|
||||||
|
distances := lineToInts(strings.TrimPrefix(scanner.Text(), "Distance:"))
|
||||||
|
fmt.Println(times, distances)
|
||||||
|
|
||||||
|
margin := 1
|
||||||
|
|
||||||
|
for i := 0; i < len(times); i++ {
|
||||||
|
x1, x2 := zeros(times[i], distances[i])
|
||||||
|
if distance(x1, times[i]) <= distances[i] {
|
||||||
|
x1++
|
||||||
|
}
|
||||||
|
margin *= x2 - x1
|
||||||
|
}
|
||||||
|
|
||||||
|
return margin, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func zeros(time, distance int) (int, int) {
|
||||||
|
t, d := float64(time), float64(distance)
|
||||||
|
delta := math.Sqrt(math.Pow(t, 2) - 4*d)
|
||||||
|
return int(math.Ceil((t - delta) / 2)), int(math.Ceil((t + delta) / 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func distance(timePress, timeMax int) int {
|
||||||
|
return timePress * (timeMax - timePress)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Day06Part2(input io.Reader) (int, error) {
|
||||||
|
scanner := bufio.NewScanner(input)
|
||||||
|
|
||||||
|
scanner.Scan()
|
||||||
|
times := lineToInts(strings.ReplaceAll(strings.TrimPrefix(scanner.Text(), "Time:"), " ", ""))
|
||||||
|
scanner.Scan()
|
||||||
|
distances := lineToInts(strings.ReplaceAll(strings.TrimPrefix(scanner.Text(), "Distance:"), " ", ""))
|
||||||
|
fmt.Println(times, distances)
|
||||||
|
|
||||||
|
margin := 1
|
||||||
|
|
||||||
|
for i := 0; i < len(times); i++ {
|
||||||
|
x1, x2 := zeros(times[i], distances[i])
|
||||||
|
if distance(x1, times[i]) <= distances[i] {
|
||||||
|
x1++
|
||||||
|
}
|
||||||
|
margin *= x2 - x1
|
||||||
|
}
|
||||||
|
|
||||||
|
return margin, nil
|
||||||
|
}
|
25
2023/day06_boats_test.go
Normal file
25
2023/day06_boats_test.go
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package _023
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDay06Part1(t *testing.T) {
|
||||||
|
tests := []testCase{
|
||||||
|
{"inputs/day06_test1", 288},
|
||||||
|
{"inputs/day06", 1660968},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.filename, check(test, Day06Part1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDay06Part2(t *testing.T) {
|
||||||
|
tests := []testCase{
|
||||||
|
{"inputs/day06_test1", 71503},
|
||||||
|
{"inputs/day06", 26499773},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.filename, check(test, Day06Part2))
|
||||||
|
}
|
||||||
|
}
|
2
2023/inputs/day06
Normal file
2
2023/inputs/day06
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Time: 47 98 66 98
|
||||||
|
Distance: 400 1213 1011 1540
|
2
2023/inputs/day06_test1
Normal file
2
2023/inputs/day06_test1
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Time: 7 15 30
|
||||||
|
Distance: 9 40 200
|
23
2023/lib.go
Normal file
23
2023/lib.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package _023
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var reg = regexp.MustCompile(`\s+`)
|
||||||
|
|
||||||
|
func lineToInts(line string) []int {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
values := reg.Split(line, -1)
|
||||||
|
ints := make([]int, len(values))
|
||||||
|
for i, value := range values {
|
||||||
|
number, err := strconv.Atoi(value)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
ints[i] = number
|
||||||
|
}
|
||||||
|
return ints
|
||||||
|
}
|
Loading…
Reference in a new issue