Skip to content

Commit

Permalink
finished day 9
Browse files Browse the repository at this point in the history
  • Loading branch information
devries committed Dec 9, 2023
1 parent 9467549 commit fc1c2a2
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Advent of Code 2023

[![Tests](https://github.com/devries/advent_of_code_2023/actions/workflows/main.yml/badge.svg)](https://github.com/devries/advent_of_code_2023/actions/workflows/main.yml)
[![Stars: 16](https://img.shields.io/badge/⭐_Stars-16-yellow)](https://adventofcode.com/2023)
[![Stars: 18](https://img.shields.io/badge/⭐_Stars-18-yellow)](https://adventofcode.com/2023)

## Plan for This Year

Expand Down Expand Up @@ -141,3 +141,10 @@ the third run of my solution after compilation on my Raspberry Pi.
If the problem had not been contrived in that way, it is possible that there
would not have been a period over which all starting states eventually
synchronize so all ending states are reached at the same time.

- [Day 9: Mirage Maintenance](https://adventofcode.com/2023/day/9) - [part 1](day09p1/solution.go), [part 2](day09p2/solution.go)

I thought about trying to somehow be clever and calculate only as much as I had
to at the edges of the sequences, but then I thought each sequence is short so
I just followed the procedure in the example. It turned out to be very
straightforward.
55 changes: 55 additions & 0 deletions day09p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package day09p1

import (
"io"
"strconv"
"strings"

"aoc/utils"
)

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)

sum := 0
for _, ln := range lines {
parts := strings.Fields(ln)

values := []int{}

for _, p := range parts {
v, err := strconv.Atoi(p)
utils.Check(err, "Unable to convert %s to int", p)

values = append(values, v)
}
// Start differentiating
valueRows := [][]int{values}
for {
newvalues := make([]int, 0, len(values)-1)

allzeros := true
for i := 1; i < len(values); i++ {
nv := values[i] - values[i-1]
if nv != 0 {
allzeros = false
}
newvalues = append(newvalues, nv)
}
valueRows = append(valueRows, newvalues)
values = newvalues

if allzeros {
break
}
}

delta := 0
for i := len(valueRows) - 1; i >= 0; i-- {
delta = valueRows[i][len(valueRows[i])-1] + delta
}
sum += delta
}

return sum
}
35 changes: 35 additions & 0 deletions day09p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package day09p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 114},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}
55 changes: 55 additions & 0 deletions day09p2/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package day09p2

import (
"io"
"strconv"
"strings"

"aoc/utils"
)

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)

sum := 0
for _, ln := range lines {
parts := strings.Fields(ln)

values := []int{}

for _, p := range parts {
v, err := strconv.Atoi(p)
utils.Check(err, "Unable to convert %s to int", p)

values = append(values, v)
}
// Start differentiating
valueRows := [][]int{values}
for {
newvalues := make([]int, 0, len(values)-1)

allzeros := true
for i := 1; i < len(values); i++ {
nv := values[i] - values[i-1]
if nv != 0 {
allzeros = false
}
newvalues = append(newvalues, nv)
}
valueRows = append(valueRows, newvalues)
values = newvalues

if allzeros {
break
}
}

delta := 0
for i := len(valueRows) - 1; i >= 0; i-- {
delta = valueRows[i][0] - delta
}
sum += delta
}

return sum
}
35 changes: 35 additions & 0 deletions day09p2/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package day09p2

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 2},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}
2 changes: 1 addition & 1 deletion inputs

0 comments on commit fc1c2a2

Please sign in to comment.