input = DATA.readlines(chomp: true) .flat_map.with_index {|row, y| row.chars.map.with_index {|chr, x| [[y,x], chr] } }.to_h # part one dirs = (-1..1).flat_map {|dy| (-1..1).map {|dx| [dy,dx] }} dirs.delete([0,0]) dirs = dirs.map {|dy, dx| 4.times.map {|i| [dy*i, dx*i] } } pp input.keys.sum {|y, x| dirs.count {|deltas| deltas.map {|dy, dx| input[[y+dy, x+dx]] }.join == "XMAS" } } # part two diagonals = [ [[-1, 1], [1, -1]], [[-1, -1], [1, 1]], ] pp input.keys.select {|y, x| input[[y,x]] == ?A && diagonals.all? {|deltas| deltas.map {|dy,dx| input[[y+dy, x+dx]] }.to_set == Set["M", "S"] } }.size __END__ MMMSXXMASM MSAMXMSMSA AMXSXMAAMM MSAMASMSMX XMASAMXAMM XXAMMXXAMA SMSMSASXSS SAXAMASAAA MAMMMXMMMM MXMXAXMASX