mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-21 18:00:47 +00:00
day-9
This commit is contained in:
parent
af4701dab7
commit
4a7c9082ba
3
day-9/go.mod
Normal file
3
day-9/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module github.com/onyx-and-iris/aoc2023/day-9
|
||||
|
||||
go 1.20
|
25
day-9/one.go
Normal file
25
day-9/one.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
// next returns the next value in the sequence
|
||||
func next(nums []int) int {
|
||||
diffs := []int{}
|
||||
for i := 1; i < len(nums); i++ {
|
||||
diffs = append(diffs, nums[i]-nums[i-1])
|
||||
}
|
||||
|
||||
if allEqual(diffs) {
|
||||
return nums[len(nums)-1] + diffs[len(diffs)-1]
|
||||
}
|
||||
return nums[len(nums)-1] + next(diffs)
|
||||
}
|
||||
|
||||
// one returns the sum of all next values
|
||||
func one(lines []string) int {
|
||||
sum := 0
|
||||
for _, line := range lines {
|
||||
nums := convertToInts(line)
|
||||
sum += next(nums)
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
5
day-9/run.sh
Normal file
5
day-9/run.sh
Normal file
@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
INPUT="input.txt"
|
||||
|
||||
cat $INPUT | go run .
|
21
day-9/solution.go
Normal file
21
day-9/solution.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetLevel(log.InfoLevel)
|
||||
}
|
||||
|
||||
func main() {
|
||||
lines := readlines()
|
||||
|
||||
ans := one(lines)
|
||||
fmt.Printf("solution one: %d\n", ans)
|
||||
|
||||
ans = two(lines)
|
||||
fmt.Printf("solution two: %d\n", ans)
|
||||
}
|
25
day-9/two.go
Normal file
25
day-9/two.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
// previous returns the previous value in the sequence
|
||||
func previous(nums []int) int {
|
||||
diffs := []int{}
|
||||
for i := 1; i < len(nums); i++ {
|
||||
diffs = append(diffs, nums[i]-nums[i-1])
|
||||
}
|
||||
|
||||
if allEqual(diffs) {
|
||||
return nums[0] - diffs[0]
|
||||
}
|
||||
return nums[0] - previous(diffs)
|
||||
}
|
||||
|
||||
// two returns the sum of all previous values
|
||||
func two(lines []string) int {
|
||||
sum := 0
|
||||
for _, line := range lines {
|
||||
nums := convertToInts(line)
|
||||
sum += previous(nums)
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
47
day-9/util.go
Normal file
47
day-9/util.go
Normal file
@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// readlines reads lines from stdin.
|
||||
// returns input as an array of strings
|
||||
func readlines() []string {
|
||||
lines := []string{}
|
||||
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
// convertToInts converts a string representing ints to an array of ints
|
||||
func convertToInts(line string) []int {
|
||||
numStr := strings.Split(line, " ")
|
||||
nums := make([]int, 0)
|
||||
for _, elem := range numStr {
|
||||
n, _ := strconv.Atoi(elem)
|
||||
nums = append(nums, n)
|
||||
}
|
||||
return nums
|
||||
}
|
||||
|
||||
// allEqual returns true if all values in array are equal
|
||||
func allEqual(a []int) bool {
|
||||
for i := 1; i < len(a); i++ {
|
||||
if a[i] != a[0] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
Loading…
Reference in New Issue
Block a user