Skip to content

Commit f5376b3

Browse files
rubocop
1 parent 101f0b3 commit f5376b3

File tree

9 files changed

+110
-80
lines changed

9 files changed

+110
-80
lines changed

2021/ruby/.rubocop.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
require:
2+
- rubocop-rspec
3+
- rubocop-rake
4+
15
AllCops:
26
Exclude:
37
- bin/**/*
@@ -8,13 +12,18 @@ Style/Documentation:
812
Style/FrozenStringLiteralComment:
913
Enabled: false
1014
Style/StringLiterals:
11-
Enabled: false
15+
Enabled: true
16+
EnforcedStyle: double_quotes
1217
Style/TrailingCommaInHashLiteral:
1318
Enabled: false
1419
Style/NumericPredicate:
1520
Enabled: false
1621
Metrics/BlockLength:
1722
Exclude:
1823
- spec/**/*
24+
RSpec/ExampleLength:
25+
Max: 20
26+
RSpec/MultipleExpectations:
27+
Enabled: false
1928

2029
inherit_from: .rubocop_todo.yml

2021/ruby/.rubocop_todo.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2024-12-16 16:59:11 UTC using RuboCop version 1.69.2.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 1
10+
RSpec/MultipleDescribes:
11+
Exclude:
12+
- 'spec/advent_02_spec.rb'
13+
14+
# Offense count: 1
15+
# This cop supports safe autocorrection (--autocorrect).
16+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
17+
# URISchemes: http, https
18+
Layout/LineLength:
19+
Max: 122

2021/ruby/Gemfile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# frozen_string_literal: true
22

3-
source 'https://rubygems.org'
3+
source "https://rubygems.org"
44

5-
ruby File.open('.ruby-version', 'rb') { |f| f.read.chomp }
5+
ruby File.open(".ruby-version", "rb") { |f| f.read.chomp }
66

7-
gem 'awesome_print'
8-
gem 'pry', '~> 0.12.0'
9-
gem 'rspec', '~> 3.8.0'
10-
gem 'rubocop', '~> 0.60'
7+
gem "awesome_print"
8+
gem "pry", "~> 0.12.0"
9+
gem "rspec", "~> 3.8.0"
10+
gem "rubocop", "~> 1.69.2"
11+
gem "rubocop-rake", "~> 0.6.0"
12+
gem "rubocop-rspec", "~> 3.2.0"

2021/ruby/Gemfile.lock

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ GEM
44
ast (2.4.2)
55
awesome_print (1.9.2)
66
coderay (1.1.3)
7-
diff-lcs (1.4.4)
7+
diff-lcs (1.5.1)
8+
json (2.9.0)
9+
language_server-protocol (3.17.0.3)
810
method_source (0.9.2)
9-
parallel (1.21.0)
10-
parser (3.0.3.1)
11+
parallel (1.26.3)
12+
parser (3.3.6.0)
1113
ast (~> 2.4.1)
14+
racc
1215
pry (0.12.2)
1316
coderay (~> 1.1.0)
1417
method_source (~> 0.9.0)
15-
rainbow (3.0.0)
16-
regexp_parser (2.1.1)
17-
rexml (3.2.5)
18+
racc (1.8.1)
19+
rainbow (3.1.1)
20+
regexp_parser (2.9.3)
1821
rspec (3.8.0)
1922
rspec-core (~> 3.8.0)
2023
rspec-expectations (~> 3.8.0)
@@ -28,19 +31,26 @@ GEM
2831
diff-lcs (>= 1.2.0, < 2.0)
2932
rspec-support (~> 3.8.0)
3033
rspec-support (3.8.3)
31-
rubocop (0.93.1)
34+
rubocop (1.69.2)
35+
json (~> 2.3)
36+
language_server-protocol (>= 3.17.0)
3237
parallel (~> 1.10)
33-
parser (>= 2.7.1.5)
38+
parser (>= 3.3.0.2)
3439
rainbow (>= 2.2.2, < 4.0)
35-
regexp_parser (>= 1.8)
36-
rexml
37-
rubocop-ast (>= 0.6.0)
40+
regexp_parser (>= 2.9.3, < 3.0)
41+
rubocop-ast (>= 1.36.2, < 2.0)
3842
ruby-progressbar (~> 1.7)
39-
unicode-display_width (>= 1.4.0, < 2.0)
40-
rubocop-ast (1.14.0)
41-
parser (>= 3.0.1.1)
42-
ruby-progressbar (1.11.0)
43-
unicode-display_width (1.8.0)
43+
unicode-display_width (>= 2.4.0, < 4.0)
44+
rubocop-ast (1.37.0)
45+
parser (>= 3.3.1.0)
46+
rubocop-rake (0.6.0)
47+
rubocop (~> 1.0)
48+
rubocop-rspec (3.2.0)
49+
rubocop (~> 1.61)
50+
ruby-progressbar (1.13.0)
51+
unicode-display_width (3.1.2)
52+
unicode-emoji (~> 4.0, >= 4.0.4)
53+
unicode-emoji (4.0.4)
4454

