1
0
Fork 0

update 2024/day4

This commit is contained in:
Sebastian Mark 2024-12-05 19:22:59 +01:00
parent c02820bcb9
commit e5f38abb8c

View file

@ -13,7 +13,7 @@ func main() {
count := 0
for y := range grid {
for x := range grid[0] {
if grid[y][x] == 'X' {
if grid[y][x] == 'X' || grid[y][x] == 'S' {
count += findXMAS(grid, x, y)
}
}
@ -33,20 +33,15 @@ func main() {
}
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)
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)
XMAS := "XMAS"
// horizontal right
if startX+3 <= maxX { // check for enough space (pos + 3 = 4 characters for "XMAS")
for _, XMAS := range [2]string{"XMAS", "SAMX"} {
// horizontal
if startX+3 <= width {
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
@ -57,22 +52,8 @@ func findXMAS(grid []string, startX int, startY int) (count int) {
}
}
// 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 {
// vertikal
if startY+3 <= height {
found := true
for i := range 4 {
if grid[startY+i][startX] != XMAS[i] {
@ -85,22 +66,8 @@ func findXMAS(grid []string, startX int, startY int) (count int) {
}
}
// 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 {
if startY+3 <= height && startX+3 <= width {
found := true
for i := range 4 {
if grid[startY+i][startX+i] != XMAS[i] {
@ -114,7 +81,7 @@ func findXMAS(grid []string, startX int, startY int) (count int) {
}
// diagonal down left
if startY+3 <= maxY && startX-3 >= minX {
if startY+3 <= height && startX-3 >= minX {
found := true
for i := range 4 {
if grid[startY+i][startX-i] != XMAS[i] {
@ -127,74 +94,26 @@ func findXMAS(grid []string, startX int, startY int) (count int) {
}
}
// 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++
}
// check clockwise for
// 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'
// 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 {
count++
return 1
}
}
// 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
return 0
}