Advent of Code 2024 Day 4

Author

Nathan Moore

— Part Two —

oh, actually, we are looking for X-MAS i.e. two MAS in a X shape.

Flip the word search from the instructions back over to the word search side and try again. How many times does an X-MAS appear?

x_count = 0

for i in range(1,len(inp)-1): 
    for j in range(1,len(inp[0])-1): 
        # this time we are looking for A
        # but it is also symmetrical so we can not be in first or last
        if grid[i,j] == 'A':
            # if any of the surrounds are X then we don't have a thing
            if (grid[i-1,j-1] == 'X' or grid[i-1,j+1] == 'X' or
                grid[i+1,j-1] == 'X' or grid[i+1,j+1] == 'X'):
                    pass
            # M over S
            elif (grid[i-1,j-1] == 'M' and grid[i-1,j+1] == 'M' and
                  grid[i+1,j-1] == 'S' and grid[i+1,j+1] == 'S'):
                x_count += 1
            # S over M
            elif (grid[i-1,j-1] == 'S' and grid[i-1,j+1] == 'S' and
                  grid[i+1,j-1] == 'M' and grid[i+1,j+1] == 'M'):
                x_count += 1
            # M then S
            elif (grid[i-1,j-1] == 'M' and grid[i-1,j+1] == 'S' and
                  grid[i+1,j-1] == 'M' and grid[i+1,j+1] == 'S'):
                x_count += 1
            # S then M
            elif (grid[i-1,j-1] == 'S' and grid[i-1,j+1] == 'M' and
                  grid[i+1,j-1] == 'S' and grid[i+1,j+1] == 'M'):
                x_count += 1
            
            
# 4947 A in the grid
x_count

# 3115 too high
# fix the typo making sure we use and
# 2000
2000