From 7267986bf52d7fa8a8a426ce5b1122d903cc4da9 Mon Sep 17 00:00:00 2001 From: mms Date: Wed, 4 Dec 2024 11:58:10 +0100 Subject: feat: 2024@4a+b= --- 2024/04a/run.rb | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100755 2024/04a/run.rb (limited to '2024/04a/run.rb') 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 -- cgit v1.2.3