diff --git a/inputs/12_example.txt b/inputs/12_example.txt new file mode 100644 index 0000000..e925935 --- /dev/null +++ b/inputs/12_example.txt @@ -0,0 +1,6 @@ +???.### 1,1,3 +.??..??...?##. 1,1,3 +?#?#?#?#?#?#?#? 1,3,1,6 +????.#...#... 4,1,1 +????.######..#####. 1,6,5 +?###???????? 3,2,1 diff --git a/solutions/12_example.txt b/solutions/12_example.txt new file mode 100644 index 0000000..1d7d7f6 --- /dev/null +++ b/solutions/12_example.txt @@ -0,0 +1,2 @@ +21 +525152 diff --git a/solutions/12_moelf.txt b/solutions/12_moelf.txt new file mode 100644 index 0000000..ebb1807 --- /dev/null +++ b/solutions/12_moelf.txt @@ -0,0 +1,2 @@ +7173 +29826669191291 diff --git a/src/julia/12_moelf.jl b/src/julia/12_moelf.jl new file mode 100644 index 0000000..118cb13 --- /dev/null +++ b/src/julia/12_moelf.jl @@ -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])