You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2024/ruby/day_04.rb

43 lines
762 B

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