2023 day 09
This commit is contained in:
parent
eec219eeda
commit
b54d01e43a
9 changed files with 450 additions and 0 deletions
85
2023/09/part1/part1.go
Normal file
85
2023/09/part1/part1.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
|
||||
package part1
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"strings"
|
||||
"strconv"
|
||||
"os"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
reader := bufio.NewReader(os.Stdin);
|
||||
total := 0;
|
||||
for {
|
||||
line, err := reader.ReadString('\n');
|
||||
if (err != nil) {
|
||||
break
|
||||
}
|
||||
values, err := LineConv(line)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
next, err := Next(values)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
total += next;
|
||||
fmt.Printf("%v => %v\n", values, next)
|
||||
}
|
||||
println(total)
|
||||
return
|
||||
}
|
||||
|
||||
func Next(src []int) (int, error) {
|
||||
if len(src) < 2 {
|
||||
return 0, errors.New("not enough data!")
|
||||
}
|
||||
if IsZero(src) {
|
||||
return 0, nil
|
||||
}
|
||||
deriv := Deriv(src)
|
||||
nextDelta, err := Next(deriv)
|
||||
if (err != nil) {
|
||||
return 0, err
|
||||
}
|
||||
return src[len(src)-1] + nextDelta, nil
|
||||
}
|
||||
|
||||
func Deriv(src []int) (deriv []int) {
|
||||
for i := range src {
|
||||
if i == len(src)-1 {
|
||||
break
|
||||
}
|
||||
deriv = append(deriv, src[i+1] - src[i])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func IsZero(src []int) bool {
|
||||
res := true
|
||||
for _,v := range src {
|
||||
if v != 0 {
|
||||
res = false
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func LineConv(line string) ( []int, error ) {
|
||||
strs := strings.Split(strings.Trim(line, "\n"), " ")
|
||||
values := []int{};
|
||||
for _, str := range strs {
|
||||
value, err := strconv.Atoi(str)
|
||||
if (err != nil) {
|
||||
return []int{}, err
|
||||
}
|
||||
values = append(values, value)
|
||||
}
|
||||
return values, nil;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue