day15.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strings"
  7. )
  8. func readNumbers(startingNumbers string) (int, map[int][2]int) {
  9. lastNumber := 0
  10. numbersSpoken := make(map[int][2]int)
  11. for i, item := range strings.Split(string(startingNumbers), ",") {
  12. var number int
  13. n, err := fmt.Sscanf(item, "%d", &number)
  14. if err != nil || n < 1 {
  15. log.Fatal(err)
  16. }
  17. lastNumber = number
  18. numbersSpoken[number] = [2]int{i + 1, 0}
  19. }
  20. return lastNumber, numbersSpoken
  21. }
  22. func playGame(currentRound, end, lastNumber int, numbersSpoken map[int][2]int) (int, map[int][2]int) {
  23. var currentNumber int
  24. for ; currentRound <= end; currentRound++ {
  25. if spoken, ok := numbersSpoken[lastNumber]; !ok {
  26. currentNumber = 0
  27. } else {
  28. if spoken[1] == 0 {
  29. currentNumber = 0
  30. } else {
  31. currentNumber = spoken[1] - spoken[0]
  32. }
  33. }
  34. if _, ok := numbersSpoken[currentNumber]; !ok {
  35. numbersSpoken[currentNumber] = [2]int{currentRound, 0}
  36. } else {
  37. if numbersSpoken[currentNumber][1] == 0 {
  38. numbersSpoken[currentNumber] = [2]int{numbersSpoken[currentNumber][0], currentRound}
  39. } else {
  40. numbersSpoken[currentNumber] = [2]int{numbersSpoken[currentNumber][1], currentRound}
  41. }
  42. }
  43. lastNumber = currentNumber
  44. }
  45. return currentNumber, numbersSpoken
  46. }
  47. func main() {
  48. if len(os.Args) < 2 {
  49. log.Fatal("You need to specify starting numbers!")
  50. }
  51. lastNumber, numbersSpoken := readNumbers(os.Args[1])
  52. lastNumber, numbersSpoken = playGame(len(numbersSpoken)+1, 2020, lastNumber, numbersSpoken)
  53. fmt.Println("Part1:", lastNumber)
  54. lastNumber, numbersSpoken = playGame(2021, 30000000, lastNumber, numbersSpoken)
  55. fmt.Println("Part2:", lastNumber)
  56. }