advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

main.go (698B)

    1 package main
    2 
    3 import (
    4 	"fmt"
    5 )
    6 
    7 func main() {
    8 	input := []int{11, 0, 1, 10, 5, 19}
    9 	// 870
   10 	fmt.Println(part1(input))
   11 	// 9136
   12 	fmt.Println(part2(input))
   13 }
   14 
   15 func memoryGame(input []int, target int) int {
   16 	lastAppear := map[int]int{}
   17 	for i, v := range input[:len(input)-1] {
   18 		lastAppear[v] = i + 1
   19 	}
   20 	curr := input[len(input)-1]
   21 	for i := len(input); i < target; i++ {
   22 		prev := curr
   23 		if lastAppear[prev] == 0 {
   24 			curr = 0
   25 		} else {
   26 			curr = i - lastAppear[prev]
   27 		}
   28 		// Only update after we have the next number
   29 		lastAppear[prev] = i
   30 	}
   31 	return curr
   32 }
   33 
   34 func part1(input []int) int {
   35 	return memoryGame(input, 2020)
   36 }
   37 
   38 func part2(input []int) int {
   39 	return memoryGame(input, 30_000_000)
   40 }