Skip to content

Commit

Permalink
Y2020D15, and go.mod +spew
Browse files Browse the repository at this point in the history
  • Loading branch information
janreggie committed Dec 15, 2020
1 parent 4a95b6d commit 93befb0
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 2 deletions.
2 changes: 1 addition & 1 deletion aoc2020/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* [*] Day 12: Rain Risk
* [*] Day 13: Shuttle Search
* [*] Day 14: Docking Data
* [ ] Day 15: (to be unlocked)
* [*] Day 15: Rambunctious Recitation
* [ ] Day 16: (to be unlocked)
* [ ] Day 17: (to be unlocked)
* [ ] Day 18: (to be unlocked)
Expand Down
68 changes: 68 additions & 0 deletions aoc2020/day15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package aoc2020

import (
"bufio"
"strconv"
"strings"

"github.com/pkg/errors"
)

// Day15 solves the fifteenth day puzzle "Rambunctious Recitation"
//
// Input
//
// A single line containing the "starting numbers" of the memory game. For example:
//
// some sample input indented to become a code block
//
// It is guaranteed that the numbers are nonnegative.
func Day15(scanner *bufio.Scanner) (answer1, answer2 string, err error) {
scanner.Scan()
rawNums := strings.Split(scanner.Text(), ",")
var memory [30000000]int
whenLastSaid := make(map[int]int)
for ii, vv := range rawNums {
var e error
memory[ii], e = strconv.Atoi(vv)
if e != nil {
err = errors.Wrapf(e, "couldn't parse %s from input", vv)
return
}
whenLastSaid[memory[ii]] = ii
}
ind := len(rawNums) // at where shall we start?
lastSpoken := memory[ind-1]

// findWhenLastSaid finds the largest index less than ind that num appears in memory.
// It will first check in allSaid if it is even worth in finding it.
// If such "does not exist" i.e., first timer, return -1.
findWhenLastSaid := func(ind, num int) int {
if vv, ok := whenLastSaid[num]; ok && vv <= ind-2 {
return vv
}
return -1
}

// bar := progressbar.Default(int64(len(memory)))
for ind < len(memory) {
if ff := findWhenLastSaid(ind, lastSpoken); ff == -1 {
memory[ind] = 0
} else {
memory[ind] = ind - 1 - ff
}
whenLastSaid[memory[ind-1]] = ind - 1 // memory[ind-1] used to avoid writing the current no. for the first time
lastSpoken = memory[ind]
ind++

if ind%10000 == 0 {
// bar.Add(10000)
}
}
// bar.Finish()

answer1 = strconv.Itoa(memory[2019])
answer2 = strconv.Itoa(memory[29999999])

return
}
29 changes: 29 additions & 0 deletions aoc2020/day15_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package aoc2020

import (
"testing"

"github.com/janreggie/aoc/internal"
"github.com/stretchr/testify/assert"
)

func TestDay15(t *testing.T) {
assert := assert.New(t)
testCases := []internal.TestCase{
{
Details: "Y2020D15 sample input",
Input: day15sampleInput,
Result1: "436",
Result2: "175594",
},
{
Details: "Y2020D15 my input",
Input: day15myInput,
Result1: "260",
Result2: "950",
},
}
for _, tt := range testCases {
tt.Test(Day15, assert)
}
}
3 changes: 3 additions & 0 deletions aoc2020/inputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9356,3 +9356,6 @@ mem[6578] = 469499
mem[60824] = 1460
mem[50713] = 7725506
`

const day15sampleInput = `0,3,6`
const day15myInput = `13,0,10,12,1,5,8`
2 changes: 1 addition & 1 deletion aoc2020/solutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var AllSolutions = []func(*bufio.Scanner) (string, string, error){
Day12, // day 12
Day13, // day 13
Day14, // day 14
Unimplemented, // day 15
Day15, // day 15
Unimplemented, // day 16
Unimplemented, // day 17
Unimplemented, // day 18
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.13

require (
github.com/antonholmquist/jason v1.0.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/antonholmquist/jason v1.0.0 h1:Ytg94Bcf1Bfi965K2q0s22mig/n4eGqEij/atE
github.com/antonholmquist/jason v1.0.0/go.mod h1:+GxMEKI0Va2U8h3os6oiUAetHAlGMvxjdpAH/9uvUMA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
1 change: 1 addition & 0 deletions inputs/2020_15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13,0,10,12,1,5,8

0 comments on commit 93befb0

Please sign in to comment.