Skip to content

Commit 0532ab5

Browse files
committed
Day 19 part 1 Ruby solution
1 parent 98405ae commit 0532ab5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

19-1.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env ruby
2+
3+
class Pattern
4+
def initialize(pattern, towels)
5+
@pattern = pattern
6+
@towels = towels.split(',').map(&:strip)
7+
end
8+
9+
def arrange_towels(arrangement, remaining_pattern, enum)
10+
enum.yield arrangement if remaining_pattern.empty?
11+
12+
@towels.each do |towel|
13+
next unless remaining_pattern.start_with? towel
14+
15+
arrange_towels(arrangement + towel, remaining_pattern[towel.size..], enum)
16+
end
17+
end
18+
19+
def solve
20+
solutions = Enumerator.new do |enum|
21+
arrange_towels('', @pattern, enum)
22+
end
23+
24+
solutions.peek
25+
1
26+
rescue StopIteration
27+
0
28+
end
29+
end
30+
31+
lines = File.read('19.input').lines.map(&:strip)
32+
towels = lines[0]
33+
patterns = lines[2..].map { |l| Pattern.new(l, towels) }
34+
puts patterns.map(&:solve).sum

0 commit comments

Comments
 (0)