From 4a7c9082ba71c4bed73731ccd914f7f1bf9c412c Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sat, 9 Dec 2023 17:25:53 +0000 Subject: [PATCH] day-9 --- day-9/go.mod | 3 +++ day-9/one.go | 25 +++++++++++++++++++++++++ day-9/run.sh | 5 +++++ day-9/solution.go | 21 +++++++++++++++++++++ day-9/two.go | 25 +++++++++++++++++++++++++ day-9/util.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 day-9/go.mod create mode 100644 day-9/one.go create mode 100644 day-9/run.sh create mode 100644 day-9/solution.go create mode 100644 day-9/two.go create mode 100644 day-9/util.go diff --git a/day-9/go.mod b/day-9/go.mod new file mode 100644 index 0000000..2ec3aca --- /dev/null +++ b/day-9/go.mod @@ -0,0 +1,3 @@ +module github.com/onyx-and-iris/aoc2023/day-9 + +go 1.20 diff --git a/day-9/one.go b/day-9/one.go new file mode 100644 index 0000000..a35fc56 --- /dev/null +++ b/day-9/one.go @@ -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 +} diff --git a/day-9/run.sh b/day-9/run.sh new file mode 100644 index 0000000..3f6776a --- /dev/null +++ b/day-9/run.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +INPUT="input.txt" + +cat $INPUT | go run . \ No newline at end of file diff --git a/day-9/solution.go b/day-9/solution.go new file mode 100644 index 0000000..926f1d4 --- /dev/null +++ b/day-9/solution.go @@ -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) +} diff --git a/day-9/two.go b/day-9/two.go new file mode 100644 index 0000000..b0921e5 --- /dev/null +++ b/day-9/two.go @@ -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 +} diff --git a/day-9/util.go b/day-9/util.go new file mode 100644 index 0000000..9e41e11 --- /dev/null +++ b/day-9/util.go @@ -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 +}