201 lines
3.5 KiB
Go
201 lines
3.5 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' {
|
||
|
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, minY := 0, 0 // upper and left border is 0
|
||
|
maxY := len(grid) - 1 // lower border is len-1 (index start = 0)
|
||
|
maxX := len(grid[0]) - 1 // right border is len-1 (index start = 0)
|
||
|
|
||
|
XMAS := "XMAS"
|
||
|
|
||
|
// horizontal right
|
||
|
if startX+3 <= maxX { // check for enough space (pos + 3 = 4 characters for "XMAS")
|
||
|
found := true
|
||
|
for i := range 4 {
|
||
|
// check if gridposition at "i" has character for position "i"
|
||
|
// i=0 -> XMAS[0] -> "X"
|
||
|
// ..
|
||
|
// i=3 -> XMAS[3] -> "S"
|
||
|
if grid[startY][startX+i] != XMAS[i] {
|
||
|
found = false
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if found {
|
||
|
count++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// horizontal left
|
||
|
if startX-3 >= minX {
|
||
|
found := true
|
||
|
for i := range 4 {
|
||
|
if grid[startY][startX-i] != XMAS[i] {
|
||
|
found = false
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if found {
|
||
|
count++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// vertikal down
|
||
|
if startY+3 <= maxY {
|
||
|
found := true
|
||
|
for i := range 4 {
|
||
|
if grid[startY+i][startX] != XMAS[i] {
|
||
|
found = false
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if found {
|
||
|
count++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// vertikal up
|
||
|
if startY-3 >= minY {
|
||
|
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 <= maxY && startX+3 <= maxX {
|
||
|
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 <= maxY && startX-3 >= minX {
|
||
|
found := true
|
||
|
for i := range 4 {
|
||
|
if grid[startY+i][startX-i] != XMAS[i] {
|
||
|
found = false
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if found {
|
||
|
count++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// diagonal up right
|
||
|
if startY-3 >= minY && startX+3 <= maxX {
|
||
|
found := true
|
||
|
for i := range 4 {
|
||
|
if grid[startY-i][startX+i] != XMAS[i] {
|
||
|
found = false
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if found {
|
||
|
count++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// diagonal up left
|
||
|
if startY-3 >= minY && 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) {
|
||
|
|
||
|
// M.S
|
||
|
// .A.
|
||
|
// M.S
|
||
|
found := grid[startY-1][startX-1] == 'M' && grid[startY-1][startX+1] == 'S' &&
|
||
|
grid[startY+1][startX-1] == 'M' && grid[startY+1][startX+1] == 'S'
|
||
|
if found {
|
||
|
count++
|
||
|
}
|
||
|
|
||
|
// M.M
|
||
|
// .A.
|
||
|
// S.S
|
||
|
found = grid[startY-1][startX-1] == 'M' && grid[startY-1][startX+1] == 'M' &&
|
||
|
grid[startY+1][startX-1] == 'S' && grid[startY+1][startX+1] == 'S'
|
||
|
if found {
|
||
|
count++
|
||
|
}
|
||
|
|
||
|
// S.M
|
||
|
// .A.
|
||
|
// S.M
|
||
|
found = grid[startY-1][startX-1] == 'S' && grid[startY-1][startX+1] == 'M' &&
|
||
|
grid[startY+1][startX-1] == 'S' && grid[startY+1][startX+1] == 'M'
|
||
|
if found {
|
||
|
count++
|
||
|
}
|
||
|
|
||
|
// S.S
|
||
|
// .A.
|
||
|
// M.M
|
||
|
found = grid[startY-1][startX-1] == 'S' && grid[startY-1][startX+1] == 'S' &&
|
||
|
grid[startY+1][startX-1] == 'M' && grid[startY+1][startX+1] == 'M'
|
||
|
if found {
|
||
|
count++
|
||
|
}
|
||
|
|
||
|
return count
|
||
|
}
|