code.go 2.4 KB

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