diff --git a/ruby/2015/1/bin/problem b/ruby/2015/1/bin/problem new file mode 100644 index 0000000..7c5cf31 --- /dev/null +++ b/ruby/2015/1/bin/problem @@ -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}" \ No newline at end of file diff --git a/ruby/2015/1/lib/directions.rb b/ruby/2015/1/lib/directions.rb new file mode 100644 index 0000000..74ac5d3 --- /dev/null +++ b/ruby/2015/1/lib/directions.rb @@ -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 diff --git a/ruby/2015/1/problem.rb b/ruby/2015/1/problem.rb index c705c0f..bdb68d0 100644 --- a/ruby/2015/1/problem.rb +++ b/ruby/2015/1/problem.rb @@ -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}" diff --git a/ruby/2015/1/test/test_directions.rb b/ruby/2015/1/test/test_directions.rb new file mode 100644 index 0000000..39f88a8 --- /dev/null +++ b/ruby/2015/1/test/test_directions.rb @@ -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 diff --git a/ruby/bin/run b/ruby/bin/run index c381539..f9c5d56 100755 --- a/ruby/bin/run +++ b/ruby/bin/run @@ -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)