4555
PLATFORMS
4656
arm64-darwin-24
@@ -50,7 +60,9 @@ DEPENDENCIES
5060
awesome_print
5161
pry (~> 0.12.0)
5262
rspec (~> 3.8.0)
53-
rubocop (~> 0.60)
63+
rubocop (~> 1.69.2)
64+
rubocop-rake (~> 0.6.0)
65+
rubocop-rspec (~> 3.2.0)
5466

5567
RUBY VERSION
5668
ruby 3.3.6p108

2021/ruby/lib/advent_02.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
class DepthCalculator
32
def initialize(data)
43
@data = data
@@ -8,18 +7,18 @@ def initialize(data)
87

98
def process_action(action, amount)
109
case action
11-
when 'forward'
10+
when "forward"
1211
@horizontal_position += amount
13-
when 'down'
12+
when "down"
1413
@depth += amount
15-
when 'up'
14+
when "up"
1615
@depth -= amount
1716
end
1817
end
1918

2019
def process
2120
@data.each do |line|
22-
action, amount = line.split(' ')
21+
action, amount = line.split(" ")
2322
amount = amount.to_i
2423
process_action(action, amount)
2524
end
@@ -30,7 +29,6 @@ def product
3029
end
3130
end
3231

33-
3432
class AimCalculator
3533
def initialize(data)
3634
@data = data
@@ -41,19 +39,19 @@ def initialize(data)
4139

4240
def process_action(action, amount)
4341
case action
44-
when 'forward'
42+
when "forward"
4543
@horizontal_position += amount
4644
@depth += (@aim * amount)
47-
when 'down'
45+
when "down"
4846
@aim += amount
49-
when 'up'
47+
when "up"
5048
@aim -= amount
5149
end
5250
end
5351

5452
def process
5553
@data.each do |line|
56-
action, amount = line.split(' ')
54+
action, amount = line.split(" ")
5755
amount = amount.to_i
5856
process_action(action, amount)
5957
end

2021/ruby/lib/advent_03.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,27 @@ def least_common_bit(data, index)
3131

3232
def episolon_rate
3333
result = (0...@word_length).map { |index| least_common_bit(@data, index) }
34-
result.join('').to_i(2)
34+
result.join("").to_i(2)
3535
end
3636

3737
def gamma_rate
3838
result = (0...@word_length).map { |index| most_common_bit(@data, index) }
39-
result.join('').to_i(2)
39+
result.join("").to_i(2)
4040
end
4141

4242
def power_consumption
4343
episolon_rate * gamma_rate
4444
end
4545

46-
def determine_rating(data, use_most_common_bit = true, index = 0)
47-
return 0 if data.length.zero?
46+
def determine_rating(data, use_most_common_bit: true, index: 0)
47+
return 0 if data.empty?
4848
return data[0].to_i(2) if data.length == 1
4949

5050
bit = use_most_common_bit ? most_common_bit(data, index) : least_common_bit(data, index)
5151
data.select! { |e| e[index] == bit.to_s }
5252
index += 1
5353

54-
determine_rating(data, use_most_common_bit, index)
54+
determine_rating(data, use_most_common_bit: use_most_common_bit, index: index)
5555
end
5656

5757
def oxygen_generator_rating
@@ -61,7 +61,7 @@ def oxygen_generator_rating
6161

6262
def co2_scubber_rating
6363
to_process = @data.map(&:clone)
64-
determine_rating(to_process, false)
64+
determine_rating(to_process, use_most_common_bit: false)
6565
end
6666

