Compare commits

...

3 Commits

Author SHA1 Message Date
sejo 029b023901 attempting day 12, part 2 XD 2023-12-12 18:12:21 +01:00
sejo 6e571734f2 day 12, part 1! recursive approach 2023-12-12 17:32:05 +01:00
sejo 5035d78e5e day 12 progress 2023-12-12 07:45:47 +01:00
2 changed files with 100 additions and 0 deletions

94
12023/12/12.lua Normal file
View File

@ -0,0 +1,94 @@
io.input("input")
function is_valid_arrangement(s_damaged, s_proposed)
for i=1,#s_damaged do
local d, p = s_damaged:sub(i,i), s_proposed:sub(i,i)
if d=="#" and p=="." or d=="." and p=="#"then
return false
end
end
return true
end
function gen_proposed(len, g, init)
local t = {}
local ins
for i=1,len do
if i>=init and i<init+g then
ins = "#"
else ins = "." end
table.insert(t,ins)
end
return table.concat(t)
end
function sum(t)
local count = 0
for i,v in ipairs(t) do count=count+v end
return count
end
function count_arrangements(s_damaged, groups)
local groups_copy = {}
table.move(groups, 1, #groups, 1, groups_copy)
local g = table.remove(groups_copy,1)
local count = 0
local len = #s_damaged
if #groups_copy==0 then --last element
for i=1,len-g+1 do
local prop = gen_proposed(len, g, i)
local is_valid = is_valid_arrangement(s_damaged, prop)
if is_valid then count = count + 1 end
end
else -- uy uy uy
local n_remaining = #groups_copy
local sum_remaining = sum(groups_copy)
local buffer = n_remaining + sum_remaining
local active = len - buffer + 1
for i=1,len-buffer do
local prop_len = g+i
local subd = s_damaged:sub(1,prop_len)
local prop = gen_proposed(prop_len, g, i)
local is_valid = is_valid_arrangement(subd, prop)
--print("group"..g,subd, prop, is_valid, s_damaged, prop_len, len)
if is_valid then
local d_remaining = s_damaged:sub(prop_len+1)
--print(g,d_remaining)
count = count + count_arrangements(d_remaining, groups_copy)
end
end
end
return count
end
n = 1
sum1 = 0
sum2 = 0
for line in io.lines() do
--print(line)
print("line",n)
n = n + 1
map, records_list = line:match("([%#%.%?]+) ([%d%,]+)$")
records = {}
for r in records_list:gmatch("(%d+)") do
table.insert(records, tonumber(r))
end
local count = count_arrangements(map, records)
sum1 = sum1 + count
-- part 2
local newmap = map
local newrecords = {}
table.move(records,1,#records,1,newrecords)
for i=1,4 do
newmap = newmap .."?"..map
table.move(records,1,#records,1 + #records*i,newrecords)
end
print(newmap)
local count2 = count_arrangements(newmap, newrecords)
sum2 = sum2 + count2
print(count2)
end
print("part 1", sum1)
print("part 2", sum2)

6
12023/12/test Normal file
View File

@ -0,0 +1,6 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1