File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments