Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico-Posada authored Dec 12, 2023
1 parent 514703d commit bdbf669
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Day-12/part1.rb
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
48 changes: 48 additions & 0 deletions Day-12/part2.rb
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)
}

0 comments on commit bdbf669

Please sign in to comment.