#!/usr/bin/env ruby @letters = {} data = File.read("data.txt") data = data.split("\n") data = data.map(&:chars) data.each_with_index do |row, y| row.each_with_index do |letter, x| @letters[[x,y]] = letter end end found = 0 potential_starts = @letters.select{|key, val| val == "A" } potential_starts.each do |key, value| score = 0 if @letters[[key[0] - 1, key[1] - 1]] == "M" && @letters[[key[0] + 1, key[1] + 1]] == "S" score = score + 1 end if @letters[[key[0] - 1, key[1] - 1]] == "S" && @letters[[key[0] + 1, key[1] + 1]] == "M" score = score + 1 end if @letters[[key[0] + 1, key[1] - 1]] == "M" && @letters[[key[0] - 1, key[1] + 1]] == "S" score = score + 1 end if @letters[[key[0] + 1, key[1] - 1]] == "S" && @letters[[key[0] - 1, key[1] + 1]] == "M" score = score + 1 end found += 1 if score >= 2 end p found