Zebra puzzle: Difference between revisions

Content deleted Content added
Thundergnat (talk | contribs)
Rename Perl 6 -> Raku, alphabetize, minor clean-up
→‎{{header|F_Sharp|F#}}: Use impoved Ring.Plainchanges
Line 2,530:
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/[Permutations_by_swapping#F.23]]
<lang fsharp>
(*Here I solve the Zebra puzzle using Plain Changes, definitely a challenge to some campanoligist to solve it using Grandsire Doubles.
Nigel Galloway: January 27th., 2017 *)
type N = |English=0 |Swedish=1|Danish=2 |German=3|Norwegian=4
type I = |Tea=0 |Coffee=1 |Milk=2 |Beer=3 |Water=4
type G = |Dog=0 |Birds=1 |Cats=2 |Horse=3 |Zebra=4
type E = |Red=0 |Green=1 |White=2 |Blue=3 |Yellow=4
type L = |PallMall=0|Dunhill=1|BlueMaster=2|Prince=3|Blend=4
type NIGELz={Nz:N[];Iz:I[];Gz:G[];Ez:E[];Lz:L[]}
let fn (i:'n[]) g (e:'g[]) l = //coincident?
Line 2,553:
_fn 0
let fg (i:'n[]) g (e:'g[]) l = (fi i g e l || fi e l i g) //adjacent?
let n = Ring.PlainChanges [|N.English;for N.Swedish;n Nin System.Danish; NEnum.German; GetValues(typeof<N>)->n:?>N.Norwegian|]|>Seq.filter(fun n->match n with |Some(n)->(n.[0]=N.Norwegian) |_->false) //#10
let i = Ring.PlainChanges [|I.Tea;for n in ISystem.Coffee; IEnum.Milk; GetValues(typeof<I.Beer; >)->n:?>I.Water |]|>Seq.filter(fun n->match n with |Some(n)->(n.[2]=I.Milk) |_->false) //# 9
let g = Ring.PlainChanges [|G.Dog;for n in GSystem.Birds; GEnum.Cats; GetValues(typeof<G.Horse; G.Zebra |]|>Seq.filter(fun n)->match n with |Some(n)-:?>true G|_->false)]
let e = Ring.PlainChanges [|E.Red;for n in ESystem.Green; EEnum.White; GetValues(typeof<E.Blue; >)->n:?>E.Yellow |]|>Seq.filter(fun n->match n with |Some(n)->fi n E.Green n E.White|_->false) //# 5
let l = Ring.PlainChanges [|L.PallMall;L.Dunhill;for L.BlueMaster;L.Prince;n L.Blendin |]|>SeqSystem.filterEnum.GetValues(fun n-typeof<L>match n with |Some(n)->true n:?>L|_->false)]
match n|>Seq.map(fun n->{Nz=Option.get n;Iz=[||];Gz=[||];Ez=[||];Lz=[||]})
|>Seq.collect(fun n->i|>Seq.map(fun i->{n with Iz=Option.get i}))|>Seq.filter(fun n-> fn n.Nz N.Danish n.Iz I.Tea) //# 4
|>Seq.collect(fun n->g|>Seq.map(fun i->{n with Gz=Option.get i}))|>Seq.filter(fun n-> fn n.Nz N.Swedish n.Gz G.Dog) //# 3
|>Seq.collect(fun n->e|>Seq.map(fun i->{n with Ez=Option.get i}))|>Seq.filter(fun n-> fn n.Nz N.English n.Ez E.Red && //# 2
fn n.Ez E.Green n.Iz I.Coffee&& //# 6
fg n.Nz N.Norwegian n.Ez E.Blue) //#15
|>Seq.collect(fun n->l|>Seq.map(fun i->{n with Lz=Option.get i}))|>Seq.tryFind(fun n->fn n.Lz L.PallMall n.Gz G.Birds && //# 7
fg n.Lz L.Blend n.Gz G.Cats && //#11
fn n.Lz L.Prince n.Nz N.German&& //#14
fg n.Lz L.Blend n.Iz I.Water && //#16
fg n.Lz L.Dunhill n.Gz G.Horse && //#12
fn n.Lz L.Dunhill n.Ez E.Yellow&& //# 8
fn n.Iz I.Beer n.Lz L.BlueMaster) with //#13
|Some(nn) -> nn.Gz |> Array.iteri(fun n g -> if (g = G.Zebra) then printfn "\nThe man who owns a zebra is %A\n" nn.Nz.[n]); printfn "%A" nn
|None -> printfn "No solution found"