File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
1818end
You canβt perform that action at this time.
0 commit comments