Skip to content

Commit a1a0a11

Browse files
committed
Day 19 - Puzzle 2
1 parent 47f4467 commit a1a0a11

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

β€Žlib/solutions/day_19.rbβ€Ž

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,36 @@ def part_one(input)
55
end
66

77
def part_two(input)
8-
0
8+
sim = DayTwo.new(input)
9+
sim.run!
10+
end
11+
12+
class DayTwo
13+
def initialize(input)
14+
lines = input.split("\n")
15+
@towels = lines[0].split(",").map(&:strip)
16+
@patterns = lines[2..-1]
17+
end
18+
19+
def possible?(pattern)
20+
possibles = Array.new(pattern.size + 1, 0)
21+
possibles[0] = 1
22+
23+
(0...pattern.size).each do |i|
24+
next if possibles[i] == 0
25+
@towels.each do |towel|
26+
if pattern[i..-1].start_with?(towel)
27+
possibles[i + towel.size] += possibles[i]
28+
end
29+
end
30+
end
31+
32+
possibles[pattern.size]
33+
end
34+
35+
def run!
36+
@patterns.reduce(0) { |count, pattern| count += possible?(pattern) }
37+
end
938
end
1039

1140
class DayOne

β€Žspec/solutions/day_19_spec.rbβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
describe '#part_two' do
1414
it 'calculates the correct solutions for part two' do
15-
expect(subject.part_two(input)).to eq(0)
15+
expect(subject.part_two(input)).to eq(16)
1616
end
1717
end
1818
end

0 commit comments

Comments
Β (0)