Solve hanging lantern problem: Difference between revisions

Content added Content deleted
(→‎fast analytical 1..N count only: replaced with more flexible version, now needs/copes with eg {1,3,3})
(→‎{{header|Julia}}: allow larger entries)
Line 88: Line 88:
=={{header|Julia}}==
=={{header|Julia}}==
<lang ruby>""" rosettacode.org /wiki/Lantern_Problem """
<lang ruby>""" rosettacode.org /wiki/Lantern_Problem """

using Combinatorics
using Combinatorics

function lanternproblem(verbose = true)
function lanternproblem(verbose = true)
println("Input number of columns, then column heights in sequence:")
println("Input number of columns, then column heights in sequence:")
inputs = [parse(Int, i) for i in split(readline(), r"\s+")]
inputs = [parse(Int, i) for i in split(readline(), r"\s+")]
n = popfirst!(inputs)
n = popfirst!(inputs)
println("\nThere are ", multinomial(BigInt.(inputs)...), " ways to take these ", n, " columns down.")
takedownways = unique(permutations(reduce(vcat, [fill(i, m) for (i, m) in enumerate(inputs)])))
println("\nThere are ", length(takedownways), " ways to take these ", n, " columns down.")

if verbose
if verbose
idx, fullmat = 0, zeros(Int, n, maximum(n))
idx, fullmat = 0, zeros(Int, n, maximum(n))
Line 107: Line 106:
show(stdout, "text/plain", map(n -> n > 0 ? "$n " : " ", fullmat))
show(stdout, "text/plain", map(n -> n > 0 ? "$n " : " ", fullmat))
println("\n")
println("\n")
takedownways = unique(permutations(reduce(vcat, [fill(i, m) for (i, m) in enumerate(inputs)])))
for way in takedownways
for way in takedownways
print("[")
print("[")
Line 118: Line 118:
end
end
end
end

lanternproblem()
lanternproblem()
lanternproblem()
lanternproblem(false)
</lang>{{out}}
</lang>{{out}}
<pre style="height:64ex;overflow:scroll">
<pre style="height:64ex;overflow:scroll">
Line 342: Line 344:
[7, 6, 5, 4, 3, 1, 2]
[7, 6, 5, 4, 3, 1, 2]
[7, 6, 5, 4, 3, 2, 1]
[7, 6, 5, 4, 3, 2, 1]

Input number of columns, then column heights in sequence:
9 1 2 3 4 5 6 7 8 9

There are 65191584694745586153436251091200000 ways to take these 9 columns down.
</pre>
</pre>