Skip to content

Commit

Permalink
rubocopness and file renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
invalidusrname committed Dec 5, 2024
1 parent 6e17bb8 commit 464c3ce
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 316 deletions.
4 changes: 2 additions & 2 deletions 2024/ruby/lib/advent_01_hystorian_hysteria.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions 2024/ruby/lib/advent_02_red_nosed_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion 2024/ruby/lib/advent_03_mull_it_over.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions 2024/ruby/lib/advent_04_ceres_search.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -48,33 +48,33 @@ 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)
[str, str.reverse].any? { |w| w == word }
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
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)

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)

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)

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)

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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
52 changes: 52 additions & 0 deletions 2024/ruby/spec/advent_03_mull_it_over_spec.rb
Original file line number Diff line number Diff line change
@@ -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
53 changes: 0 additions & 53 deletions 2024/ruby/spec/advent_03_spec.rb

This file was deleted.

Loading

0 comments on commit 464c3ce

Please sign in to comment.