parent
2928c03ce0
commit
31afd41329
@ -1,14 +1,70 @@
|
|||||||
p ARGF.readlines(chomp: true).map {|row|
|
input = ARGF.readlines(chomp: true).map {|row|
|
||||||
springs, groups = row.split(" ")
|
springs, groups = row.split(" ")
|
||||||
[springs, groups.scan(/\d+/).map(&:to_i)]
|
[springs, groups.scan(/\d+/).map(&:to_i)]
|
||||||
}.map {|springs, groups|
|
}
|
||||||
unknowns = springs.count(??)
|
|
||||||
(0...2**unknowns).map {|arrangement|
|
p input
|
||||||
arrangement = arrangement.to_s(2).rjust(unknowns, ?0).chars.map { _1 == ?0 ? ?. : ?# }
|
# .map { [ _1.chars, _2 ] }
|
||||||
candidate = springs.gsub(??) { arrangement.shift }
|
.map { [Array.new(5, _1).join(??).chars, _2 * 5] }
|
||||||
candidate.chars
|
.map {|springs, groups|
|
||||||
.slice_when { _1 != _2 }
|
arrangement_counts = Hash.new {|h,k|
|
||||||
.reject { _1[0] == ?. }
|
count, springs, groups = k
|
||||||
.map(&:size) == groups
|
first, *rest = springs
|
||||||
}.count(&:itself)
|
h[k] = case [count, first, groups]
|
||||||
}.sum
|
in count, _, []
|
||||||
|
if count.nonzero?
|
||||||
|
0
|
||||||
|
elsif springs.include?(?#)
|
||||||
|
0
|
||||||
|
else
|
||||||
|
1
|
||||||
|
end
|
||||||
|
in count, _, groups if count > groups.fetch(0)
|
||||||
|
0 # early exit if the damaged group ever gets too large
|
||||||
|
in count, nil, groups # no more springs
|
||||||
|
if groups.size > 1
|
||||||
|
0
|
||||||
|
elsif count == groups.fetch(0)
|
||||||
|
1
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
|
in count, ?#, groups # continue a damaged group
|
||||||
|
h[[count + 1, rest, groups]]
|
||||||
|
in 0, ?., groups
|
||||||
|
h[[0, rest, groups]]
|
||||||
|
in count, ?., groups # finish a damaged group
|
||||||
|
if count == groups.fetch(0)
|
||||||
|
h[[0, rest, groups[1..]]]
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
|
in count, ??, g if count == g.fetch(0) # unknown spring, matched current group
|
||||||
|
h[[0, rest, g[1..]]]
|
||||||
|
in count, ??, g unless count.zero? # unknown spring, ongoing group
|
||||||
|
h[[count + 1, rest, g]]
|
||||||
|
in count, ??, g # unknown spring
|
||||||
|
[
|
||||||
|
h[[count, rest, g]], # undamaged
|
||||||
|
h[[count + 1, rest, g]], # damaged
|
||||||
|
].sum
|
||||||
|
else
|
||||||
|
fail "#{{first:, springs: springs.join, groups:}}"
|
||||||
|
end
|
||||||
|
}
|
||||||
|
arrangement_counts[[0, springs, groups]]
|
||||||
|
}.sum
|
||||||
|
|
||||||
|
# original part one
|
||||||
|
# p input.map {|springs, groups|
|
||||||
|
# unknowns = springs.count(??)
|
||||||
|
# (0...2**unknowns).map {|arrangement|
|
||||||
|
# arrangement = arrangement.to_s(2).rjust(unknowns, ?0).chars.map { _1 == ?0 ? ?. : ?# }
|
||||||
|
# candidate = springs.gsub(??) { arrangement.shift }
|
||||||
|
# candidate.chars
|
||||||
|
# .slice_when { _1 != _2 }
|
||||||
|
# .reject { _1[0] == ?. }
|
||||||
|
# .map(&:size) == groups
|
||||||
|
# }.count(&:itself)
|
||||||
|
# }.sum
|
||||||
|
|
||||||
|
Loading…
Reference in new issue