-
Notifications
You must be signed in to change notification settings - Fork 1
/
12-2.rb
executable file
·65 lines (53 loc) · 1.41 KB
/
12-2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ruby
require 'memoist'
class Springs
extend Memoist
attr_accessor :row, :groupings
def initialize(line)
row, groupings = line.split
@row = ([row] * 5).join('?').chars
@groupings = groupings.split(',').map(&:to_i) * 5
end
def possibilities
count_possibilities(@row, @groupings, 0)
end
def count_possibilities(row, groupings, current_group_size)
if row.empty?
if groupings.empty? or
(groupings.size == 1 and groupings[0] == current_group_size)
return 1
else
return 0
end
end
arrangements = 0
cases = if row[0] == '?'
['#', '.']
else
[row[0]]
end
cases.each do |c|
case c
when '#'
arrangements += count_possibilities(row[1..], groupings, current_group_size + 1) unless groupings.empty?
when '.'
if current_group_size == groupings[0]
arrangements += count_possibilities(row[1..], groupings[1..], 0)
elsif current_group_size.zero?
arrangements += count_possibilities(row[1..], groupings, 0)
end
end
end
arrangements
end
memoize :count_possibilities
def inspect
to_s
end
def to_s
"<#{self.class}: #{@row.join} #{@groupings.join}>"
end
end
input = File.read('12.input').lines.map(&:strip)
springs = input.map { |l| Springs.new(l) }
print springs.sum(&:possibilities), "\n"