6767
def life_support_rating

2021/ruby/spec/advent_01_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
require 'spec_helper'
2-
require 'advent_01'
1+
require "spec_helper"
2+
require "advent_01"
33

4-
describe 'DepthCounter' do
4+
describe "DepthCounter" do
55
let(:puzzle_input) do
6-
File.readlines('./spec/fixtures/advent-01.txt').map(&:to_i)
6+
File.readlines("./spec/fixtures/advent-01.txt").map(&:to_i)
77
end
88

9-
it 'counts the number of increased measurements' do
9+
it "counts the number of increased measurements" do
1010
count = DepthCounter.new(puzzle_input).total_increments
1111
expect(count).to be(1754)
1212
end
1313

14-
it 'counts using window measurement' do
14+
it "counts using window measurement" do
1515
count = DepthCounter.new(puzzle_input).total_window_measurement_increments
1616

1717
expect(count).to be(1789)

2021/ruby/spec/advent_02_spec.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
require 'spec_helper'
2-
require 'advent_02'
1+
require "spec_helper"
2+
require "advent_02"
33

4-
describe 'DepthCalculator' do
4+
describe "DepthCalculator" do
55
let(:puzzle_input) do
6-
File.readlines('./spec/fixtures/advent-02.txt')
6+
File.readlines("./spec/fixtures/advent-02.txt")
77
end
88

9-
it 'calculates the product' do
9+
it "calculates the product" do
1010
dc = DepthCalculator.new(puzzle_input)
1111
dc.process
1212

1313
expect(dc.product).to be(185_581_4)
1414
end
1515
end
1616

17-
describe 'AimCalculator' do
17+
describe "AimCalculator" do
1818
let(:puzzle_input) do
19-
File.readlines('./spec/fixtures/advent-02.txt')
19+
File.readlines("./spec/fixtures/advent-02.txt")
2020
end
2121

22-
it 'calculates the product from sample' do
22+
it "calculates the product from sample" do
2323
sample_input = [
24-
'forward 5',
25-
'down 5',
26-
'forward 8',
27-
'up 3',
28-
'down 8',
29-
'forward 2',
24+
"forward 5",
25+
"down 5",
26+
"forward 8",
27+
"up 3",
28+
"down 8",
29+
"forward 2"
3030
]
3131

3232
dc = AimCalculator.new(sample_input)
@@ -35,7 +35,7 @@
3535
expect(dc.product).to be(900)
3636
end
3737

38-
it 'calculates the product' do
38+
it "calculates the product" do
3939
dc = AimCalculator.new(puzzle_input)
4040
dc.process
4141

2021/ruby/spec/advent_03_spec.rb

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
require 'spec_helper'
2-
require 'advent_03'
1+
require "spec_helper"
2+
require "advent_03"
33

4-
describe 'BinaryDiagnostic' do
4+
describe "BinaryDiagnostic" do
55
let(:puzzle_input) do
6-
File.readlines('./spec/fixtures/advent-03.txt').map(&:chomp)
6+
File.readlines("./spec/fixtures/advent-03.txt").map(&:chomp)
77
end
88

9-
it 'calculates the sample power consumption' do
9+
it "calculates the sample power consumption" do
1010
sample_input = %w[
11-
00100
12-
11110
13-
10110
14-
10111
15-
10101
16-
01111
17-
00111
18-
11100
19-
10000
20-
11001
21-
00010
22-
01010
11+
00100 11110 10110 10111 10101 01111 00111 11100 10000 11001
12+
00010 01010
2313
]
2414

2515
dc = BinaryDiagnostic.new(sample_input)
@@ -34,13 +24,13 @@
3424
expect(dc.life_support_rating).to be(230)
3525
end
3626

37-
it 'calculates the power consumption' do
27+
it "calculates the power consumption" do
3828
dc = BinaryDiagnostic.new(puzzle_input)
3929

4030
expect(dc.power_consumption).to be(388_256_4)
4131
end
4232

43-
it 'calculates life support rating' do
33+
it "calculates life support rating" do
4434
dc = BinaryDiagnostic.new(puzzle_input)
4535

4636
expect(dc.life_support_rating).to be(338_517_0)

0 commit comments

Comments
 (0)