Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day09 solutions #22

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/ints/ints.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ func Min(numbers []int) int {
}
return m
}

func AllSame(numbers []int) bool {
for i := 1; i < len(numbers); i++ {
if numbers[i] != numbers[i-1] {
return false
}
}
return true
}
20 changes: 20 additions & 0 deletions pkg/ints/ints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,23 @@ func TestAbs(t *testing.T) {
assert.Equal(t, expected, actual)
})
}

func TestAllSame(t *testing.T) {

t.Run("Should return true if all values in a slice are the same", func(t *testing.T) {
assert.True(t, AllSame([]int{1, 1, 1}))
})

t.Run("Should return false if all values in a slice are not the same", func(t *testing.T) {
assert.False(t, AllSame([]int{1, 2, 1}))
})
}

func TestMin(t *testing.T) {

t.Run("Should return the minimum value in a slice of ints", func(t *testing.T) {
expected := 1
actual := Min([]int{1, 2, 3})
assert.Equal(t, expected, actual)
})
}
83 changes: 83 additions & 0 deletions puzzles/day09/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"fmt"
"github.com/kylehoehns/aoc-2023-go/pkg/files"
"github.com/kylehoehns/aoc-2023-go/pkg/ints"
"slices"
"strings"
)

func main() {
fmt.Println("Part 1: ", performMirageMaintenance("input.txt", false))
fmt.Println("Part 2: ", performMirageMaintenance("input.txt", true))
}

func performMirageMaintenance(name string, reverse bool) int {
lines := files.ReadLines(name)
report := createEnvironmentalReport(lines, reverse)

sumOfPredicatedValues := 0
for _, history := range report.histories {
sumOfPredicatedValues += history.predictNextValue()
}

return sumOfPredicatedValues
}

type EnvironmentalReport struct {
histories []History
}

func createEnvironmentalReport(lines []string, reverse bool) EnvironmentalReport {
histories := make([]History, 0)
for _, line := range lines {
values := ints.FromStringSlice(strings.Fields(line))
if reverse {
slices.Reverse(values)
}
histories = append(histories, History{values})
}
return EnvironmentalReport{histories}
}

type History struct {
values []int
}

func (h History) predictNextValue() int {
allHistoryValues := h.extrapolateAndBuildAllHistories()
updatedHistories := make([][]int, len(allHistoryValues))

for i := len(allHistoryValues) - 1; i >= 0; i-- {
historyCopy := make([]int, len(allHistoryValues[i]))
copy(historyCopy, allHistoryValues[i])

if ints.AllSame(historyCopy) {
historyCopy = append(historyCopy, historyCopy[0])
} else {
lastValue := historyCopy[len(historyCopy)-1]
lastValueInRowBelow := updatedHistories[i+1][len(updatedHistories[i+1])-1]
historyCopy = append(historyCopy, lastValue+lastValueInRowBelow)
}
updatedHistories[i] = historyCopy
}
return updatedHistories[0][len(updatedHistories[0])-1]
}

func (h History) extrapolateAndBuildAllHistories() [][]int {
var allHistoryValues [][]int
allHistoryValues = append(allHistoryValues, h.values)
currentHistory := h

for !ints.AllSame(currentHistory.values) {
newValues := make([]int, len(currentHistory.values)-1)
for i := 1; i < len(currentHistory.values); i++ {
newValues[i-1] = currentHistory.values[i] - currentHistory.values[i-1]
}
currentHistory = History{newValues}
allHistoryValues = append(allHistoryValues, currentHistory.values)
}

return allHistoryValues
}
27 changes: 27 additions & 0 deletions puzzles/day09/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestPart1(t *testing.T) {

t.Run("Part 1", func(t *testing.T) {
expected := 114
actual := performMirageMaintenance("test-input.txt", false)
assert.Equal(t, expected, actual)
})

}

func TestPart2(t *testing.T) {

t.Run("Part 2", func(t *testing.T) {
expected := 2
actual := performMirageMaintenance("test-input.txt", true)
assert.Equal(t, expected, actual)
})

}
3 changes: 3 additions & 0 deletions puzzles/day09/test-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45