diff --git a/2024/04/main.go b/2024/04/main.go index f03318a..e5264e0 100644 --- a/2024/04/main.go +++ b/2024/04/main.go @@ -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,168 +33,87 @@ 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") - 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 + 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++ } } - 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 + // vertikal + if startY+3 <= height { + found := true + for i := range 4 { + if grid[startY+i][startX] != XMAS[i] { + found = false + break + } + } + if found { + count++ } } - 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 + // 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++ } } - 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 + // 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++ } } - 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++ - } - + // 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' - if found { - count++ + // 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 + } } - // 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 }