|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +var chars [][]string |
| 10 | + |
| 11 | +func main() { |
| 12 | + filenames := []string{"testInput", "input"} |
| 13 | + for _, fileName := range filenames { |
| 14 | + fileContents, err := os.ReadFile(fileName) |
| 15 | + if err != nil { |
| 16 | + panic(err) |
| 17 | + } |
| 18 | + fmt.Println(fileName) |
| 19 | + lines := strings.Split(string(fileContents), "\n") |
| 20 | + chars = make([][]string, len(lines)) |
| 21 | + for index, line := range lines { |
| 22 | + lineChars := strings.Split(line, "") |
| 23 | + chars[index] = lineChars |
| 24 | + } |
| 25 | + var xmases int |
| 26 | + for y := 0; y < len(chars); y++ { |
| 27 | + for x := 0; x < len(chars[y]); x++ { |
| 28 | + xmases += countXMasAtMPosition(x, y) |
| 29 | + } |
| 30 | + } |
| 31 | + println(xmases) |
| 32 | + } |
| 33 | +} |
| 34 | +func countXMasAtMPosition(x int, y int) int { |
| 35 | + if chars[y][x] != "M" { |
| 36 | + return 0 |
| 37 | + } |
| 38 | + var xmases int |
| 39 | + //XMAS |
| 40 | + if x-1 >= 0 && x+2 < len(chars[y]) { |
| 41 | + if chars[y][x-1] == "X" && chars[y][x+1] == "A" && chars[y][x+2] == "S" { |
| 42 | + xmases++ |
| 43 | + } |
| 44 | + } |
| 45 | + //SAMX |
| 46 | + if x-2 >= 0 && x+1 < len(chars[y]) { |
| 47 | + if chars[y][x+1] == "X" && chars[y][x-1] == "A" && chars[y][x-2] == "S" { |
| 48 | + xmases++ |
| 49 | + } |
| 50 | + } |
| 51 | + //X |
| 52 | + //M |
| 53 | + //A |
| 54 | + //S |
| 55 | + if y-1 >= 0 && y+2 < len(chars) { |
| 56 | + if chars[y-1][x] == "X" && chars[y+1][x] == "A" && chars[y+2][x] == "S" { |
| 57 | + xmases++ |
| 58 | + } |
| 59 | + } |
| 60 | + //S |
| 61 | + //A |
| 62 | + //M |
| 63 | + //X |
| 64 | + if y-2 >= 0 && y+1 < len(chars) { |
| 65 | + if chars[y+1][x] == "X" && chars[y-1][x] == "A" && chars[y-2][x] == "S" { |
| 66 | + xmases++ |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + //X |
| 71 | + // M |
| 72 | + // A |
| 73 | + // S |
| 74 | + if x-1 >= 0 && x+2 < len(chars[y]) && y-1 >= 0 && y+2 < len(chars) { |
| 75 | + if chars[y-1][x-1] == "X" && chars[y+1][x+1] == "A" && chars[y+2][x+2] == "S" { |
| 76 | + xmases++ |
| 77 | + } |
| 78 | + } |
| 79 | + //S |
| 80 | + // A |
| 81 | + // M |
| 82 | + // X |
| 83 | + if x-2 >= 0 && x+1 < len(chars[y]) && y-2 >= 0 && y+1 < len(chars) { |
| 84 | + if chars[y+1][x+1] == "X" && chars[y-1][x-1] == "A" && chars[y-2][x-2] == "S" { |
| 85 | + xmases++ |
| 86 | + } |
| 87 | + } |
| 88 | + // X |
| 89 | + // M |
| 90 | + // A |
| 91 | + //S |
| 92 | + if y-1 >= 0 && y+2 < len(chars) && x-2 >= 0 && x+1 < len(chars[y]) { |
| 93 | + if chars[y-1][x+1] == "X" && chars[y+1][x-1] == "A" && chars[y+2][x-2] == "S" { |
| 94 | + xmases++ |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // S |
| 99 | + // A |
| 100 | + // M |
| 101 | + //X |
| 102 | + if y+1 < len(chars) && y-2 >= 0 && x-1 >= 0 && x+2 < len(chars[y]) { |
| 103 | + if chars[y+1][x-1] == "X" && chars[y-1][x+1] == "A" && chars[y-2][x+2] == "S" { |
| 104 | + xmases++ |
| 105 | + } |
| 106 | + } |
| 107 | + return xmases |
| 108 | +} |
0 commit comments