From a1a0a11442476f7b0e4c02104339e558d81c8e18 Mon Sep 17 00:00:00 2001 From: Ariejan de Vroom Date: Thu, 19 Dec 2024 07:06:04 +0100 Subject: [PATCH] Day 19 - Puzzle 2 --- lib/solutions/day_19.rb | 31 ++++++++++++++++++++++++++++++- spec/solutions/day_19_spec.rb | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/solutions/day_19.rb b/lib/solutions/day_19.rb index 09eefdd..de19f5f 100644 --- a/lib/solutions/day_19.rb +++ b/lib/solutions/day_19.rb @@ -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 diff --git a/spec/solutions/day_19_spec.rb b/spec/solutions/day_19_spec.rb index 46306d4..704ffa6 100644 --- a/spec/solutions/day_19_spec.rb +++ b/spec/solutions/day_19_spec.rb @@ -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