1
0
Fork 0
advent-of-code-2024/04/4-2.rb

67 lines
1.1 KiB
Ruby
Raw Permalink Normal View History

2024-12-05 22:58:54 -05:00
#!/usr/bin/env ruby
problem = 4
input = File.readlines("#{problem}.input").map { _1.chomp.chars }.reject { _1.empty? }
#input = DATA.read.split("\n").map(&:chars)
mas = [["M", "M", "S", "S"], ["M", "S", "M", "S"],
["S", "M", "S", "M"], ["S", "S", "M", "M"]]
x_mas = 0
matches = []
(1...input.size-1).each do |y|
(1...input.first.size-1).each do |x|
next unless input[y][x] == "A"
corners = [
input.dig(y-1, x-1),
input.dig(y-1, x+1),
input.dig(y+1, x-1),
input.dig(y+1, x+1)
]
if match = mas.find { corners == _1 }
matches << [y,x]
x_mas += 1
end
end
end
relevant = matches.map do |match|
[
match,
[match[0] - 1, match[1] - 1],
[match[0] - 1, match[1] + 1],
[match[0] + 1, match[1] - 1],
[match[0] + 1, match[1] + 1]
]
end.flatten(1)
# input.each_with_index do |_, y|
# input[y].each_with_index do |v, x|
# if relevant.include?([y,x])
# print v
# else
# print "."
# end
# end
# puts
# end
p x_mas
# wrong
# - 1968
__END__
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX