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 count := 0
for y := range grid { for y := range grid {
for x := range grid[0] { for x := range grid[0] {
if grid[y][x] == 'X' { if grid[y][x] == 'X' || grid[y][x] == 'S' {
count += findXMAS(grid, x, y) count += findXMAS(grid, x, y)
} }
} }
@ -33,168 +33,87 @@ func main() {
} }
func findXMAS(grid []string, startX int, startY int) (count int) { func findXMAS(grid []string, startX int, startY int) (count int) {
minX, minY := 0, 0 // upper and left border is 0 minX := 0 // upper and left border is 0
maxY := len(grid) - 1 // lower border is len-1 (index start = 0) height := len(grid) - 1 // lower border is len-1 (index start = 0)
maxX := len(grid[0]) - 1 // right border is len-1 (index start = 0) width := len(grid[0]) - 1 // right border is len-1 (index start = 0)
XMAS := "XMAS" for _, XMAS := range [2]string{"XMAS", "SAMX"} {
// horizontal
// horizontal right if startX+3 <= width {
if startX+3 <= maxX { // check for enough space (pos + 3 = 4 characters for "XMAS") found := true
found := true for i := range 4 {
for i := range 4 { if grid[startY][startX+i] != XMAS[i] {
// check if gridposition at "i" has character for position "i" found = false
// i=0 -> XMAS[0] -> "X" break
// .. }
// i=3 -> XMAS[3] -> "S" }
if grid[startY][startX+i] != XMAS[i] { if found {
found = false count++
break
} }
} }
if found {
count++
}
}
// horizontal left // vertikal
if startX-3 >= minX { if startY+3 <= height {
found := true found := true
for i := range 4 { for i := range 4 {
if grid[startY][startX-i] != XMAS[i] { if grid[startY+i][startX] != XMAS[i] {
found = false found = false
break break
}
}
if found {
count++
} }
} }
if found {
count++
}
}
// vertikal down // diagonal down right
if startY+3 <= maxY { if startY+3 <= height && startX+3 <= width {
found := true found := true
for i := range 4 { for i := range 4 {
if grid[startY+i][startX] != XMAS[i] { if grid[startY+i][startX+i] != XMAS[i] {
found = false found = false
break break
}
}
if found {
count++
} }
} }
if found {
count++
}
}
// vertikal up // diagonal down left
if startY-3 >= minY { if startY+3 <= height && startX-3 >= minX {
found := true found := true
for i := range 4 { for i := range 4 {
if grid[startY-i][startX] != XMAS[i] { if grid[startY+i][startX-i] != XMAS[i] {
found = false found = false
break break
}
}
if found {
count++
} }
} }
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 return count
} }
func findCrossMAS(grid []string, startX int, startY int) (count int) { func findCrossMAS(grid []string, startX int, startY int) (count int) {
// check clockwise for
// 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 // M.M
// .A. // .A.
// S.S // S.S
found = grid[startY-1][startX-1] == 'M' && grid[startY-1][startX+1] == 'M' && // then rotate the outer ring clockwise and check again
grid[startY+1][startX-1] == 'S' && grid[startY+1][startX+1] == 'S' for _, searchstring := range [4]string{"MMSS", "SMMS", "SSMM", "MSSM"} {
if found { found := grid[startY-1][startX-1] == searchstring[0] &&
count++ 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
}
} }
// S.M return 0
// .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
} }