Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
85
Task/Zebra-puzzle/C-sharp/zebra-puzzle-1.cs
Normal file
85
Task/Zebra-puzzle/C-sharp/zebra-puzzle-1.cs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using static System.Console;
|
||||
|
||||
public enum Colour { Red, Green, White, Yellow, Blue }
|
||||
public enum Nationality { Englishman, Swede, Dane, Norwegian,German }
|
||||
public enum Pet { Dog, Birds, Cats, Horse, Zebra }
|
||||
public enum Drink { Coffee, Tea, Milk, Beer, Water }
|
||||
public enum Smoke { PallMall, Dunhill, Blend, BlueMaster, Prince}
|
||||
|
||||
public static class ZebraPuzzle
|
||||
{
|
||||
private static (Colour[] colours, Drink[] drinks, Smoke[] smokes, Pet[] pets, Nationality[] nations) _solved;
|
||||
|
||||
static ZebraPuzzle()
|
||||
{
|
||||
var solve = from colours in Permute<Colour>() //r1 5 range
|
||||
where (colours,Colour.White).IsRightOf(colours, Colour.Green) // r5
|
||||
from nations in Permute<Nationality>()
|
||||
where nations[0] == Nationality.Norwegian // r10
|
||||
where (nations, Nationality.Englishman).IsSameIndex(colours, Colour.Red) //r2
|
||||
where (nations,Nationality.Norwegian).IsNextTo(colours,Colour.Blue) // r15
|
||||
from drinks in Permute<Drink>()
|
||||
where drinks[2] == Drink.Milk //r9
|
||||
where (drinks, Drink.Coffee).IsSameIndex(colours, Colour.Green) // r6
|
||||
where (drinks, Drink.Tea).IsSameIndex(nations, Nationality.Dane) //r4
|
||||
from pets in Permute<Pet>()
|
||||
where (pets, Pet.Dog).IsSameIndex(nations, Nationality.Swede) // r3
|
||||
from smokes in Permute<Smoke>()
|
||||
where (smokes, Smoke.PallMall).IsSameIndex(pets, Pet.Birds) // r7
|
||||
where (smokes, Smoke.Dunhill).IsSameIndex(colours, Colour.Yellow) // r8
|
||||
where (smokes, Smoke.Blend).IsNextTo(pets, Pet.Cats) // r11
|
||||
where (smokes, Smoke.Dunhill).IsNextTo(pets, Pet.Horse) //r12
|
||||
where (smokes, Smoke.BlueMaster).IsSameIndex(drinks, Drink.Beer) //r13
|
||||
where (smokes, Smoke.Prince).IsSameIndex(nations, Nationality.German) // r14
|
||||
where (drinks,Drink.Water).IsNextTo(smokes,Smoke.Blend) // r16
|
||||
select (colours, drinks, smokes, pets, nations);
|
||||
|
||||
_solved = solve.First();
|
||||
}
|
||||
|
||||
private static int IndexOf<T>(this T[] arr, T obj) => Array.IndexOf(arr, obj);
|
||||
|
||||
private static bool IsRightOf<T, U>(this (T[] a, T v) right, U[] a, U v) => right.a.IndexOf(right.v) == a.IndexOf(v) + 1;
|
||||
|
||||
private static bool IsSameIndex<T, U>(this (T[] a, T v)x, U[] a, U v) => x.a.IndexOf(x.v) == a.IndexOf(v);
|
||||
|
||||
private static bool IsNextTo<T, U>(this (T[] a, T v)x, U[] a, U v) => (x.a,x.v).IsRightOf(a, v) || (a,v).IsRightOf(x.a,x.v);
|
||||
|
||||
// made more generic from https://codereview.stackexchange.com/questions/91808/permutations-in-c
|
||||
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[]> Permute<T>() => ToEnumerable<T>().Permutations().Select(p=>p.ToArray());
|
||||
|
||||
private static IEnumerable<T> ToSingleton<T>(this T item){ yield return item; }
|
||||
|
||||
private static IEnumerable<T> ToEnumerable<T>() => Enum.GetValues(typeof(T)).Cast<T>();
|
||||
|
||||
public static new String ToString()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("House Colour Drink Nationality Smokes Pet");
|
||||
sb.AppendLine("───── ────── ──────── ─────────── ────────── ─────");
|
||||
var (colours, drinks, smokes, pets, nations) = _solved;
|
||||
for (var i = 0; i < 5; i++)
|
||||
sb.AppendLine($"{i+1,5} {colours[i],-6} {drinks[i],-8} {nations[i],-11} {smokes[i],-10} {pets[i],-10}");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static void Main(string[] arguments)
|
||||
{
|
||||
var owner = _solved.nations[_solved.pets.IndexOf(Pet.Zebra)];
|
||||
WriteLine($"The zebra owner is {owner}");
|
||||
Write(ToString());
|
||||
Read();
|
||||
}
|
||||
}
|
||||
109
Task/Zebra-puzzle/C-sharp/zebra-puzzle-2.cs
Normal file
109
Task/Zebra-puzzle/C-sharp/zebra-puzzle-2.cs
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using static System.Console;
|
||||
|
||||
namespace ZebraPuzzleSolver
|
||||
{
|
||||
public enum Colour { Red, Green, White, Yellow, Blue }
|
||||
public enum Nationality { Englishman, Swede, Dane, Norwegian, German }
|
||||
public enum Pet { Dog, Birds, Cats, Horse, Zebra }
|
||||
public enum Drink { Coffee, Tea, Milk, Beer, Water }
|
||||
public enum Smoke { PallMall, Dunhill, Blend, BlueMaster, Prince }
|
||||
|
||||
public struct House
|
||||
{
|
||||
public Drink D { get; }
|
||||
public Colour C { get; }
|
||||
public Pet P { get; }
|
||||
public Nationality N { get; }
|
||||
public Smoke S { get; }
|
||||
|
||||
House(Drink d, Colour c, Pet p, Nationality n, Smoke s) => (D, C, P, N, S) = (d, c, p, n, s);
|
||||
|
||||
public static House Create(Drink d, Colour c, Pet p, Nationality n, Smoke s) => new House(d, c, p, n, s);
|
||||
|
||||
public bool AllUnequal(House other) => D != other.D && C != other.C && P != other.P && N != other.N && S != other.S;
|
||||
|
||||
public override string ToString() =>$"{C,-6} {D,-8} {N,-11} {S,-10} {P,-10}";
|
||||
}
|
||||
|
||||
public static class LinqNoPerm
|
||||
{
|
||||
public static IEnumerable<T> ToEnumerable<T>() => Enum.GetValues(typeof(T)).Cast<T>();
|
||||
|
||||
public static IEnumerable<House> FreeCandidates(this IEnumerable<House> houses, IEnumerable<House> picked) =>
|
||||
houses.Where(house => picked.All(house.AllUnequal));
|
||||
|
||||
static Dictionary<Type, Func<House, dynamic, bool>> _eFn = new Dictionary<Type, Func<House, dynamic, bool>>
|
||||
{ {typeof(Drink),(h,e)=>h.D==e},
|
||||
{typeof(Nationality),(h,e)=>h.N==e},
|
||||
{typeof(Colour),(h,e)=>h.C==e},
|
||||
{typeof(Pet),(h,e)=>h.P==e},
|
||||
{typeof(Smoke),(h, e)=>h.S==e}
|
||||
};
|
||||
|
||||
public static bool IsNextTo<T, U>(this IEnumerable<House> hs,T t, U u) => hs.IsLeftOf(t,u) || hs.IsLeftOf(u, t);
|
||||
|
||||
public static bool IsLeftOf<T, U>(this IEnumerable<House> hs, T left, U right) =>
|
||||
hs.Zip(hs.Skip(1), (l, r) => (_eFn[left.GetType()](l, left) && _eFn[right.GetType()](r, right))).Any(l => l);
|
||||
|
||||
static House[] _solved;
|
||||
|
||||
static LinqNoPerm()
|
||||
{
|
||||
var candidates =
|
||||
from colours in ToEnumerable<Colour>()
|
||||
from nations in ToEnumerable<Nationality>()
|
||||
from drinks in ToEnumerable<Drink>()
|
||||
from pets in ToEnumerable<Pet>()
|
||||
from smokes in ToEnumerable<Smoke>()
|
||||
where (colours == Colour.Red) == (nations == Nationality.Englishman) //r2
|
||||
where (nations == Nationality.Swede) == (pets == Pet.Dog) //r3
|
||||
where (nations == Nationality.Dane) == (drinks == Drink.Tea) //r4
|
||||
where (colours == Colour.Green) == (drinks == Drink.Coffee) //r6
|
||||
where (smokes == Smoke.PallMall) == (pets == Pet.Birds) //r7
|
||||
where (smokes == Smoke.Dunhill) == (colours == Colour.Yellow) // r8
|
||||
where (smokes == Smoke.BlueMaster) == (drinks == Drink.Beer) //r13
|
||||
where (smokes == Smoke.Prince) == (nations == Nationality.German) // r14
|
||||
select House.Create(drinks,colours,pets,nations, smokes);
|
||||
var members =
|
||||
from h1 in candidates
|
||||
where h1.N == Nationality.Norwegian //r10
|
||||
from h3 in candidates.FreeCandidates(new[] { h1 })
|
||||
where h3.D == Drink.Milk //r9
|
||||
from h2 in candidates.FreeCandidates(new[] { h1, h3 })
|
||||
let h123 = new[] { h1, h2, h3 }
|
||||
where h123.IsNextTo(Nationality.Norwegian, Colour.Blue) //r15
|
||||
where h123.IsNextTo(Smoke.Blend, Pet.Cats)//r11
|
||||
where h123.IsNextTo(Smoke.Dunhill, Pet.Horse) //r12
|
||||
from h4 in candidates.FreeCandidates(h123)
|
||||
from h5 in candidates.FreeCandidates(new[] { h1, h3, h2, h4 })
|
||||
let houses = new[] { h1, h2, h3, h4, h5 }
|
||||
where houses.IsLeftOf(Colour.Green, Colour.White) //r5
|
||||
select houses;
|
||||
_solved = members.First();
|
||||
}
|
||||
|
||||
public static new String ToString()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
sb.AppendLine("House Colour Drink Nationality Smokes Pet");
|
||||
sb.AppendLine("───── ────── ──────── ─────────── ────────── ─────");
|
||||
for (var i = 0; i < 5; i++)
|
||||
sb.AppendLine($"{i + 1,5} {_solved[i].ToString()}");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static void Main(string[] arguments)
|
||||
{
|
||||
var owner = _solved.Where(h=>h.P==Pet.Zebra).Single().N;
|
||||
WriteLine($"The zebra owner is {owner}");
|
||||
Write(ToString());
|
||||
Read();
|
||||
}
|
||||
}
|
||||
}
|
||||
86
Task/Zebra-puzzle/C-sharp/zebra-puzzle-3.cs
Normal file
86
Task/Zebra-puzzle/C-sharp/zebra-puzzle-3.cs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
using Amb;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using static System.Console;
|
||||
|
||||
static class ZebraProgram
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
var amb = new Amb.Amb();
|
||||
|
||||
var domain = new[] { 1, 2, 3, 4, 5 };
|
||||
var terms = new Dictionary<IValue<int>, string>();
|
||||
IValue<int> Term(string name)
|
||||
{
|
||||
var x = amb.Choose(domain);
|
||||
terms.Add(x, name);
|
||||
return x;
|
||||
};
|
||||
|
||||
void IsUnequal(params IValue<int>[] values) =>amb.Require(() => values.Select(v => v.Value).Distinct().Count() == 5);
|
||||
void IsSame(IValue<int> left, IValue<int> right) => amb.Require(() => left.Value == right.Value);
|
||||
void IsLeftOf(IValue<int> left, IValue<int> right) => amb.Require(() => right.Value - left.Value == 1);
|
||||
void IsIn(IValue<int> attrib, int house) => amb.Require(() => attrib.Value == house);
|
||||
void IsNextTo(IValue<int> left, IValue<int> right) => amb.Require(() => Math.Abs(left.Value - right.Value) == 1);
|
||||
|
||||
IValue<int> english = Term("Englishman"), swede = Term("Swede"), dane = Term("Dane"), norwegian = Term("Norwegian"), german = Term("German");
|
||||
IsIn(norwegian, 1);
|
||||
IsUnequal(english, swede, german, dane, norwegian);
|
||||
|
||||
IValue<int> red = Term("red"), green = Term("green"), white = Term("white"), blue = Term("blue"), yellow = Term("yellow");
|
||||
IsUnequal(red, green, white, blue, yellow);
|
||||
IsNextTo(norwegian, blue);
|
||||
IsLeftOf(green, white);
|
||||
IsSame(english, red);
|
||||
|
||||
IValue<int> tea = Term("tea"), coffee = Term("coffee"), milk = Term("milk"), beer = Term("beer"), water = Term("water");
|
||||
IsIn(milk, 3);
|
||||
IsUnequal(tea, coffee, milk, beer, water);
|
||||
IsSame(dane, tea);
|
||||
IsSame(green, coffee);
|
||||
|
||||
IValue<int> dog = Term("dog"), birds = Term("birds"), cats = Term("cats"), horse = Term("horse"), zebra = Term("zebra");
|
||||
IsUnequal(dog, cats, birds, horse, zebra);
|
||||
IsSame(swede, dog);
|
||||
|
||||
IValue<int> pallmall = Term("pallmall"), dunhill = Term("dunhill"), blend = Term("blend"), bluemaster = Term("bluemaster"),prince = Term("prince");
|
||||
IsUnequal(pallmall, dunhill, bluemaster, prince, blend);
|
||||
IsSame(pallmall, birds);
|
||||
IsSame(dunhill, yellow);
|
||||
IsNextTo(blend, cats);
|
||||
IsNextTo(horse, dunhill);
|
||||
IsSame(bluemaster, beer);
|
||||
IsSame(german, prince);
|
||||
IsNextTo(water, blend);
|
||||
|
||||
if (!amb.Disambiguate())
|
||||
{
|
||||
WriteLine("No solution found.");
|
||||
Read();
|
||||
return;
|
||||
}
|
||||
|
||||
var h = new List<string>[5];
|
||||
for (int i = 0; i < 5; i++)
|
||||
h[i] = new List<string>();
|
||||
|
||||
foreach (var (key, value) in terms.Select(kvp => (kvp.Key, kvp.Value)))
|
||||
{
|
||||
h[key.Value - 1].Add(value);
|
||||
}
|
||||
|
||||
var owner = String.Concat(h.Where(l => l.Contains("zebra")).Select(l => l[0]));
|
||||
WriteLine($"The {owner} owns the zebra");
|
||||
|
||||
foreach (var house in h)
|
||||
{
|
||||
Write("|");
|
||||
foreach (var attrib in house)
|
||||
Write($"{attrib,-10}|");
|
||||
Write("\n");
|
||||
}
|
||||
Read();
|
||||
}
|
||||
}
|
||||
113
Task/Zebra-puzzle/C-sharp/zebra-puzzle-4.cs
Normal file
113
Task/Zebra-puzzle/C-sharp/zebra-puzzle-4.cs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.SolverFoundation.Solvers;
|
||||
|
||||
using static System.Console;
|
||||
|
||||
static class ZebraProgram
|
||||
{
|
||||
static ConstraintSystem _solver;
|
||||
|
||||
static CspTerm IsLeftOf(this CspTerm left, CspTerm right) => _solver.Equal(1, right - left);
|
||||
static CspTerm IsInSameHouseAs(this CspTerm left, CspTerm right) => _solver.Equal(left, right);
|
||||
static CspTerm IsNextTo(this CspTerm left, CspTerm right) => _solver.Equal(1,_solver.Abs(left-right));
|
||||
static CspTerm IsInHouse(this CspTerm @this, int i) => _solver.Equal(i, @this);
|
||||
|
||||
static (ConstraintSystem, Dictionary<CspTerm, string>) BuildSolver()
|
||||
{
|
||||
var solver = ConstraintSystem.CreateSolver();
|
||||
_solver = solver;
|
||||
var terms = new Dictionary<CspTerm, string>();
|
||||
|
||||
CspTerm Term(string name)
|
||||
{
|
||||
CspTerm x = solver.CreateVariable(solver.CreateIntegerInterval(1, 5), name);
|
||||
terms.Add(x, name);
|
||||
return x;
|
||||
};
|
||||
|
||||
CspTerm red = Term("red"), green = Term("green"), white = Term("white"), blue = Term("blue"), yellow = Term("yellow");
|
||||
CspTerm tea = Term("tea"), coffee = Term("coffee"), milk = Term("milk"), beer = Term("beer"), water = Term("water");
|
||||
CspTerm english = Term("Englishman"), swede = Term("Swede"), dane = Term("Dane"), norwegian = Term("Norwegian"),
|
||||
german = Term("German");
|
||||
CspTerm dog = Term("dog"), birds = Term("birds"), cats = Term("cats"), horse = Term("horse"), zebra = Term("zebra");
|
||||
CspTerm pallmall = Term("pallmall"), dunhill = Term("dunhill"), blend = Term("blend"), bluemaster = Term("bluemaster"),
|
||||
prince = Term("prince");
|
||||
|
||||
solver.AddConstraints(
|
||||
solver.Unequal(english, swede, german, dane, norwegian),
|
||||
solver.Unequal(red, green, white, blue, yellow),
|
||||
solver.Unequal(dog, cats, birds, horse, zebra),
|
||||
solver.Unequal(pallmall, dunhill, bluemaster, prince, blend),
|
||||
solver.Unequal(tea, coffee, milk, beer, water),
|
||||
|
||||
english.IsInSameHouseAs(red), //r2
|
||||
swede.IsInSameHouseAs(dog), //r3
|
||||
dane.IsInSameHouseAs(tea), //r4
|
||||
green.IsLeftOf(white), //r5
|
||||
green.IsInSameHouseAs(coffee), //r6
|
||||
pallmall.IsInSameHouseAs(birds), //r7
|
||||
dunhill.IsInSameHouseAs(yellow), //r8
|
||||
milk.IsInHouse(3), //r9
|
||||
norwegian.IsInHouse(1), //r10
|
||||
blend.IsNextTo(cats), //r11
|
||||
horse.IsNextTo(dunhill),// r12
|
||||
bluemaster.IsInSameHouseAs(beer), // r13
|
||||
german.IsInSameHouseAs(prince), // r14
|
||||
norwegian.IsNextTo(blue), //r15
|
||||
water.IsNextTo(blend) //r16
|
||||
);
|
||||
return (solver, terms);
|
||||
}
|
||||
|
||||
static List<string>[] TermsToString(ConstraintSolverSolution solved, Dictionary<CspTerm, string> terms)
|
||||
{
|
||||
var h = new List<string>[5];
|
||||
for (int i = 0; i < 5; i++)
|
||||
h[i] = new List<string>();
|
||||
|
||||
foreach (var (key, value) in terms.Select(kvp => (kvp.Key, kvp.Value)))
|
||||
{
|
||||
if (!solved.TryGetValue(key, out object house))
|
||||
throw new InvalidProgramException("Can't find a term - {value} - in the solution");
|
||||
h[(int)house - 1].Add(value);
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
static new string ToString(List<string>[] houses)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
foreach (var house in houses)
|
||||
{
|
||||
sb.Append("|");
|
||||
foreach (var attrib in house)
|
||||
sb.Append($"{attrib,-10}|");
|
||||
sb.Append("\n");
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
var (solver, terms) = BuildSolver();
|
||||
|
||||
var solved = solver.Solve();
|
||||
|
||||
if (solved.HasFoundSolution)
|
||||
{
|
||||
var h = TermsToString(solved, terms);
|
||||
|
||||
var owner = String.Concat(h.Where(l => l.Contains("zebra")).Select(l => l[2]));
|
||||
WriteLine($"The {owner} owns the zebra");
|
||||
WriteLine();
|
||||
Write(ToString(h));
|
||||
}
|
||||
else
|
||||
WriteLine("No solution found.");
|
||||
Read();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue