diff options
Diffstat (limited to '2024/04a/run.rb')
-rwxr-xr-x | 2024/04a/run.rb | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/2024/04a/run.rb b/2024/04a/run.rb new file mode 100755 index 0000000..22118c2 --- /dev/null +++ b/2024/04a/run.rb @@ -0,0 +1,84 @@ +#!/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 +PREFIX = "X" +SUFIX = %w(M A S) + +found = 0 + +def find_with_vector(start_x, start_y, x, y) + word = (1..3).map do |iter| + + @letters[[start_x + (iter * x), start_y + (iter * y)]] + end + + true if word.to_a == SUFIX +end + +potential_starts = @letters.select{|key, val| val == PREFIX } +potential_starts.each do |key, value| + + found+= 1 if find_with_vector(key[0], key[1], 0, 1) + found+= 1 if find_with_vector(key[0], key[1], 1, 0) + + found+= 1 if find_with_vector(key[0], key[1], 0, -1) + found+= 1 if find_with_vector(key[0], key[1], -1, 0) + + found+= 1 if find_with_vector(key[0], key[1], 1, 1) + found+= 1 if find_with_vector(key[0], key[1], -1, 1) + found+= 1 if find_with_vector(key[0], key[1], 1, -1) + found+= 1 if find_with_vector(key[0], key[1], -1, -1) +end + +p found + + + +exit + +def find_with_vector(start_y, start_x, y, x) + word = (1..3).map do |offset| + tested_row = @data[ start_x + offset* x ] + return false unless tested_row + + letter = tested_row[ start_y + offset * y ] + return unless SUFIX.include? letter + + letter + end + + p [word, start_x, start_y, x, y] if word == SUFIX + true if word == SUFIX + + +end + +found_count = 0 +@data.each_with_index do |row, y| + row.each_with_index do |letter, x| + next unless letter == "X" + + found_count += 1 if find_with_vector(x,y, 0, 1) + found_count += 1 if find_with_vector(x,y, 1, 0) + + found_count += 1 if find_with_vector(x,y, 0, -1) + found_count += 1 if find_with_vector(x,y, -1, 0) + + found_count += 1 if find_with_vector(x,y, 1, 1) + found_count += 1 if find_with_vector(x,y, -1, 1) + found_count += 1 if find_with_vector(x,y, 1, -1) + found_count += 1 if find_with_vector(x,y, -1, -1) + + + end +end + +p found_count |