Skip to content

Commit

Permalink
Year 2024 Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
tudorpavel committed Dec 4, 2024
1 parent 26f5820 commit 5546c1e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
53 changes: 53 additions & 0 deletions 2024/day04.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
input = STDIN.read
mat = input.split("\n").map(&:chars)

# Part 1
def count_for(input)
(input + "\n" + input.reverse).scan('XMAS').size
end

def make_input_diag(mat)
input_diag = []
0.upto(mat.first.size - 4) do |col|
i = 0
j = col
while i < mat.size && j < mat.first.size
input_diag << mat[i][j]
i += 1
j += 1
end
input_diag << '|'
end
1.upto(mat.size - 4) do |row|
i = row
j = 0
while i < mat.size && j < mat.first.size
input_diag << mat[i][j]
i += 1
j += 1
end
input_diag << '|'
end
input_diag.join
end

input_transposed = mat.transpose.map(&:join).join("\n")

horiz_count = count_for(input)
vert_count = count_for(input_transposed)
diag_count = count_for(make_input_diag(mat)) + count_for(make_input_diag(mat.reverse))

puts horiz_count + vert_count + diag_count

# Part 2
count = 0
1.upto(mat.size - 2).each do |i|
1.upto(mat.first.size - 2).each do |j|
next unless mat[i][j] == 'A'

primary = %w[MS SM].include?([mat[i-1][j-1], mat[i+1][j+1]].join)
secondary = %w[MS SM].include?([mat[i+1][j-1], mat[i-1][j+1]].join)
count += 1 if primary && secondary
end
end
puts count
10 changes: 10 additions & 0 deletions 2024/examples/day04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX

0 comments on commit 5546c1e

Please sign in to comment.