2023 day 09
This commit is contained in:
parent
eec219eeda
commit
b54d01e43a
9 changed files with 450 additions and 0 deletions
36
2023/09/part2/cmd/main.go
Normal file
36
2023/09/part2/cmd/main.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"fmt"
|
||||
"git.tristans.cloud/tristan/aoc/2023/09/part1"
|
||||
"git.tristans.cloud/tristan/aoc/2023/09/part2/part2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
reader := bufio.NewReader(os.Stdin);
|
||||
total := 0;
|
||||
for {
|
||||
line, err := reader.ReadString('\n');
|
||||
if (err != nil) {
|
||||
break
|
||||
}
|
||||
values, err := part1.LineConv(line)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
next, err := part2.Next(values, true)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
total += next;
|
||||
fmt.Printf("%v => %v\n", values, next)
|
||||
}
|
||||
println(total)
|
||||
return
|
||||
}
|
||||
|
28
2023/09/part2/part2/part2.go
Normal file
28
2023/09/part2/part2/part2.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
|
||||
package part2
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.tristans.cloud/tristan/aoc/2023/09/part1"
|
||||
)
|
||||
|
||||
func Next(src []int, dir bool) (int, error) {
|
||||
if len(src) < 2 {
|
||||
return 0, errors.New("not enough data!")
|
||||
}
|
||||
if part1.IsZero(src) {
|
||||
return 0, nil
|
||||
}
|
||||
deriv := part1.Deriv(src)
|
||||
nextDelta, err := Next(deriv, dir)
|
||||
if (err != nil) {
|
||||
return 0, err
|
||||
}
|
||||
if dir {
|
||||
return src[0] - nextDelta, nil
|
||||
} else {
|
||||
return src[len(src)-1] + nextDelta, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
19
2023/09/part2/part2/part2_test.go
Normal file
19
2023/09/part2/part2/part2_test.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
|
||||
package part2
|
||||
import (
|
||||
"testing"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func TestPrev(t *testing.T) {
|
||||
next, err := Next([]int{0,1,2,3}, true)
|
||||
equal(t,err,nil)
|
||||
equal(t,next,-1)
|
||||
}
|
||||
|
||||
func equal(t *testing.T, actual any, target any) {
|
||||
if !reflect.DeepEqual(actual, target) {
|
||||
t.Errorf("expected %v, got %v", target, actual)
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue