diff --git a/2020/day14/main.go b/2020/day14/main.go index a67a2c1..7b02e65 100644 --- a/2020/day14/main.go +++ b/2020/day14/main.go @@ -8,7 +8,7 @@ import ( ) func main() { - fmt.Println("# Day 13") + fmt.Println("# Day 14") inp := h.StdinToStringSlice() vsn := h.Atoi(h.OptArgNumber(1, "1")) if vsn == 1 { diff --git a/2020/day15/input b/2020/day15/input new file mode 100644 index 0000000..5c1cb85 --- /dev/null +++ b/2020/day15/input @@ -0,0 +1 @@ +0,8,15,2,12,1,4 diff --git a/2020/day15/main.go b/2020/day15/main.go new file mode 100644 index 0000000..9895830 --- /dev/null +++ b/2020/day15/main.go @@ -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 + } +} diff --git a/2020/day15/testinput b/2020/day15/testinput new file mode 100644 index 0000000..c84ffe7 --- /dev/null +++ b/2020/day15/testinput @@ -0,0 +1 @@ +0,3,6