2020 Day 15 Complete
This commit is contained in:
parent
0e60e19452
commit
2f6b8a688e
@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("# Day 13")
|
fmt.Println("# Day 14")
|
||||||
inp := h.StdinToStringSlice()
|
inp := h.StdinToStringSlice()
|
||||||
vsn := h.Atoi(h.OptArgNumber(1, "1"))
|
vsn := h.Atoi(h.OptArgNumber(1, "1"))
|
||||||
if vsn == 1 {
|
if vsn == 1 {
|
||||||
|
1
2020/day15/input
Normal file
1
2020/day15/input
Normal file
@ -0,0 +1 @@
|
|||||||
|
0,8,15,2,12,1,4
|
47
2020/day15/main.go
Normal file
47
2020/day15/main.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
1
2020/day15/testinput
Normal file
1
2020/day15/testinput
Normal file
@ -0,0 +1 @@
|
|||||||
|
0,3,6
|
Loading…
Reference in New Issue
Block a user