-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
514703d
commit bdbf669
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!ruby | ||
a = $<.map{ | ||
a,b=_1.split | ||
[a, b.split(?,).map(&:to_i)] | ||
} | ||
|
||
$s = 0 | ||
def solve(str, str_idx, list) | ||
if str_idx == str.size | ||
m = str.scan(/#+/).map &:size | ||
$s += 1 if m.size == list.size && m.zip(list).all?{_1 == _2} | ||
elsif str[str_idx] != ?? | ||
solve(str, str_idx + 1, list) | ||
else | ||
idx = str.index(??) || str.size | ||
m = str[0, idx].scan(/#+/) | ||
return if m.size > list.size | ||
|
||
m.each_with_index do |s, i| | ||
return if s.size > list[i] | ||
end | ||
|
||
copy = str.dup | ||
copy[str_idx] = ?. | ||
solve(copy, str_idx + 1, list) | ||
|
||
str[str_idx] = ?# | ||
solve(str, str_idx + 1, list) | ||
end | ||
end | ||
|
||
a.each do |list, chunks| | ||
prev = $s | ||
solve(list, 0, chunks) | ||
p $s - prev | ||
end | ||
|
||
p $s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!ruby | ||
$memo = {} | ||
|
||
def solve(str, nums) | ||
memo_arr = [str, *nums] | ||
return $memo[memo_arr] if $memo.has_key?(memo_arr) | ||
|
||
t = 0 | ||
first, *rest = nums | ||
if first.nil? | ||
result = str.size == 0 || !str[?#] ? 1 : 0 | ||
$memo[memo_arr] = result | ||
return result | ||
end | ||
|
||
if str.size == 0 | ||
$memo[memo_arr] = 0 | ||
return 0 | ||
end | ||
|
||
reg = /^[\?\#]{#{first}}([\.\?]\.*(.*)|)$/ | ||
if str =~ /^\?+/ | ||
$~.end(0).times do | ||
if str =~ reg | ||
t += solve($2||'', rest) | ||
end | ||
|
||
str = str[1..] | ||
end | ||
end | ||
|
||
if str =~ reg | ||
t += solve($2||'', rest) | ||
elsif str =~ /^\.+/ | ||
t += solve(str.gsub(/^\.+/, ''), nums) | ||
end | ||
|
||
$memo[memo_arr] = t | ||
return t | ||
end | ||
|
||
p $<.sum{|input| | ||
l, r = input.split | ||
r = r.split(?,).map(&:to_i) * 5 | ||
l = ([l] * 5) * ?? | ||
l.gsub!(/^\.+|\.+$/,'') | ||
solve(l, r) | ||
} |