diff --git a/.github/shared/setup/action.yaml b/.github/shared/setup/action.yaml new file mode 100644 index 0000000..bc7ca11 --- /dev/null +++ b/.github/shared/setup/action.yaml @@ -0,0 +1,11 @@ +name: "setup ruby" +description: "setup ruby" + +runs: + using: "composite" + steps: + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + working-directory: ./2024/ruby diff --git a/.github/workflows/2018-ruby.yaml b/.github/workflows/2018-ruby.yaml index f3ab9e0..f2a78b0 100644 --- a/.github/workflows/2018-ruby.yaml +++ b/.github/workflows/2018-ruby.yaml @@ -26,5 +26,6 @@ jobs: with: ruby-version: ${{ steps.determine-ruby-version.outputs.VERSION }} bundler-cache: true + working-directory: ./2018/ruby - name: Run tests run: bundle exec rspec diff --git a/.github/workflows/2021-ruby.yaml b/.github/workflows/2021-ruby.yaml index e3ad965..9231707 100644 --- a/.github/workflows/2021-ruby.yaml +++ b/.github/workflows/2021-ruby.yaml @@ -26,5 +26,6 @@ jobs: with: ruby-version: ${{ steps.determine-ruby-version.outputs.VERSION }} bundler-cache: true + working-directory: ./2021/ruby - name: Run tests run: bundle exec rspec diff --git a/.github/workflows/2024-ruby.yaml b/.github/workflows/2024-ruby.yaml index e2571b5..72ae51d 100644 --- a/.github/workflows/2024-ruby.yaml +++ b/.github/workflows/2024-ruby.yaml @@ -3,7 +3,6 @@ name: 2024-ruby on: pull_request: push: - branches: [master] paths: - 2024/ruby/** @@ -12,14 +11,19 @@ defaults: working-directory: ./2024/ruby jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup Ruby + uses: ./.github/shared/setup + - name: Run rubocop + run: bundle exec rubocop tests: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - bundler-cache: true - working-directory: ./2024/ruby - - name: Run tests - run: bundle exec rspec + - uses: actions/checkout@v3 + - name: Setup Ruby + uses: ./.github/shared/setup + - name: Run tests + run: bundle exec rspec diff --git a/2018/ruby/spec/advent_03_spec.rb b/2018/ruby/spec/advent_03_spec.rb index 073cebe..796e22b 100644 --- a/2018/ruby/spec/advent_03_spec.rb +++ b/2018/ruby/spec/advent_03_spec.rb @@ -29,7 +29,7 @@ expect(rectangle.width).to eq(4) end - it "solves the puzzle", solution: true do + it "solves the puzzle", solution: true, slow: true do reader = ClaimReader.new(puzzle_input) claims = reader.process diff --git a/2018/ruby/spec/advent_05_spec.rb b/2018/ruby/spec/advent_05_spec.rb index 7a3c45c..caac678 100644 --- a/2018/ruby/spec/advent_05_spec.rb +++ b/2018/ruby/spec/advent_05_spec.rb @@ -24,7 +24,7 @@ end end - xit "solves part 1" do + it "solves part 1", slow: true do reader = AlchemicalReduction.new(puzzle_input) reader.process @@ -55,7 +55,7 @@ expect(lowest[1]).to eq(4) end - xit "solves part 2" do + it "solves part 2", slow: true do counts = {} ('a'..'z').each do |letter| diff --git a/2018/ruby/spec/advent_06_spec.rb b/2018/ruby/spec/advent_06_spec.rb index f33941b..107760d 100644 --- a/2018/ruby/spec/advent_06_spec.rb +++ b/2018/ruby/spec/advent_06_spec.rb @@ -30,18 +30,17 @@ expect(c.largest_area).to eq(17) end - xit "solves part 1" do + it "solves part 1", slow: true do coordinates = puzzle_input.collect do |item| Coordinate.new(item[0], item[1]) end c = ChronalCoordinates.new(coordinates) - # c.print_grid expect(c.largest_area).to eq(3401) end - it "solves part 2" do + it "solves part 2", slow: true do coordinates = puzzle_input.collect do |item| Coordinate.new(item[0], item[1]) end diff --git a/2018/ruby/spec/advent_07_spec.rb b/2018/ruby/spec/advent_07_spec.rb index 4e5dfe9..3a0a584 100644 --- a/2018/ruby/spec/advent_07_spec.rb +++ b/2018/ruby/spec/advent_07_spec.rb @@ -58,7 +58,7 @@ def create_graph(input, weighted = false, seed_weight = 0) expect(orderer.compressed_order).to eq("CABDFE") end - xit "figures out the puzzle order" do + it "figures out the puzzle order", slow: true do graph = create_graph(puzzle_input) orderer = Orderer.new(graph) @@ -76,7 +76,7 @@ def create_graph(input, weighted = false, seed_weight = 0) expect(seconds).to eq(15) end - xit "figures out part 2 puzzle order" do + it "figures out part 2 puzzle order", slow: true do graph = create_graph(puzzle_input, true, 60) orderer = Orderer.new(graph) diff --git a/2024/ruby/.rubocop_todo.yml b/2024/ruby/.rubocop_todo.yml index 0d0b31c..6ee478b 100644 --- a/2024/ruby/.rubocop_todo.yml +++ b/2024/ruby/.rubocop_todo.yml @@ -21,30 +21,3 @@ Metrics/BlockLength: # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: Max: 120 - -# Offense count: 2 -# Configuration parameters: EnforcedStyle, AllowedPatterns. -# SupportedStyles: snake_case, camelCase -Naming/MethodName: - Exclude: - - 'lib/advent_04_ceres_search.rb' - - 'test.rb' - -# Offense count: 2 -# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns. -# SupportedStyles: snake_case, normalcase, non_integer -# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64 -Naming/VariableNumber: - Exclude: - - 'lib/advent_04_ceres_search.rb' - -# Offense count: 4 -# Configuration parameters: AllowedConstants. -Style/Documentation: - Exclude: - - 'spec/**/*' - - 'test/**/*' - - 'lib/advent_01_hystorian_hysteria.rb' - - 'lib/advent_02_red_nosed_reports.rb' - - 'lib/advent_03_mull_it_over.rb' - - 'lib/advent_04_ceres_search.rb' diff --git a/2024/ruby/Gemfile b/2024/ruby/Gemfile index 6a8e48d..cbf586b 100644 --- a/2024/ruby/Gemfile +++ b/2024/ruby/Gemfile @@ -1,18 +1,18 @@ # frozen_string_literal: true -source 'https://rubygems.org' +source "https://rubygems.org" -ruby File.open('.ruby-version', 'rb') { |f| f.read.chomp } +ruby File.open(".ruby-version", "rb") { |f| f.read.chomp } -gem 'awesome_print' +gem "awesome_print" -gem 'guard' -gem 'guard-rspec', require: false +gem "guard" +gem "guard-rspec", require: false -gem 'pry', '~> 0.15.0' +gem "pry", "~> 0.15.0" -gem 'rake', '~> 13.2' -gem 'rspec', '~> 3.13.0' -gem 'rubocop', '~> 1.69.0' -gem 'rubocop-rake', '0.6.0', require: false -gem 'rubocop-rspec', '3.2.0', require: false +gem "rake", "~> 13.2" +gem "rspec", "~> 3.13.0" +gem "rubocop", "~> 1.69.0" +gem "rubocop-rake", "0.6.0", require: false +gem "rubocop-rspec", "3.2.0", require: false diff --git a/2024/ruby/Guardfile b/2024/ruby/Guardfile index 5a83809..e35b7a4 100644 --- a/2024/ruby/Guardfile +++ b/2024/ruby/Guardfile @@ -26,8 +26,8 @@ # * zeus: 'zeus rspec' (requires the server to be started separately) # * 'just' rspec: 'rspec' -guard :rspec, cmd: 'bundle exec rspec' do - require 'guard/rspec/dsl' +guard :rspec, cmd: "bundle exec rspec" do + require "guard/rspec/dsl" dsl = Guard::RSpec::Dsl.new(self) # Feel free to open issues for suggestions and improvements @@ -67,6 +67,6 @@ guard :rspec, cmd: 'bundle exec rspec' do # Turnip features and steps watch(%r{^spec/acceptance/(.+)\.feature$}) watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m| - Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' + Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance" end end diff --git a/2024/ruby/Rakefile b/2024/ruby/Rakefile index e917db9..100bdaa 100644 --- a/2024/ruby/Rakefile +++ b/2024/ruby/Rakefile @@ -42,10 +42,6 @@ task :new do erb = ERB.new(File.read(h[:template])) rendered = erb.result_with_hash(require_path: basename, day_number: formatted_day) - puts "creating #{path}" - puts rendered.inspect - puts "----------------" - File.write(path, rendered) end diff --git a/2024/ruby/lib/advent_01_hystorian_hysteria.rb b/2024/ruby/lib/advent_01_hystorian_hysteria.rb index 61e061d..a57278f 100644 --- a/2024/ruby/lib/advent_01_hystorian_hysteria.rb +++ b/2024/ruby/lib/advent_01_hystorian_hysteria.rb @@ -1,8 +1,8 @@ -require 'pry' +require "pry" class DistanceCalculator def initialize(list_one, list_two) - raise 'lists must be the same length' if list_one.length != list_two.length + raise "lists must be the same length" if list_one.length != list_two.length @list_one = list_one @list_two = list_two diff --git a/2024/ruby/lib/advent_02_red_nosed_reports.rb b/2024/ruby/lib/advent_02_red_nosed_reports.rb index b64d901..b321504 100644 --- a/2024/ruby/lib/advent_02_red_nosed_reports.rb +++ b/2024/ruby/lib/advent_02_red_nosed_reports.rb @@ -6,7 +6,7 @@ def initialize(levels) end def initial_direction(levels) - levels.length > 1 && levels[0] < levels[1] ? 'increasing' : 'decreasing' + levels.length > 1 && levels[0] < levels[1] ? "increasing" : "decreasing" end def safe_with_dampener? @@ -44,7 +44,7 @@ def safe? end def valid_direction?(direction, level, prev) - (prev < level && direction == 'increasing') || (prev > level && direction == 'decreasing') + (prev < level && direction == "increasing") || (prev > level && direction == "decreasing") end def adjacent_safe?(prev, level) diff --git a/2024/ruby/lib/advent_03_mull_it_over.rb b/2024/ruby/lib/advent_03_mull_it_over.rb index cc0d827..e749462 100644 --- a/2024/ruby/lib/advent_03_mull_it_over.rb +++ b/2024/ruby/lib/advent_03_mull_it_over.rb @@ -25,7 +25,7 @@ def uncorrupted_pieces end def process_piece(str) - splits = str.tr('^0123456789,', '').split(',') + splits = str.tr("^0123456789,", "").split(",") splits[0].to_i * splits[1].to_i end diff --git a/2024/ruby/lib/advent_04_ceres_search.rb b/2024/ruby/lib/advent_04_ceres_search.rb index f5e3aa8..92fa60a 100644 --- a/2024/ruby/lib/advent_04_ceres_search.rb +++ b/2024/ruby/lib/advent_04_ceres_search.rb @@ -1,7 +1,7 @@ class WordSearch attr_reader :data, :word - def initialize(data, word = 'XMAS') + def initialize(data, word = "XMAS") @data = data @word = word end @@ -19,16 +19,16 @@ def count_at(row, col) end def letter_at(row, col) - in_bounds?(row, col) ? data[row][col] : '' + in_bounds?(row, col) ? data[row][col] : "" end def horizontal_word(row, col) first = col last = col + word.length - 1 - return '' unless in_bounds?(first, last) + return "" unless in_bounds?(first, last) - data[row][first..last] * '' + data[row][first..last] * "" end def horizontal?(row, col) @@ -38,7 +38,7 @@ def horizontal?(row, col) def vertical_word(row, col) (row..row + word.length - 1).map do |pos| letter_at(pos, col) - end * '' + end * "" end def vertical?(row, col) @@ -48,25 +48,25 @@ def vertical?(row, col) def diagonal_top_left_word(row, col) (0..word.length - 1).map do |pos| letter_at(row - pos, col - pos) - end * '' + end * "" end def diagonal_top_right_word(row, col) (0..word.length - 1).map do |pos| letter_at(row - pos, col + pos) - end * '' + end * "" end def diagonal_bottom_right_word(row, col) (0..word.length - 1).map do |pos| letter_at(row + pos, col + pos) - end * '' + end * "" end def diagonal_bottom_left_word(row, col) (0..word.length - 1).map do |pos| letter_at(row + pos, col - pos) - end * '' + end * "" end def match?(str) @@ -74,15 +74,15 @@ def match?(str) end def cross_word_at?(row, col) - cross_word = 'MAS' + cross_word = "MAS" - diag_1 = letter_at(row - 1, col - 1) + letter_at(row, col) + letter_at(row + 1, col + 1) - diag_2 = letter_at(row + 1, col - 1) + letter_at(row, col) + letter_at(row - 1, col + 1) + diag1 = letter_at(row - 1, col - 1) + letter_at(row, col) + letter_at(row + 1, col + 1) + diag2 = letter_at(row + 1, col - 1) + letter_at(row, col) + letter_at(row - 1, col + 1) - diag_1_match = [cross_word, cross_word.reverse].any? { |str| str == diag_1 } - diag_2_match = [cross_word, cross_word.reverse].any? { |str| str == diag_2 } + diag1_match = [cross_word, cross_word.reverse].any? { |str| str == diag1 } + diag2_match = [cross_word, cross_word.reverse].any? { |str| str == diag2 } - diag_1_match && diag_2_match + diag1_match && diag2_match end def cross_word_count @@ -147,9 +147,3 @@ def render end end end - -def badName - return unless something - - test -end diff --git a/2024/ruby/spec/advent_01_spec.rb b/2024/ruby/spec/advent_01_hystorian_hysteria_spec.rb similarity index 59% rename from 2024/ruby/spec/advent_01_spec.rb rename to 2024/ruby/spec/advent_01_hystorian_hysteria_spec.rb index e9c24ab..f6edb88 100644 --- a/2024/ruby/spec/advent_01_spec.rb +++ b/2024/ruby/spec/advent_01_hystorian_hysteria_spec.rb @@ -1,22 +1,22 @@ -require 'spec_helper' -require 'advent_01_hystorian_hysteria' +require "spec_helper" +require "advent_01_hystorian_hysteria" -describe 'DistanceCalculator' do +describe "DistanceCalculator" do let(:data) do - File.readlines('./spec/fixtures/advent-01.txt').each do |line| - line.chomp.gsub(/\s+/, ' ') + File.readlines("./spec/fixtures/advent-01.txt").each do |line| + line.chomp.gsub(/\s+/, " ") end end let(:list_one) do - data.map { |line| line.split(' ')[0].to_i } + data.map { |line| line.split(" ")[0].to_i } end let(:list_two) do - data.map { |line| line.split(' ')[1].to_i } + data.map { |line| line.split(" ")[1].to_i } end - it 'calculates distance' do + it "calculates distance" do calc = DistanceCalculator.new([], []) result = calc.distance(1, 3) @@ -24,13 +24,13 @@ expect(result).to be(2) end - it 'calculates total distance' do + it "calculates total distance" do calc = DistanceCalculator.new(list_one, list_two) expect(calc.total_distance).to eq(2_166_959) end - it 'calculates similarity distance with matching number' do + it "calculates similarity distance with matching number" do a = [3, 4, 2, 1, 3, 3] b = [4, 3, 5, 3, 9, 3] calc = DistanceCalculator.new(a, b) @@ -38,7 +38,7 @@ expect(calc.similarity(4)).to eq(4) end - it 'calculates similarity distance with multiple matches' do + it "calculates similarity distance with multiple matches" do a = [3, 4, 2, 1, 3, 3] b = [4, 3, 5, 3, 9, 3] calc = DistanceCalculator.new(a, b) @@ -46,7 +46,7 @@ expect(calc.similarity(3)).to eq(9) end - it 'calculates similarity distance with missing number' do + it "calculates similarity distance with missing number" do a = [3, 4, 2, 1, 3, 3] b = [4, 3, 5, 3, 9, 3] calc = DistanceCalculator.new(a, b) @@ -54,7 +54,7 @@ expect(calc.similarity(2)).to eq(0) end - it 'calculates total similarity distance' do + it "calculates total similarity distance" do calc = DistanceCalculator.new(list_one, list_two) expect(calc.total_similarity_score).to eq(23_741_109) diff --git a/2024/ruby/spec/advent_02_spec.rb b/2024/ruby/spec/advent_02_red_nosed_reports_spec.rb similarity index 56% rename from 2024/ruby/spec/advent_02_spec.rb rename to 2024/ruby/spec/advent_02_red_nosed_reports_spec.rb index 0947a14..9affb9e 100644 --- a/2024/ruby/spec/advent_02_spec.rb +++ b/2024/ruby/spec/advent_02_red_nosed_reports_spec.rb @@ -1,82 +1,81 @@ -require 'spec_helper' -require 'pry' -require 'advent_02_red_nosed_reports' +require "spec_helper" +require "advent_02_red_nosed_reports" -describe 'Report' do - context 'with safe increasing levels' do +describe "Report" do + context "with safe increasing levels" do let(:levels) { [5, 6, 7, 8, 9, 10] } - it 'is safe' do + it "is safe" do report = Report.new(levels) expect(report.safe?).to be(true) end end - context 'with safe decreasing levels' do + context "with safe decreasing levels" do let(:levels) { [5, 4, 3, 2, 1] } - it 'is safe' do + it "is safe" do report = Report.new(levels) expect(report.safe?).to be(true) end end - context 'with mixed levels' do + context "with mixed levels" do let(:levels) { [5, 4, 6] } - it 'is not safe' do + it "is not safe" do report = Report.new(levels) expect(report.safe?).to be(false) end end - context 'with bad adjacent levels' do + context "with bad adjacent levels" do let(:levels) { [1, 2, 4, 8, 9] } - it 'is not safe' do + it "is not safe" do report = Report.new(levels) expect(report.safe?).to be(false) end end - context 'with sample data' do + context "with sample data" do let(:data) do - File.readlines('spec/fixtures/advent-02-sample.txt').map do |e| - e.chomp.split(' ').map(&:to_i) + File.readlines("spec/fixtures/advent-02-sample.txt").map do |e| + e.chomp.split(" ").map(&:to_i) end end - it 'calculates how many are safe' do + it "calculates how many are safe" do safe = data.select { |levels| Report.new(levels).safe? } expect(safe.length).to be(2) end - it 'calculates how many are safe with a dampener of 1' do + it "calculates how many are safe with a dampener of 1" do safe = data.select { |levels| Report.new(levels).safe_with_dampener? } expect(safe.length).to be(4) end end - context 'with puzzle data' do + context "with puzzle data" do let(:data) do - File.readlines('spec/fixtures/advent-02.txt').map do |e| - e.chomp.split(' ').map(&:to_i) + File.readlines("spec/fixtures/advent-02.txt").map do |e| + e.chomp.split(" ").map(&:to_i) end end - it 'calculates how many are safe' do + it "calculates how many are safe" do safe = data.select { |levels| Report.new(levels).safe? } expect(safe.length).to be(660) end - it 'calculates how many are safe with a dampener of 1' do + it "calculates how many are safe with a dampener of 1" do safe = data.select { |levels| Report.new(levels).safe_with_dampener? } expect(safe.length).to be(689) diff --git a/2024/ruby/spec/advent_03_mull_it_over_spec.rb b/2024/ruby/spec/advent_03_mull_it_over_spec.rb new file mode 100644 index 0000000..6aee231 --- /dev/null +++ b/2024/ruby/spec/advent_03_mull_it_over_spec.rb @@ -0,0 +1,52 @@ +require "spec_helper" +require "advent_03_mull_it_over" + +describe "Muller" do + context "with uncorrupted pieces" do + let(:data) { File.read("./spec/fixtures/advent-03-sample-ii.txt").chomp } + + it "parses uncorrupted pieces" do + muller = Muller.new(data) + + expect(muller.uncorrupted_pieces).to eq(["mul(2,4)", "mul(8,5)"]) + end + + it "mulls uncorrupted data" do + muller = Muller.new(data) + + expect(muller.uncorrupted_mull).to eq(48) + end + end + + context "with sample data" do + let(:data) { File.read("./spec/fixtures/advent-03-sample.txt").chomp } + + it "parses the pieces" do + muller = Muller.new(data) + + expect(muller.pieces).to eq(["mul(2,4)", "mul(5,5)", "mul(11,8)", "mul(8,5)"]) + end + + it "mulls the data" do + muller = Muller.new(data) + + expect(muller.mull).to eq(161) + end + end + + context "with puzzle data" do + let(:data) { File.read("./spec/fixtures/advent-03.txt").chomp } + + it "mulls the data" do + muller = Muller.new(data) + + expect(muller.mull).to eq(165_225_049) + end + + it "mulls uncorrupted data" do + muller = Muller.new(data) + + expect(muller.uncorrupted_mull).to eq(108_830_766) + end + end +end diff --git a/2024/ruby/spec/advent_03_spec.rb b/2024/ruby/spec/advent_03_spec.rb deleted file mode 100644 index 710e902..0000000 --- a/2024/ruby/spec/advent_03_spec.rb +++ /dev/null @@ -1,53 +0,0 @@ -require 'spec_helper' -require 'pry' -require 'advent_03_mull_it_over' - -describe 'Muller' do - context 'with uncorrupted pieces' do - let(:data) { File.read('./spec/fixtures/advent-03-sample-ii.txt').chomp } - - it 'parses uncorrupted pieces' do - muller = Muller.new(data) - - expect(muller.uncorrupted_pieces).to eq(['mul(2,4)', 'mul(8,5)']) - end - - it 'mulls uncorrupted data' do - muller = Muller.new(data) - - expect(muller.uncorrupted_mull).to eq(48) - end - end - - context 'with sample data' do - let(:data) { File.read('./spec/fixtures/advent-03-sample.txt').chomp } - - it 'parses the pieces' do - muller = Muller.new(data) - - expect(muller.pieces).to eq(['mul(2,4)', 'mul(5,5)', 'mul(11,8)', 'mul(8,5)']) - end - - it 'mulls the data' do - muller = Muller.new(data) - - expect(muller.mull).to eq(161) - end - end - - context 'with puzzle data' do - let(:data) { File.read('./spec/fixtures/advent-03.txt').chomp } - - it 'mulls the data' do - muller = Muller.new(data) - - expect(muller.mull).to eq(165_225_049) - end - - it 'mulls uncorrupted data' do - muller = Muller.new(data) - - expect(muller.uncorrupted_mull).to eq(108_830_766) - end - end -end diff --git a/2024/ruby/spec/advent_04_ceres_search_spec.rb b/2024/ruby/spec/advent_04_ceres_search_spec.rb new file mode 100644 index 0000000..6c193bc --- /dev/null +++ b/2024/ruby/spec/advent_04_ceres_search_spec.rb @@ -0,0 +1,212 @@ +require "spec_helper" +require "advent_04_ceres_search" + +describe "WordSearch" do + it "counts horizontal" do + data = [ + ["X", "M", "A", "S"], + [".", ".", ".", "."], + [".", ".", ".", "."], + [".", ".", ".", "."] + ] + + search = WordSearch.new(data) + expect(search.horizonal_count).to eq(1) + end + + it "counts horizontal in reverse" do + data = [ + ["S", "A", "M", "X"], + [".", ".", ".", "."], + [".", ".", ".", "."], + [".", ".", ".", "."] + ] + + search = WordSearch.new(data) + + expect(search.horizonal_count).to eq(1) + end + + it "counts vertical" do + data = [ + ["X", ".", ".", "."], + ["M", ".", ".", "."], + ["A", ".", ".", "."], + ["S", ".", ".", "."] + ] + + search = WordSearch.new(data) + + expect(search.vertical_count).to eq(1) + end + + it "counts vertical in reverse" do + data = [ + ["S", ".", ".", "."], + ["A", ".", ".", "."], + ["M", ".", ".", "."], + ["X", ".", ".", "."] + ] + + search = WordSearch.new(data) + + expect(search.vertical_count).to eq(1) + end + + it "counts downward diagonal" do + data = [ + ["X", ".", ".", "."], + [".", "M", ".", "."], + [".", ".", "A", "."], + [".", ".", ".", "S"] + ] + + search = WordSearch.new(data) + + expect(search.diagonal_count).to eq(1) + end + + it "counts downward reverse diagonal" do + data = [ + ["S", ".", ".", "."], + [".", "A", ".", "."], + [".", ".", "M", "."], + [".", ".", ".", "X"] + ] + + search = WordSearch.new(data) + + expect(search.diagonal_count).to eq(1) + end + + it "counts upward diagonal" do + data = [ + [".", ".", ".", "S"], + [".", ".", "A", "."], + [".", "M", ".", "."], + ["X", ".", ".", "."] + ] + + search = WordSearch.new(data) + + expect(search.diagonal_count).to eq(1) + end + + it "counts upward reverse diagonal" do + data = [ + [".", ".", ".", "X"], + [".", ".", "M", "."], + [".", "A", ".", "."], + ["S", ".", ".", "."] + ] + + search = WordSearch.new(data) + + expect(search.diagonal_count).to eq(1) + end + + it "counts crossing diagonals" do + data = [ + ["X", ".", ".", "S"], + [".", "M", "A", "."], + [".", "M", "A", "."], + ["X", ".", ".", "S"] + ] + + search = WordSearch.new(data) + + expect(search.diagonal_count).to eq(2) + end + + it "counts cross diagonals" do + data = [ + ["S", ".", ".", "S"], + [".", "A", "A", "."], + [".", "M", "M", "."], + ["X", ".", ".", "X"] + ] + + search = WordSearch.new(data) + + expect(search.diagonal_count).to eq(2) + end + + it "finds no matches" do + data = [ + %w[X X X X], + %w[X X X M], + %w[X X X X], + %w[A X X X], + %w[X S X X] + ] + + search = WordSearch.new(data) + + expect(search.diagonal_count).to eq(0) + end + + it "counts diagonals" do + data = [ + [".", "S", ".", ".", ".", ".", ".", "S", ".", "."], + [".", ".", "A", ".", ".", ".", "A", ".", ".", "."], + [".", ".", ".", "M", ".", "M", ".", ".", ".", "."], + [".", ".", ".", ".", "X", ".", ".", ".", ".", "."], + [".", ".", ".", "M", ".", "M", ".", ".", ".", "."], + [".", ".", "A", ".", ".", ".", "A", ".", ".", "."], + [".", "S", ".", ".", ".", ".", ".", "S", ".", "."] + ] + + search = WordSearch.new(data) + + expect(search.diagonal_top_left_word(3, 4)).to eq("XMAS") + expect(search.diagonal_top_right_word(3, 4)).to eq("XMAS") + expect(search.diagonal_bottom_right_word(3, 4)).to eq("XMAS") + expect(search.diagonal_bottom_left_word(3, 4)).to eq("XMAS") + + expect(search.diagonal_count).to eq(4) + expect(search.word_count).to eq(4) + end + + it "counts cross words" do + data = [ + ["M", ".", "S"], + [".", "A", "."], + ["M", ".", "S"] + ] + + search = WordSearch.new(data) + + expect(search.cross_word_at?(1, 1)).to be(true) + expect(search.cross_word_count).to eq(1) + end + + context "with sample data" do + let(:data) do + File.readlines("./spec/fixtures/advent-04-sample.txt").map { |l| l.chomp.chars } + end + + it "finds counts of XMAS" do + search = WordSearch.new(data) + + expect(search.word_count).to eq(18) + end + end + + context "with puzzle data" do + let(:data) do + File.readlines("./spec/fixtures/advent-04.txt").map { |l| l.chomp.chars } + end + + it "finds counts of XMAS" do + search = WordSearch.new(data) + + expect(search.word_count).to eq(2447) + end + + it "finds cross word counts" do + search = WordSearch.new(data) + + expect(search.cross_word_count).to eq(1868) + end + end +end diff --git a/2024/ruby/spec/advent_04_spec.rb b/2024/ruby/spec/advent_04_spec.rb deleted file mode 100644 index fc222b2..0000000 --- a/2024/ruby/spec/advent_04_spec.rb +++ /dev/null @@ -1,212 +0,0 @@ -require 'spec_helper' -require 'advent_04_ceres_search' - -describe 'WordSearch' do - it 'counts horizontal' do - data = [ - ['X', 'M', 'A', 'S'], - ['.', '.', '.', '.'], - ['.', '.', '.', '.'], - ['.', '.', '.', '.'] - ] - - search = WordSearch.new(data) - expect(search.horizonal_count).to eq(1) - end - - it 'counts horizontal in reverse' do - data = [ - ['S', 'A', 'M', 'X'], - ['.', '.', '.', '.'], - ['.', '.', '.', '.'], - ['.', '.', '.', '.'] - ] - - search = WordSearch.new(data) - - expect(search.horizonal_count).to eq(1) - end - - it 'counts vertical' do - data = [ - ['X', '.', '.', '.'], - ['M', '.', '.', '.'], - ['A', '.', '.', '.'], - ['S', '.', '.', '.'] - ] - - search = WordSearch.new(data) - - expect(search.vertical_count).to eq(1) - end - - it 'counts vertical in reverse' do - data = [ - ['S', '.', '.', '.'], - ['A', '.', '.', '.'], - ['M', '.', '.', '.'], - ['X', '.', '.', '.'] - ] - - search = WordSearch.new(data) - - expect(search.vertical_count).to eq(1) - end - - it 'counts downward diagonal' do - data = [ - ['X', '.', '.', '.'], - ['.', 'M', '.', '.'], - ['.', '.', 'A', '.'], - ['.', '.', '.', 'S'] - ] - - search = WordSearch.new(data) - - expect(search.diagonal_count).to eq(1) - end - - it 'counts downward reverse diagonal' do - data = [ - ['S', '.', '.', '.'], - ['.', 'A', '.', '.'], - ['.', '.', 'M', '.'], - ['.', '.', '.', 'X'] - ] - - search = WordSearch.new(data) - - expect(search.diagonal_count).to eq(1) - end - - it 'counts upward diagonal' do - data = [ - ['.', '.', '.', 'S'], - ['.', '.', 'A', '.'], - ['.', 'M', '.', '.'], - ['X', '.', '.', '.'] - ] - - search = WordSearch.new(data) - - expect(search.diagonal_count).to eq(1) - end - - it 'counts upward reverse diagonal' do - data = [ - ['.', '.', '.', 'X'], - ['.', '.', 'M', '.'], - ['.', 'A', '.', '.'], - ['S', '.', '.', '.'] - ] - - search = WordSearch.new(data) - - expect(search.diagonal_count).to eq(1) - end - - it 'counts crossing diagonals' do - data = [ - ['X', '.', '.', 'S'], - ['.', 'M', 'A', '.'], - ['.', 'M', 'A', '.'], - ['X', '.', '.', 'S'] - ] - - search = WordSearch.new(data) - - expect(search.diagonal_count).to eq(2) - end - - it 'counts cross diagonals' do - data = [ - ['S', '.', '.', 'S'], - ['.', 'A', 'A', '.'], - ['.', 'M', 'M', '.'], - ['X', '.', '.', 'X'] - ] - - search = WordSearch.new(data) - - expect(search.diagonal_count).to eq(2) - end - - it 'finds no matches' do - data = [ - %w[X X X X], - %w[X X X M], - %w[X X X X], - %w[A X X X], - %w[X S X X] - ] - - search = WordSearch.new(data) - - expect(search.diagonal_count).to eq(0) - end - - it 'counts diagonals' do - data = [ - ['.', 'S', '.', '.', '.', '.', '.', 'S', '.', '.'], - ['.', '.', 'A', '.', '.', '.', 'A', '.', '.', '.'], - ['.', '.', '.', 'M', '.', 'M', '.', '.', '.', '.'], - ['.', '.', '.', '.', 'X', '.', '.', '.', '.', '.'], - ['.', '.', '.', 'M', '.', 'M', '.', '.', '.', '.'], - ['.', '.', 'A', '.', '.', '.', 'A', '.', '.', '.'], - ['.', 'S', '.', '.', '.', '.', '.', 'S', '.', '.'] - ] - - search = WordSearch.new(data) - - expect(search.diagonal_top_left_word(3, 4)).to eq('XMAS') - expect(search.diagonal_top_right_word(3, 4)).to eq('XMAS') - expect(search.diagonal_bottom_right_word(3, 4)).to eq('XMAS') - expect(search.diagonal_bottom_left_word(3, 4)).to eq('XMAS') - - expect(search.diagonal_count).to eq(4) - expect(search.word_count).to eq(4) - end - - it 'counts cross words' do - data = [ - ['M', '.', 'S'], - ['.', 'A', '.'], - ['M', '.', 'S'] - ] - - search = WordSearch.new(data) - - expect(search.cross_word_at?(1, 1)).to be(true) - expect(search.cross_word_count).to eq(1) - end - - context 'with sample data' do - let(:data) do - File.readlines('./spec/fixtures/advent-04-sample.txt').map { |l| l.chomp.chars } - end - - it 'finds counts of XMAS' do - search = WordSearch.new(data) - - expect(search.word_count).to eq(18) - end - end - - context 'with puzzle data' do - let(:data) do - File.readlines('./spec/fixtures/advent-04.txt').map { |l| l.chomp.chars } - end - - it 'finds counts of XMAS' do - search = WordSearch.new(data) - - expect(search.word_count).to eq(2447) - end - - it 'finds cross word counts' do - search = WordSearch.new(data) - - expect(search.cross_word_count).to eq(1868) - end - end -end diff --git a/2024/ruby/spec/spec_helper.rb b/2024/ruby/spec/spec_helper.rb index d7da771..19d7f15 100644 --- a/2024/ruby/spec/spec_helper.rb +++ b/2024/ruby/spec/spec_helper.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'pry' +require "pry" # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.