June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -23,5 +23,5 @@
|
|||
)
|
||||
)
|
||||
& solution$(.!people)
|
||||
| { After outputting all solutions, the lhs of the | operator fails. The rhs of the | operator, here an empty string, is the final result. }
|
||||
| { After outputting all solutions, the lhs of the | operator fails. The rhs of the | operator, here an empty string, is the final result. }
|
||||
);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using static System.Linq.Enumerable;
|
||||
|
||||
static class Program
|
||||
{
|
||||
enum Tenants { Baker = 0, Cooper = 1, Fletcher = 2, Miller = 3, Smith = 4 };
|
||||
|
||||
static void Main()
|
||||
{
|
||||
var count = Enum.GetNames(typeof(Tenants)).Length;
|
||||
var top = count - 1;
|
||||
|
||||
var solve =
|
||||
from f in Range(0, count).Permutations()
|
||||
let floors = f.ToArray()
|
||||
where floors[(int)Tenants.Baker] != top //r1
|
||||
where floors[(int)Tenants.Cooper] != 0 //r2
|
||||
where floors[(int)Tenants.Fletcher] != top && floors[(int)Tenants.Fletcher] != 0 //r3
|
||||
where floors[(int)Tenants.Miller] > floors[(int)Tenants.Cooper] //r4
|
||||
where Math.Abs(floors[(int)Tenants.Smith] - floors[(int)Tenants.Fletcher]) !=1 //r5
|
||||
where Math.Abs(floors[(int)Tenants.Fletcher] - floors[(int)Tenants.Cooper]) !=1 //r6
|
||||
select floors;
|
||||
var solved = solve.First();
|
||||
var output = Range(0,count).OrderBy(i=>solved[i]).Select(f => ((Tenants)f).ToString());
|
||||
Console.WriteLine(String.Join(" ", output));
|
||||
Console.Read();
|
||||
}
|
||||
|
||||
public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> values)
|
||||
{
|
||||
if (values.Count() == 1)
|
||||
return values.ToSingleton();
|
||||
|
||||
return values.SelectMany(v => Permutations(values.Except(v.ToSingleton())), (v, p) => p.Prepend(v));
|
||||
}
|
||||
|
||||
public static IEnumerable<T> ToSingleton<T>(this T item) { yield return item; }
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
using Combinatorics
|
||||
|
||||
function solve(n::Vector{<:AbstractString}, pred::Vector{<:Function})
|
||||
rst = Vector{typeof(n)}(0)
|
||||
for candidate in permutations(n)
|
||||
if all(p(candidate) for p in predicates)
|
||||
push!(rst, candidate)
|
||||
end
|
||||
end
|
||||
return rst
|
||||
end
|
||||
|
||||
Names = ["Baker", "Cooper", "Fletcher", "Miller", "Smith"]
|
||||
predicates = [
|
||||
(s) -> last(s) != "Baker",
|
||||
(s) -> first(s) != "Cooper",
|
||||
(s) -> first(s) != "Fletcher" && last(s) != "Fletcher",
|
||||
(s) -> findfirst(s, "Miller") > findfirst(s, "Cooper"),
|
||||
(s) -> abs(findfirst(s, "Smith") - findfirst(s, "Fletcher")) != 1,
|
||||
(s) -> abs(findfirst(s, "Cooper") - findfirst(s, "Fletcher")) != 1]
|
||||
|
||||
solutions = solve(Names, predicates)
|
||||
foreach(x -> println(join(x, ", ")), solutions)
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
perm: {x@m@&n=(#?:)'m:!n#n:#x}
|
||||
filter: {y[& x'y]}
|
||||
reject: {y[& ~x'y]}
|
||||
adjacent: {1 = _abs (z?x) - (z?y)}
|
||||
|
||||
p: perm[`Baker `Cooper `Fletcher `Miller `Smith]
|
||||
p: reject[{`Cooper=x[0]}; p]
|
||||
p: reject[{`Baker=x[4]}; p]
|
||||
p: filter[{(x ? `Miller) > (x ? `Cooper)}; p]
|
||||
p: reject[{adjacent[`Smith; `Fletcher; x]}; p]
|
||||
p: reject[{adjacent[`Cooper; `Fletcher; x]}; p]
|
||||
p: reject[{(x ? `Fletcher)_in (0 4)}; p]
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
local wrap, yield = coroutine.wrap, coroutine.yield
|
||||
local function perm(n)
|
||||
local r = {}
|
||||
for i=1,n do r[i]=i end
|
||||
return wrap(function()
|
||||
local function swap(m)
|
||||
if m==0 then
|
||||
yield(r)
|
||||
else
|
||||
for i=m,1,-1 do
|
||||
r[i],r[m]=r[m],r[i]
|
||||
swap(m-1)
|
||||
r[i],r[m]=r[m],r[i]
|
||||
end
|
||||
end
|
||||
end
|
||||
swap(n)
|
||||
end)
|
||||
end
|
||||
|
||||
local function iden(...)return ... end
|
||||
local function imap(t,f)
|
||||
local r,fn = {m=imap, c=table.concat, u=table.unpack}, f or iden
|
||||
for i=1,#t do r[i]=fn(t[i])end
|
||||
return r
|
||||
end
|
||||
|
||||
local tenants = {'Baker', 'Cooper', 'Fletcher', 'Miller', 'Smith'}
|
||||
|
||||
local conds = {
|
||||
'Baker ~= TOP',
|
||||
'Cooper ~= BOTTOM',
|
||||
'Fletcher ~= TOP and Fletcher~= BOTTOM',
|
||||
'Miller > Cooper',
|
||||
'Smith + 1 ~= Fletcher and Smith - 1 ~= Fletcher',
|
||||
'Cooper + 1 ~= Fletcher and Cooper - 1 ~= Fletcher',
|
||||
}
|
||||
|
||||
local function makePredicate(conds, tenants)
|
||||
return load('return function('..imap(tenants):c','..
|
||||
') return ' ..
|
||||
imap(conds,function(c)
|
||||
return string.format("(%s)",c)
|
||||
end):c"and "..
|
||||
" end ",'-',nil,{TOP=5, BOTTOM=1})()
|
||||
end
|
||||
|
||||
local function solve (conds, tenants)
|
||||
local try, pred, upk = perm(#tenants), makePredicate(conds, tenants), table.unpack
|
||||
local answer = try()
|
||||
while answer and not pred(upk(answer)) do answer = try()end
|
||||
if answer then
|
||||
local floor = 0
|
||||
return imap(answer, function(person)
|
||||
floor=floor+1;
|
||||
return string.format(" %s lives on floor %d",tenants[floor],person)
|
||||
end):c"\n"
|
||||
else
|
||||
return nil, 'no solution'
|
||||
end
|
||||
end
|
||||
|
||||
print(solve (conds, tenants))
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
names = unlist(strsplit("baker cooper fletcher miller smith", " "))
|
||||
|
||||
test <- function(floors) {
|
||||
f <- function(name) which(name == floors)
|
||||
if ((f('baker') != 5) &&
|
||||
(f('cooper') != 1) &&
|
||||
(any(f('fletcher') == 2:4)) &&
|
||||
(f('miller') > f('cooper')) &&
|
||||
(abs(f('fletcher') - f('cooper')) > 1) &&
|
||||
(abs(f('smith') - f('fletcher')) > 1))
|
||||
cat("\nFrom bottom to top: --> ", floors, "\n")
|
||||
}
|
||||
|
||||
do.perms <- function(seq, func, built = c()){
|
||||
if (0 == length(seq)) func(built)
|
||||
else for (x in seq) do.perms( seq[!seq==x], func, c(x, built)) }
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
> do.perms(names, test)
|
||||
From bottom to top: --> smith cooper baker fletcher miller
|
||||
|
||||
> system.time(do.perms(names, test))
|
||||
From bottom to top: --> smith cooper baker fletcher miller
|
||||
user system elapsed
|
||||
0 0 0
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
N = %w(Baker Cooper Fletcher Miller Smith)
|
||||
b,c,f,m,s = N
|
||||
|
||||
N.permutation.map{|a| a.join " "}.
|
||||
grep(/(?=.*#{b}.)
|
||||
(?=.+#{c})
|
||||
(?=.+#{f}.)
|
||||
(?=.*#{c}.*#{m})
|
||||
(?=.*(#{f}..+#{s}|#{s}..+#{f}))
|
||||
(?=.*(#{f}..+#{c}|#{c}..+#{f}))/x).
|
||||
first
|
||||
Loading…
Add table
Add a link
Reference in a new issue