119 lines
2.2 KiB
Go
119 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"aoc/helper"
|
|
)
|
|
|
|
func main() {
|
|
grid := helper.ReadLinesToStringSlices()
|
|
|
|
// part 1
|
|
count := 0
|
|
for y := range grid {
|
|
for x := range grid[0] {
|
|
if grid[y][x] == 'X' || grid[y][x] == 'S' {
|
|
count += findXMAS(grid, x, y)
|
|
}
|
|
}
|
|
}
|
|
fmt.Printf("XMAS count: %d\n", count)
|
|
|
|
// part 2
|
|
count = 0
|
|
for y := 1; y < len(grid)-1; y++ { // skip first & last row
|
|
for x := 1; x < len(grid[0])-1; x++ { // skip first & last column
|
|
if grid[y][x] == 'A' {
|
|
count += findCrossMAS(grid, x, y)
|
|
}
|
|
}
|
|
}
|
|
fmt.Printf("X-MAS count: %d\n", count)
|
|
}
|
|
|
|
func findXMAS(grid []string, startX int, startY int) (count int) {
|
|
minX := 0 // upper and left border is 0
|
|
height := len(grid) - 1 // lower border is len-1 (index start = 0)
|
|
width := len(grid[0]) - 1 // right border is len-1 (index start = 0)
|
|
|
|
for _, XMAS := range [2]string{"XMAS", "SAMX"} {
|
|
// horizontal
|
|
if startX+3 <= width {
|
|
found := true
|
|
for i := range 4 {
|
|
if grid[startY][startX+i] != XMAS[i] {
|
|
found = false
|
|
break
|
|
}
|
|
}
|
|
if found {
|
|
count++
|
|
}
|
|
}
|
|
|
|
// vertikal
|
|
if startY+3 <= height {
|
|
found := true
|
|
for i := range 4 {
|
|
if grid[startY+i][startX] != XMAS[i] {
|
|
found = false
|
|
break
|
|
}
|
|
}
|
|
if found {
|
|
count++
|
|
}
|
|
}
|
|
|
|
// diagonal down right
|
|
if startY+3 <= height && startX+3 <= width {
|
|
found := true
|
|
for i := range 4 {
|
|
if grid[startY+i][startX+i] != XMAS[i] {
|
|
found = false
|
|
break
|
|
}
|
|
}
|
|
if found {
|
|
count++
|
|
}
|
|
}
|
|
|
|
// diagonal down left
|
|
if startY+3 <= height && startX-3 >= minX {
|
|
found := true
|
|
for i := range 4 {
|
|
if grid[startY+i][startX-i] != XMAS[i] {
|
|
found = false
|
|
break
|
|
}
|
|
}
|
|
if found {
|
|
count++
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
}
|
|
|
|
func findCrossMAS(grid []string, startX int, startY int) (count int) {
|
|
// check clockwise for
|
|
// M.M
|
|
// .A.
|
|
// S.S
|
|
// then rotate the outer ring clockwise and check again
|
|
for _, searchstring := range [4]string{"MMSS", "SMMS", "SSMM", "MSSM"} {
|
|
found := grid[startY-1][startX-1] == searchstring[0] &&
|
|
grid[startY-1][startX+1] == searchstring[1] &&
|
|
grid[startY+1][startX+1] == searchstring[2] &&
|
|
grid[startY+1][startX-1] == searchstring[3]
|
|
if found {
|
|
return 1
|
|
}
|
|
}
|
|
|
|
return 0
|
|
}
|