Skip to content

Commit

Permalink
[Julia] Day 12 (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moelf authored Dec 12, 2023
1 parent 94d5180 commit d1614f1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions inputs/12_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1
2 changes: 2 additions & 0 deletions solutions/12_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
21
525152
2 changes: 2 additions & 0 deletions solutions/12_moelf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
7173
29826669191291
47 changes: 47 additions & 0 deletions src/julia/12_moelf.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const CACHE = Dict()

function rec_count(str, intvec)
get!(CACHE, (str, intvec)) do
if isempty(intvec)
# no group left, illegal to have any '#'
all(!=('#'), str)
elseif sum(intvec) > length(str)
# not enough chars left to form all groups
0
elseif first(str) == '.'
rec_count(str[2:end], intvec)
else
count = 0
if first(str) == '?'
count += rec_count(str[2:end], intvec)
end
N = first(intvec)
# first N is mix of "#?"
# followed by a '.'
stub = get(str, N + 1, '.')
if all(!=('.'), first(str, N)) && stub != '#'
count += rec_count(str[N+2:end], intvec[2:end])
end
count
end
end
end

function impl(pairs, N)
sum(pairs) do (str, intvec)
str, intvec = join(fill(str, N), '?'), repeat(intvec, N)
rec_count(str, intvec)
end
end

function main(path)
lines = map(eachline(path)) do l
str, int_str = split(l)
intvec = parse.(Int, split(int_str, ","))
(str, intvec)
end
println(impl(lines, 1))
println(impl(lines, 5))
end

(abspath(PROGRAM_FILE) == @__FILE__) && main(ARGS[1])

0 comments on commit d1614f1

Please sign in to comment.