Day 15: Part 1

Signed-off-by: James Griffin <james@unsupervised.ca>
This commit is contained in:
2020-12-15 20:11:03 -04:00
parent 740bf24df6
commit 4733cfa321
5 changed files with 136 additions and 3 deletions

83
fifteen/day_fifteen.go Normal file
View File

@@ -0,0 +1,83 @@
package fifteen
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type game struct {
starting []int
numbers []int
spoken map[int]int
lastIndex map[int]int
}
func (g *game) load(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
input := strings.Split(scanner.Text(), ",")
for _, i := range input {
v, err := strconv.Atoi(i)
if err != nil {
return err
}
g.starting = append(g.starting, v)
}
}
return nil
}
func (g *game) valueAt(index int) int {
g.spoken = make(map[int]int, index)
g.lastIndex = make(map[int]int, index)
for i, n := range g.starting {
if i != len(g.starting)-1 {
g.spoken[n]++
}
g.lastIndex[n] = i
g.numbers = append(g.numbers, n)
}
for i := len(g.starting); i < index; i++ {
last := g.numbers[i-1]
g.spoken[last]++
lastIndex := g.lastIndex[last]
g.lastIndex[last] = i - 1
next := 0
if g.spoken[last] > 1 {
next = i - 1 - lastIndex
}
g.numbers = append(g.numbers, next)
}
if len(g.numbers) != index {
return -1
}
return g.numbers[index-1]
}
// PartOne What is the 2020th number given my input
func PartOne() string {
g := game{}
if err := g.load("fifteen/input.txt"); err != nil {
return err.Error()
}
return fmt.Sprintf("The 2020th number is %d", g.valueAt(2020))
}

View File

@@ -0,0 +1,45 @@
package fifteen
import "testing"
func Test_load_input(t *testing.T) {
g := game{}
if err := g.load("sample.txt"); err != nil {
t.Logf(err.Error())
t.FailNow()
}
if len(g.numbers) != 3 {
t.Logf("Expected 3 starting numbers, got %d", len(g.numbers))
t.FailNow()
}
}
func Test_get_guess1(t *testing.T) {
g := game{}
if err := g.load("sample.txt"); err != nil {
t.Logf(err.Error())
t.FailNow()
}
r := g.valueAt(9)
if r != 4 {
t.Logf("Expected 4, but got %d", r)
t.FailNow()
}
}
func Test_get_guess2(t *testing.T) {
g := game{}
if err := g.load("sample.txt"); err != nil {
t.Logf(err.Error())
t.FailNow()
}
r := g.valueAt(2020)
if r != 436 {
t.Logf("Expected 436, but got %d", r)
t.FailNow()
}
}

1
fifteen/input.txt Normal file
View File

@@ -0,0 +1 @@
18,8,0,5,4,1,20

1
fifteen/sample.txt Normal file
View File

@@ -0,0 +1 @@
0,3,6