--- Day 4: Ceres Search ---

"Looks like the Chief's not here. Next!" One of The Historians pulls out a device and pushes the only button on it. After a brief flash, you recognize the interior of the Ceres monitoring station!

As the search for the Chief continues, a small Elf who lives on the station tugs on your shirt; she'd like to know if you could help her with her word search (your puzzle input). She only has to find one word: XMAS.

This word search allows words to be horizontal, vertical, diagonal, written backwards, or even overlapping other words. It's a little unusual, though, as you don't merely need to find one instance of XMAS - you need to find all of them. Here are a few ways XMAS might appear, where irrelevant characters have been replaced with .:

..X...
.SAMX.
.A..A.
XMAS.S
.X....
The actual word search will be full of letters instead. For example:

MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
In this word search, XMAS occurs a total of 18 times; here's the same word search again, but where letters not involved in any XMAS have been replaced with .:

....XXMAS.
.SAMXMS...
...S..A...
..A.A.MS.X
XMASAMX.MM
X.....XA.A
S.S.S.S.SS
.A.A.A.A.A
..M.M.M.MM
.X.X.XMASX
Take a look at the little Elf's word search. How many times does XMAS appear?

Your puzzle answer was 2434.

--- Part Two ---

The Elf looks quizzically at you. Did you misunderstand the assignment?

Looking for the instructions, you flip over the word search to find that this isn't actually an XMAS puzzle; it's an X-MAS puzzle in which you're supposed to find two MAS in the shape of an X. One way to achieve that is like this:

M.S
.A.
M.S
Irrelevant characters have again been replaced with . in the above diagram. Within the X, each MAS can be written forwards or backwards.

Here's the same example from before, but this time all of the X-MASes have been kept instead:

.M.S......
..A..MSMS.
.M.S.MAA..
..A.ASMSM.
.M.S.M....
..........
S.S.S.S.S.
.A.A.A.A..
M.M.M.M.M.
..........
In this example, an X-MAS appears 9 times.

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?
input = open('./input.txt').readlines
input.map!(&:strip)

# Read in and seach vertincally (backwards and forwards)
count = 0
# input.each_with_index do | line, index |
#     count += line.scan(/XMAS/).size
#     count += line.reverse.scan(/XMAS/).size
#     input[index] = line.split("")
# end

# # Rotate array by 90
# rot_arr = []
# input.transpose.each do | line |
#     rot_arr << line.reverse
# end

# rot_arr.each do | line |
#     line = line.join()
#     puts line
#     count += line.scan(/XMAS/).size
#     count += line.reverse.scan(/XMAS/).size
# end

# # Rotate array by -45
# ang_arr = []
# max_x = input[0].length - 1
# max_y = input.length - 1
# max_loop = max_x + max_y + 1
# bound = [max_x, max_y].min

# xx = 0
# yy = 0

# max_loop.times do | t |
#     ang = []
#     x = 0
#     x = t - yy if t > yy
#     y = yy
#     t = max_loop - t - 1 if t > bound
#     (t + 1).times do
#         puts t, [x, y].inspect
#         ang << rot_arr[y][x]
#         y -= 1
#         x += 1
#     end
#     ang_arr << ang.join('')
#     puts ang_arr.inspect
#     if(yy > max_x)
#         xx += 1
#     end
#     if (yy < max_y)
#         yy += 1
#     end
# end

# puts ang_arr

# ang_arr.each do | line |
#     count += line.scan(/XMAS/).size
#     count += line.reverse.scan(/XMAS/).size
# end

# # Rotate array by 45
# ang_arr = []
# max_x = input[0].length - 1
# max_y = input.length - 1
# max_loop = max_x + max_y + 1
# bound = [max_x, max_y].min

# xx = 0
# yy = 0

# max_loop.times do | t |
#     ang = []
#     x = 0
#     x = t - yy if t > yy
#     y = yy
#     t = max_loop - t - 1 if t > bound
#     (t + 1).times do
#         puts t, [x, y].inspect
#         ang << input[y][x]
#         y -= 1
#         x += 1
#     end
#     ang_arr << ang.join('')
#     puts ang_arr.inspect
#     if(yy > max_x)
#         xx += 1
#     end
#     if (yy < max_y)
#         yy += 1
#     end
# end

# puts ang_arr

# ang_arr.each do | line |
#     count += line.scan(/XMAS/).size
#     count += line.reverse.scan(/XMAS/).size
# end

max_x = input[0].length
max_y = input.length
counter = 0
(max_y - 2).times do | y |
    (max_x - 2).times do | x |
        scanner = ""
        scanner += input[y][x..(x+2)]
        scanner += input[y + 1][x..(x+2)]
        scanner += input[y + 2][x..(x+2)]
        puts scanner
        # M.M S.M S.S M.S
        # .A. .A. .A. .A.
        # S.S S.M M.M M.S
        count += scanner.scan(/M.M.A.S.S/).size
        count += scanner.scan(/S.M.A.S.M/).size
        count += scanner.scan(/S.S.A.M.M/).size
        count += scanner.scan(/M.S.A.M.S/).size
        counter += 1
    end
end


puts counter, count