Try minitest with ruby

This commit is contained in:
Bill Rossi 2025-11-30 21:09:48 -05:00
parent 384f4a28e5
commit cddfabe954
5 changed files with 61 additions and 16 deletions

7
ruby/2015/1/bin/problem Normal file
View File

@ -0,0 +1,7 @@
#/usr/bin/env ruby
require "directions"
input = STDIN.read.chomp
directions = Directions.for input
puts "Part 1: #{directions.final_floor}"
puts "Part 2: #{directions.first_basement_step}"

View File

@ -0,0 +1,18 @@
class Directions
attr_reader :steps
def initialize(steps)
@steps = steps
end
def self.for(string)
new string.split("")
end
def final_floor
steps.reduce(0) { |current_floor, step| step == "(" ? current_floor + 1 : current_floor - 1 }
end
def first_basement_step
steps.reduce([0, 0]) { |acc, step| acc[0] >= 0 ? (step == "(" ? [acc[0] + 1, acc[1] + 1] : [acc[0] - 1, acc[1] + 1]) : acc }[1]
end
end

View File

@ -1,15 +1,5 @@
input = STDIN.read.split("")
counts = input.tally
final_floor = counts["("] - counts[")"]
puts "Part 1: #{final_floor}"
require "directions"
floor = 0
steps = 0
input.each do |char|
floor += 1 if char == "("
floor -= 1 if char == ")"
steps += 1
break if floor < 0
end
puts "Part 2: #{steps}"
input = STDIN.read.chomp
directions = Directions.for input
puts "Part 1: #{directions.final_floor}"

View File

@ -0,0 +1,30 @@
require "minitest/autorun"
require "directions"
class TestDirections < ::Minitest::Test
def test_zero_examples
assert_equal(0, Directions.for("(())").final_floor)
assert_equal(0, Directions.for("()()").final_floor)
end
def test_three_examples
assert_equal(3, Directions.for("(((").final_floor)
assert_equal(3, Directions.for("(()(()(").final_floor)
assert_equal(3, Directions.for("))(((((").final_floor)
end
def test_negative_one_examples
assert_equal(-1, Directions.for("())").final_floor)
assert_equal(-1, Directions.for("))(").final_floor)
end
def test_negative_three_examples
assert_equal(-3, Directions.for(")))").final_floor)
assert_equal(-3, Directions.for(")())())").final_floor)
end
def test_first_basement_floor
assert_equal(1, Directions.for(")").first_basement_step)
assert_equal(5, Directions.for("()())").first_basement_step)
end
end

View File

@ -4,11 +4,11 @@ year=$1
day=$2
mkdir -p $year/$day
source_file=$(ls $year/$day/*.rb)
source_file=$(ls $year/$day/bin/problem)
if [[ -z $source_file ]] ; then
echo "No ruby source file found in $year/$day"
exit 1
fi
time (cat ../data/$year/$day/input.txt | ruby $source_file)
time (cat ../data/$year/$day/input.txt | ruby -I$year/$day/lib $source_file)