-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path12-1.rb
executable file
·80 lines (66 loc) · 1.51 KB
/
12-1.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env ruby
class Springs
attr_accessor :row, :groupings
def initialize(line)
row, groupings = line.split
@row = row.chars
@groupings = groupings.split(',').map(&:to_i)
end
def possible?(row)
groups = []
in_group = false
cur_group = 0
row.each_index do |n|
case row[n]
when '?'
return true
when '.'
in_group = false
if cur_group > 0
groups << cur_group
cur_group = 0
return false unless groups[groups.size - 1] == @groupings[groups.size - 1]
end
when '#'
cur_group = 0 unless in_group
in_group = true
cur_group += 1
end
end
groups << cur_group if cur_group > 0
if groups[groups.size - 1] != @groupings[groups.size - 1] or
groups.size != @groupings.size
return false
end
true
end
def possibilities
queue = [@row.dup]
definite = []
until queue.empty?
p = queue.shift
next unless possible?(p)
next_unknown = p.index '?'
if next_unknown
temp = p.dup
temp[next_unknown] = '.'
queue << temp
temp = p.dup
temp[next_unknown] = '#'
queue << temp
else
definite << p
end
end
definite
end
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 { |s| s.possibilities.size }, "\n"