diff --git a/.github/shared/setup/action.yaml b/.github/shared/setup/action.yaml index bc7ca11..fd348a2 100644 --- a/.github/shared/setup/action.yaml +++ b/.github/shared/setup/action.yaml @@ -1,11 +1,14 @@ -name: "setup ruby" -description: "setup ruby" - +name: setup ruby +description: setup ruby +inputs: + working-directory: + description: directory to use + required: true runs: - using: "composite" + using: composite steps: - name: Set up Ruby uses: ruby/setup-ruby@v1 with: bundler-cache: true - working-directory: ./2024/ruby + working-directory: ${{ inputs.working-directory }} diff --git a/.github/workflows/2021-ruby.yaml b/.github/workflows/2021-ruby.yaml index 9231707..526f05b 100644 --- a/.github/workflows/2021-ruby.yaml +++ b/.github/workflows/2021-ruby.yaml @@ -12,20 +12,25 @@ defaults: working-directory: ./2021/ruby jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: debug + run: pwd + - name: Setup Ruby + uses: ./.github/shared/setup + with: + working-directory: ./2021/ruby + - name: Run rubocop + run: bundle exec rubocop tests: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Determine ruby version - id: determine-ruby-version - run: | - VERSION=$(cat .ruby-version) - echo "VERSION=${VERSION}" >> "$GITHUB_OUTPUT" - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: ${{ steps.determine-ruby-version.outputs.VERSION }} - bundler-cache: true - working-directory: ./2021/ruby - - name: Run tests - run: bundle exec rspec + - uses: actions/checkout@v3 + - name: Setup Ruby + uses: ./.github/shared/setup + with: + 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 72ae51d..bd1a36c 100644 --- a/.github/workflows/2024-ruby.yaml +++ b/.github/workflows/2024-ruby.yaml @@ -17,6 +17,8 @@ jobs: - uses: actions/checkout@v3 - name: Setup Ruby uses: ./.github/shared/setup + with: + working-directory: ./2024/ruby - name: Run rubocop run: bundle exec rubocop tests: @@ -25,5 +27,7 @@ jobs: - uses: actions/checkout@v3 - name: Setup Ruby uses: ./.github/shared/setup + with: + working-directory: ./2024/ruby - name: Run tests run: bundle exec rspec diff --git a/2021/ruby/.rubocop.yml b/2021/ruby/.rubocop.yml index a44bea2..6accd83 100644 --- a/2021/ruby/.rubocop.yml +++ b/2021/ruby/.rubocop.yml @@ -1,3 +1,7 @@ +require: + - rubocop-rspec + - rubocop-rake + AllCops: Exclude: - bin/**/* @@ -8,7 +12,8 @@ Style/Documentation: Style/FrozenStringLiteralComment: Enabled: false Style/StringLiterals: - Enabled: false + Enabled: true + EnforcedStyle: double_quotes Style/TrailingCommaInHashLiteral: Enabled: false Style/NumericPredicate: @@ -16,5 +21,9 @@ Style/NumericPredicate: Metrics/BlockLength: Exclude: - spec/**/* +RSpec/ExampleLength: + Max: 20 +RSpec/MultipleExpectations: + Enabled: false inherit_from: .rubocop_todo.yml diff --git a/2021/ruby/.rubocop_todo.yml b/2021/ruby/.rubocop_todo.yml new file mode 100644 index 0000000..dfb999f --- /dev/null +++ b/2021/ruby/.rubocop_todo.yml @@ -0,0 +1,19 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2024-12-16 16:59:11 UTC using RuboCop version 1.69.2. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +RSpec/MultipleDescribes: + Exclude: + - 'spec/advent_02_spec.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings. +# URISchemes: http, https +Layout/LineLength: + Max: 122 diff --git a/2021/ruby/.ruby-version b/2021/ruby/.ruby-version index d9c62ed..9c25013 100644 --- a/2021/ruby/.ruby-version +++ b/2021/ruby/.ruby-version @@ -1 +1 @@ -3.0.2 \ No newline at end of file +3.3.6 diff --git a/2021/ruby/Gemfile b/2021/ruby/Gemfile index 4269dc0..1e39f42 100644 --- a/2021/ruby/Gemfile +++ b/2021/ruby/Gemfile @@ -1,10 +1,12 @@ # 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 'pry', '~> 0.12.0' -gem 'rspec', '~> 3.8.0' -gem 'rubocop', '~> 0.60' +gem "awesome_print" +gem "pry", "~> 0.12.0" +gem "rspec", "~> 3.8.0" +gem "rubocop", "~> 1.69.2" +gem "rubocop-rake", "~> 0.6.0" +gem "rubocop-rspec", "~> 3.2.0" diff --git a/2021/ruby/Gemfile.lock b/2021/ruby/Gemfile.lock index 29ef029..470614b 100644 --- a/2021/ruby/Gemfile.lock +++ b/2021/ruby/Gemfile.lock @@ -4,17 +4,20 @@ GEM ast (2.4.2) awesome_print (1.9.2) coderay (1.1.3) - diff-lcs (1.4.4) + diff-lcs (1.5.1) + json (2.9.0) + language_server-protocol (3.17.0.3) method_source (0.9.2) - parallel (1.21.0) - parser (3.0.3.1) + parallel (1.26.3) + parser (3.3.6.0) ast (~> 2.4.1) + racc pry (0.12.2) coderay (~> 1.1.0) method_source (~> 0.9.0) - rainbow (3.0.0) - regexp_parser (2.1.1) - rexml (3.2.5) + racc (1.8.1) + rainbow (3.1.1) + regexp_parser (2.9.3) rspec (3.8.0) rspec-core (~> 3.8.0) rspec-expectations (~> 3.8.0) @@ -28,31 +31,41 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) rspec-support (3.8.3) - rubocop (0.93.1) + rubocop (1.69.2) + json (~> 2.3) + language_server-protocol (>= 3.17.0) parallel (~> 1.10) - parser (>= 2.7.1.5) + parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 1.8) - rexml - rubocop-ast (>= 0.6.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.36.2, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 1.4.0, < 2.0) - rubocop-ast (1.14.0) - parser (>= 3.0.1.1) - ruby-progressbar (1.11.0) - unicode-display_width (1.8.0) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.37.0) + parser (>= 3.3.1.0) + rubocop-rake (0.6.0) + rubocop (~> 1.0) + rubocop-rspec (3.2.0) + rubocop (~> 1.61) + ruby-progressbar (1.13.0) + unicode-display_width (3.1.2) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) PLATFORMS + arm64-darwin-24 x86_64-linux DEPENDENCIES awesome_print pry (~> 0.12.0) rspec (~> 3.8.0) - rubocop (~> 0.60) + rubocop (~> 1.69.2) + rubocop-rake (~> 0.6.0) + rubocop-rspec (~> 3.2.0) RUBY VERSION - ruby 3.0.2p107 + ruby 3.3.6p108 BUNDLED WITH 2.2.22 diff --git a/2021/ruby/lib/advent_02.rb b/2021/ruby/lib/advent_02.rb index dd55aa4..72f2a9e 100644 --- a/2021/ruby/lib/advent_02.rb +++ b/2021/ruby/lib/advent_02.rb @@ -1,4 +1,3 @@ - class DepthCalculator def initialize(data) @data = data @@ -8,18 +7,18 @@ def initialize(data) def process_action(action, amount) case action - when 'forward' + when "forward" @horizontal_position += amount - when 'down' + when "down" @depth += amount - when 'up' + when "up" @depth -= amount end end def process @data.each do |line| - action, amount = line.split(' ') + action, amount = line.split(" ") amount = amount.to_i process_action(action, amount) end @@ -30,7 +29,6 @@ def product end end - class AimCalculator def initialize(data) @data = data @@ -41,19 +39,19 @@ def initialize(data) def process_action(action, amount) case action - when 'forward' + when "forward" @horizontal_position += amount @depth += (@aim * amount) - when 'down' + when "down" @aim += amount - when 'up' + when "up" @aim -= amount end end def process @data.each do |line| - action, amount = line.split(' ') + action, amount = line.split(" ") amount = amount.to_i process_action(action, amount) end diff --git a/2021/ruby/lib/advent_03.rb b/2021/ruby/lib/advent_03.rb index e9c067e..f806a5f 100644 --- a/2021/ruby/lib/advent_03.rb +++ b/2021/ruby/lib/advent_03.rb @@ -31,27 +31,27 @@ def least_common_bit(data, index) def episolon_rate result = (0...@word_length).map { |index| least_common_bit(@data, index) } - result.join('').to_i(2) + result.join("").to_i(2) end def gamma_rate result = (0...@word_length).map { |index| most_common_bit(@data, index) } - result.join('').to_i(2) + result.join("").to_i(2) end def power_consumption episolon_rate * gamma_rate end - def determine_rating(data, use_most_common_bit = true, index = 0) - return 0 if data.length.zero? + def determine_rating(data, use_most_common_bit: true, index: 0) + return 0 if data.empty? return data[0].to_i(2) if data.length == 1 bit = use_most_common_bit ? most_common_bit(data, index) : least_common_bit(data, index) data.select! { |e| e[index] == bit.to_s } index += 1 - determine_rating(data, use_most_common_bit, index) + determine_rating(data, use_most_common_bit: use_most_common_bit, index: index) end def oxygen_generator_rating @@ -61,7 +61,7 @@ def oxygen_generator_rating def co2_scubber_rating to_process = @data.map(&:clone) - determine_rating(to_process, false) + determine_rating(to_process, use_most_common_bit: false) end def life_support_rating diff --git a/2021/ruby/spec/advent_01_spec.rb b/2021/ruby/spec/advent_01_spec.rb index f209307..6339313 100644 --- a/2021/ruby/spec/advent_01_spec.rb +++ b/2021/ruby/spec/advent_01_spec.rb @@ -1,17 +1,17 @@ -require 'spec_helper' -require 'advent_01' +require "spec_helper" +require "advent_01" -describe 'DepthCounter' do +describe "DepthCounter" do let(:puzzle_input) do - File.readlines('./spec/fixtures/advent-01.txt').map(&:to_i) + File.readlines("./spec/fixtures/advent-01.txt").map(&:to_i) end - it 'counts the number of increased measurements' do + it "counts the number of increased measurements" do count = DepthCounter.new(puzzle_input).total_increments expect(count).to be(1754) end - it 'counts using window measurement' do + it "counts using window measurement" do count = DepthCounter.new(puzzle_input).total_window_measurement_increments expect(count).to be(1789) diff --git a/2021/ruby/spec/advent_02_spec.rb b/2021/ruby/spec/advent_02_spec.rb index fd9339e..97c206a 100644 --- a/2021/ruby/spec/advent_02_spec.rb +++ b/2021/ruby/spec/advent_02_spec.rb @@ -1,12 +1,12 @@ -require 'spec_helper' -require 'advent_02' +require "spec_helper" +require "advent_02" -describe 'DepthCalculator' do +describe "DepthCalculator" do let(:puzzle_input) do - File.readlines('./spec/fixtures/advent-02.txt') + File.readlines("./spec/fixtures/advent-02.txt") end - it 'calculates the product' do + it "calculates the product" do dc = DepthCalculator.new(puzzle_input) dc.process @@ -14,19 +14,19 @@ end end -describe 'AimCalculator' do +describe "AimCalculator" do let(:puzzle_input) do - File.readlines('./spec/fixtures/advent-02.txt') + File.readlines("./spec/fixtures/advent-02.txt") end - it 'calculates the product from sample' do + it "calculates the product from sample" do sample_input = [ - 'forward 5', - 'down 5', - 'forward 8', - 'up 3', - 'down 8', - 'forward 2', + "forward 5", + "down 5", + "forward 8", + "up 3", + "down 8", + "forward 2" ] dc = AimCalculator.new(sample_input) @@ -35,7 +35,7 @@ expect(dc.product).to be(900) end - it 'calculates the product' do + it "calculates the product" do dc = AimCalculator.new(puzzle_input) dc.process diff --git a/2021/ruby/spec/advent_03_spec.rb b/2021/ruby/spec/advent_03_spec.rb index 4ceb551..f9a27db 100644 --- a/2021/ruby/spec/advent_03_spec.rb +++ b/2021/ruby/spec/advent_03_spec.rb @@ -1,25 +1,15 @@ -require 'spec_helper' -require 'advent_03' +require "spec_helper" +require "advent_03" -describe 'BinaryDiagnostic' do +describe "BinaryDiagnostic" do let(:puzzle_input) do - File.readlines('./spec/fixtures/advent-03.txt').map(&:chomp) + File.readlines("./spec/fixtures/advent-03.txt").map(&:chomp) end - it 'calculates the sample power consumption' do + it "calculates the sample power consumption" do sample_input = %w[ - 00100 - 11110 - 10110 - 10111 - 10101 - 01111 - 00111 - 11100 - 10000 - 11001 - 00010 - 01010 + 00100 11110 10110 10111 10101 01111 00111 11100 10000 11001 + 00010 01010 ] dc = BinaryDiagnostic.new(sample_input) @@ -34,13 +24,13 @@ expect(dc.life_support_rating).to be(230) end - it 'calculates the power consumption' do + it "calculates the power consumption" do dc = BinaryDiagnostic.new(puzzle_input) expect(dc.power_consumption).to be(388_256_4) end - it 'calculates life support rating' do + it "calculates life support rating" do dc = BinaryDiagnostic.new(puzzle_input) expect(dc.life_support_rating).to be(338_517_0)