Skip to content

Commit

Permalink
Solve day04 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tudorpavel committed Dec 13, 2023
1 parent 766b985 commit fec868d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
22 changes: 22 additions & 0 deletions day04/day04.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class Day04
def solve(lines)
p1 = 0

lines.each do |line|
_card, winning, mine = line.split(/:|\|/)
win_count = nums(winning).intersection(nums(mine)).size

p1 += 2**(win_count - 1) unless win_count.zero?
end

[p1, -2]
end

private

def nums(str)
str.scan(/(\d+)/).flatten.map(&:to_i)
end
end
6 changes: 6 additions & 0 deletions day04/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

require_relative '../utils/run_day'
require_relative 'day04'

RunDay.run(Day04.new)
36 changes: 36 additions & 0 deletions spec/day04/day04_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require_relative '../../day04/day04'

RSpec.describe Day04 do
subject(:solve) { described_class.new.solve(lines) }

let(:lines) do
[
'Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53',
'Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19',
'Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1',
'Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83',
'Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36',
'Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11'
]
end

describe '#solve' do
context 'when part 1' do
it 'returns the right result' do
p1, _p2 = solve

expect(p1).to eq(13)
end
end

context 'when part 2' do
it 'returns the right result' do
_p1, p2 = solve

expect(p2).to eq(-2)
end
end
end
end

0 comments on commit fec868d

Please sign in to comment.