code.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. func readInput(file *os.File) [][]int {
  10. scanner := bufio.NewScanner(file)
  11. var matrix [][]int
  12. for scanner.Scan() {
  13. line := scanner.Text()
  14. if line == "" {
  15. break
  16. }
  17. parts := strings.Split(line, " ")
  18. if len(parts) == 0 {
  19. log.Fatalf("Wrong input: %s", line)
  20. }
  21. var numbers []int
  22. for i := range parts {
  23. var number int
  24. n, err := fmt.Sscanf(parts[i], "%d", &number)
  25. if n != 1 || err != nil {
  26. log.Fatalf("Failed to read number: %s\n%s", parts[i], err)
  27. }
  28. numbers = append(numbers, number)
  29. }
  30. matrix = append(matrix, numbers)
  31. }
  32. return matrix
  33. }
  34. func allZeros(numbers []int) bool {
  35. for i := range numbers {
  36. if numbers[i] != 0 {
  37. return false
  38. }
  39. }
  40. return true
  41. }
  42. func buildNewMatrix(numbers []int) [][]int {
  43. var matrix [][]int
  44. matrix = append(matrix, numbers)
  45. level := 0
  46. for {
  47. if level >= len(matrix) {
  48. break
  49. }
  50. var newLevel []int
  51. for i := 1; i < len(matrix[level]); i++ {
  52. newLevel = append(newLevel, matrix[level][i]-matrix[level][i-1])
  53. }
  54. matrix = append(matrix, newLevel)
  55. if allZeros(newLevel) {
  56. break
  57. }
  58. level++
  59. }
  60. return matrix
  61. }
  62. func getNextAndPrevious(numbers []int) (int, int) {
  63. matrix := buildNewMatrix(numbers)
  64. bottom := len(matrix) - 1
  65. for i := bottom; i > 0; i-- {
  66. matrix[i-1] = append(matrix[i-1], matrix[i][len(matrix[i])-1]+matrix[i-1][len(matrix[i-1])-1])
  67. }
  68. for i := bottom; i > 0; i-- {
  69. matrix[i-1] = append([]int{matrix[i-1][0] - matrix[i][0]}, matrix[i-1]...)
  70. }
  71. return matrix[0][len(matrix[0])-1], matrix[0][0]
  72. }
  73. func parts(matrix [][]int) (int, int) {
  74. var part1, part2 int
  75. for i := range matrix {
  76. next, previous := getNextAndPrevious(matrix[i])
  77. part1 += next
  78. part2 += previous
  79. }
  80. return part1, part2
  81. }
  82. func main() {
  83. if len(os.Args) < 2 {
  84. log.Fatal("You need to specify a file!")
  85. }
  86. filePath := os.Args[1]
  87. file, err := os.Open(filePath)
  88. if err != nil {
  89. log.Fatalf("Failed to open %s!\n", filePath)
  90. }
  91. matrix := readInput(file)
  92. part1, part2 := parts(matrix)
  93. fmt.Println("Part1:", part1)
  94. fmt.Println("Part2:", part2)
  95. }