-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12part1.rb
33 lines (29 loc) · 851 Bytes
/
day12part1.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
file = File.open "day12in.txt"
def valid?(str, groups)
damaged_groups = str.split(".").reject(&:empty?)
matches = damaged_groups.length == groups.length
damaged_groups.each_with_index { |elem, i| matches &= elem.length == groups[i] rescue false }
matches
end
rows = []
file.each_line do |l|
(springs, groups) = l.chomp.split(" ")
rows.push({ springs: springs.split(""), groups: groups.split(",").map(&:to_i), spring: springs })
end
sum = 0
rows.each do |row|
(springs, groups) = [row[:springs], row[:groups]]
bangs = []
springs.each_with_index do |s, i|
bangs.push i if s == "?"
end
row_matches = 0
str = row[:spring]
str.gsub!("?", "0")
(2 ** bangs.length).times do
row_matches += 1 if valid?(str.gsub("0", ".").gsub("1", "#"), groups)
str = str.gsub("1", "9").succ
end
sum += row_matches
end
puts sum