diff options
Diffstat (limited to '2024/02b/run.rb')
-rwxr-xr-x | 2024/02b/run.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/2024/02b/run.rb b/2024/02b/run.rb new file mode 100755 index 0000000..627bac4 --- /dev/null +++ b/2024/02b/run.rb @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby + + +def directions_for_values(values) + directions = [] + values.each_with_index do |value, index| + next if index.zero? + + distance = (value.to_i - values[index-1].to_i) + direction = if (distance.abs > 3 ) + :err + elsif distance > 0 + :up + elsif distance < 0 + :down + else + :err + end + + directions << direction + end + + return 0 if directions.count(:err) > 1 + directions.uniq.count == 1 ? 1 : 0 +end + + +data = File.read("data.txt") + +data = data.split("\n") +data = data.map(&:split) + +safe_arr = data.map do |row| + results = [directions_for_values(row)] + + row.each_with_index do |value, index| + tmp_arr = row.dup + tmp_arr.delete_at(index) + results << directions_for_values(tmp_arr) + end + + results +end.map(&:sum) + + +p safe_arr.count { |row| row > 0 } + |