-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4b.rb
26 lines (21 loc) · 766 Bytes
/
4b.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$puzzle = File.readlines('inputs/4').map(&:chomp)
$rows = $puzzle.length
$cols = $puzzle[0].length
def xmas?(row, col)
# Out of bounds
if row - 1 < 0 or row + 1 >= $rows or col - 1 < 0 or col + 1 >= $cols
return false
end
# A possible x-mas cross
if $puzzle[row][col] == "A"
return (($puzzle[row-1][col-1] == "M" && $puzzle[row+1][col+1] == "S") || ($puzzle[row-1][col-1] == "S" && $puzzle[row+1][col+1] == "M")) &&
(($puzzle[row+1][col-1] == "M" && $puzzle[row-1][col+1] == "S") || ($puzzle[row+1][col-1] == "S" && $puzzle[row-1][col+1] == "M"))
end
return false
end
count = $puzzle.each_with_index.sum do |row, row_index|
row.each_char.with_index.count do |_, col_index|
xmas?(row_index, col_index)
end
end
puts count