From 7c063782df4ec5d5a352eae010b6f5ddfcd4e544 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Mon, 1 Dec 2025 04:03:24 -0500 Subject: [PATCH] 2025! Ruby, day 1 --- ruby/2025/1/bin/problem | 6 +++ ruby/2025/1/lib/combination.rb | 65 ++++++++++++++++++++++++++++ ruby/2025/1/problem.rb | 0 ruby/2025/1/test/test_combination.rb | 59 +++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 ruby/2025/1/bin/problem create mode 100644 ruby/2025/1/lib/combination.rb delete mode 100644 ruby/2025/1/problem.rb create mode 100644 ruby/2025/1/test/test_combination.rb diff --git a/ruby/2025/1/bin/problem b/ruby/2025/1/bin/problem new file mode 100644 index 0000000..fea452c --- /dev/null +++ b/ruby/2025/1/bin/problem @@ -0,0 +1,6 @@ +#/usr/bin/env ruby +require "combination" + +combination = Combination.for STDIN.read.chomp +puts "Part 1: #{combination.zero_count}" +puts "Part 2: #{combination.zero_click_count}" \ No newline at end of file diff --git a/ruby/2025/1/lib/combination.rb b/ruby/2025/1/lib/combination.rb new file mode 100644 index 0000000..41223d8 --- /dev/null +++ b/ruby/2025/1/lib/combination.rb @@ -0,0 +1,65 @@ +class Combination + attr_reader :steps + def initialize(steps) + @steps = steps + end + + def self.for(string) + new string.split("\n").map{ |line| Step.for line } + end + + def zero_count + safe = Safe.new + zc = 0 + steps.each do |step| + safe.apply_step step + zc += 1 if safe.count.zero? + end + zc + end + + def zero_click_count + safe = Safe.new + steps.each do |step| + safe.apply_step step + end + safe.zero_clicks + end + + class Step + attr_reader :dir, :count + def initialize(dir, count) + @dir = dir + @count = dir == "R" ? count : -count + end + + def self.for(string) + new string[0], string[1..].to_i + end + end + + class Safe + attr_reader :count + attr_reader :zero_clicks + def initialize + @count = 50 + @zero_clicks = 0 + end + + def apply_step(step) + if step.dir == "R" + 0.upto(step.count - 1) do + @count += 1 + @count = @count % 100 + @zero_clicks += 1 if @count.zero? + end + else + 0.upto(-step.count - 1) do + @count -= 1 + @count = @count % 100 + @zero_clicks += 1 if @count.zero? + end + end + end + end +end diff --git a/ruby/2025/1/problem.rb b/ruby/2025/1/problem.rb deleted file mode 100644 index e69de29..0000000 diff --git a/ruby/2025/1/test/test_combination.rb b/ruby/2025/1/test/test_combination.rb new file mode 100644 index 0000000..f8922f1 --- /dev/null +++ b/ruby/2025/1/test/test_combination.rb @@ -0,0 +1,59 @@ +require "minitest/autorun" +require "combination" + +TEST_COMBINATION = <