Skip to content

Commit

Permalink
Day 19 - Puzzle 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ariejan committed Dec 19, 2024
1 parent 47f4467 commit a1a0a11
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
31 changes: 30 additions & 1 deletion lib/solutions/day_19.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,36 @@ def part_one(input)
end

def part_two(input)
0
sim = DayTwo.new(input)
sim.run!
end

class DayTwo
def initialize(input)
lines = input.split("\n")
@towels = lines[0].split(",").map(&:strip)
@patterns = lines[2..-1]
end

def possible?(pattern)
possibles = Array.new(pattern.size + 1, 0)
possibles[0] = 1

(0...pattern.size).each do |i|
next if possibles[i] == 0
@towels.each do |towel|
if pattern[i..-1].start_with?(towel)
possibles[i + towel.size] += possibles[i]
end
end
end

possibles[pattern.size]
end

def run!
@patterns.reduce(0) { |count, pattern| count += possible?(pattern) }
end
end

class DayOne
Expand Down
2 changes: 1 addition & 1 deletion spec/solutions/day_19_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

describe '#part_two' do
it 'calculates the correct solutions for part two' do
expect(subject.part_two(input)).to eq(0)
expect(subject.part_two(input)).to eq(16)
end
end
end
Expand Down

0 comments on commit a1a0a11

Please sign in to comment.