1
0
Fork 0
adventofcode/2024/04/main.go

120 lines
2.2 KiB
Go
Raw Normal View History

2024-12-04 14:19:33 +00:00
package main
import (
"fmt"
"aoc/helper"
)
func main() {
grid := helper.ReadLinesToStringSlices()
// part 1
count := 0
for y := range grid {
for x := range grid[0] {
2024-12-05 18:22:59 +00:00
if grid[y][x] == 'X' || grid[y][x] == 'S' {
2024-12-04 14:19:33 +00:00
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) {
2024-12-05 18:22:59 +00:00
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
}
2024-12-04 14:19:33 +00:00
}
2024-12-05 18:22:59 +00:00
if found {
count++
2024-12-04 14:19:33 +00:00
}
}
2024-12-05 18:22:59 +00:00
// vertikal
if startY+3 <= height {
found := true
for i := range 4 {
if grid[startY+i][startX] != XMAS[i] {
found = false
break
}
2024-12-04 14:19:33 +00:00
}
2024-12-05 18:22:59 +00:00
if found {
count++
2024-12-04 14:19:33 +00:00
}
}
2024-12-05 18:22:59 +00:00
// 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
}
2024-12-04 14:19:33 +00:00
}
2024-12-05 18:22:59 +00:00
if found {
count++
2024-12-04 14:19:33 +00:00
}
}
2024-12-05 18:22:59 +00:00
// 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
}
2024-12-04 14:19:33 +00:00
}
2024-12-05 18:22:59 +00:00
if found {
count++
2024-12-04 14:19:33 +00:00
}
}
2024-12-05 18:22:59 +00:00
2024-12-04 14:19:33 +00:00
}
return count
}
func findCrossMAS(grid []string, startX int, startY int) (count int) {
2024-12-05 18:22:59 +00:00
// check clockwise for
2024-12-04 14:19:33 +00:00
// M.M
// .A.
// S.S
2024-12-05 18:22:59 +00:00
// 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
}
2024-12-04 14:19:33 +00:00
}
2024-12-05 18:22:59 +00:00
return 0
2024-12-04 14:19:33 +00:00
}