adventofcode/2020/day15/main.go

48 lines
856 B
Go
Raw Normal View History

2020-12-15 14:41:00 +00:00
package main
import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
fmt.Println("# Day 15")
inp := h.StdinToString()
fmt.Println("## Part 1")
dumbFind(inp, 2020)
fmt.Println("## Part 2")
dumbFind(inp, 30000000)
}
func dumbFind(inp string, num int) {
start := strings.Split(inp, ",")
rem := make(map[int][]int)
turn := 1
var last int
for k := range start {
last = h.Atoi(start[k])
rem[last] = []int{turn}
turn++
}
for turn <= num {
last = speakNext(last, turn, &rem)
turn++
}
fmt.Println("Answer:", last)
}
func speakNext(prev int, turn int, rem *map[int][]int) int {
wrk := *rem
if len(wrk[prev]) == 1 {
wrk[0] = append(wrk[0], turn)
return 0
} else {
last := wrk[prev][len(wrk[prev])-2]
diff := (turn - 1) - last
wrk[diff] = append(wrk[diff], turn)
return diff
}
}