update 2024/day4
This commit is contained in:
parent
c02820bcb9
commit
e5f38abb8c
1 changed files with 60 additions and 141 deletions
123
2024/04/main.go
123
2024/04/main.go
|
@ -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,20 +33,15 @@ 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 {
|
||||||
// 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] {
|
if grid[startY][startX+i] != XMAS[i] {
|
||||||
found = false
|
found = false
|
||||||
break
|
break
|
||||||
|
@ -57,22 +52,8 @@ func findXMAS(grid []string, startX int, startY int) (count int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// horizontal left
|
// vertikal
|
||||||
if startX-3 >= minX {
|
if startY+3 <= height {
|
||||||
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
|
found := true
|
||||||
for i := range 4 {
|
for i := range 4 {
|
||||||
if grid[startY+i][startX] != XMAS[i] {
|
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
|
// diagonal down right
|
||||||
if startY+3 <= maxY && startX+3 <= maxX {
|
if startY+3 <= height && startX+3 <= width {
|
||||||
found := true
|
found := true
|
||||||
for i := range 4 {
|
for i := range 4 {
|
||||||
if grid[startY+i][startX+i] != XMAS[i] {
|
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
|
// diagonal down left
|
||||||
if startY+3 <= maxY && startX-3 >= minX {
|
if startY+3 <= height && startX-3 >= minX {
|
||||||
found := true
|
found := true
|
||||||
for i := range 4 {
|
for i := range 4 {
|
||||||
if grid[startY+i][startX-i] != XMAS[i] {
|
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
|
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"} {
|
||||||
|
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 {
|
if found {
|
||||||
count++
|
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
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue