Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,68 @@
|
|||
public class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
const int count = 5;
|
||||
const int Baker = 0, Cooper = 1, Fletcher = 2, Miller = 3, Smith = 4;
|
||||
string[] names = { nameof(Baker), nameof(Cooper), nameof(Fletcher), nameof(Miller), nameof(Smith) };
|
||||
|
||||
Func<int[], bool>[] constraints = {
|
||||
floorOf => floorOf[Baker] != count-1,
|
||||
floorOf => floorOf[Cooper] != 0,
|
||||
floorOf => floorOf[Fletcher] != count-1 && floorOf[Fletcher] != 0,
|
||||
floorOf => floorOf[Miller] > floorOf[Cooper],
|
||||
floorOf => Math.Abs(floorOf[Smith] - floorOf[Fletcher]) > 1,
|
||||
floorOf => Math.Abs(floorOf[Fletcher] - floorOf[Cooper]) > 1,
|
||||
};
|
||||
|
||||
var solver = new DinesmanSolver();
|
||||
foreach (var tenants in solver.Solve(count, constraints)) {
|
||||
Console.WriteLine(string.Join(" ", tenants.Select(t => names[t])));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class DinesmanSolver
|
||||
{
|
||||
public IEnumerable<int[]> Solve(int count, params Func<int[], bool>[] constraints) {
|
||||
foreach (int[] floorOf in Permutations(count)) {
|
||||
if (constraints.All(c => c(floorOf))) {
|
||||
yield return Enumerable.Range(0, count).OrderBy(i => floorOf[i]).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static IEnumerable<int[]> Permutations(int length) {
|
||||
if (length == 0) {
|
||||
yield return new int[0];
|
||||
yield break;
|
||||
}
|
||||
bool forwards = false;
|
||||
foreach (var permutation in Permutations(length - 1)) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
yield return permutation.InsertAt(forwards ? i : length - i - 1, length - 1).ToArray();
|
||||
}
|
||||
forwards = !forwards;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class Extensions
|
||||
{
|
||||
public static IEnumerable<T> InsertAt<T>(this IEnumerable<T> source, int position, T newElement) {
|
||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||
if (position < 0) throw new ArgumentOutOfRangeException(nameof(position));
|
||||
return InsertAtIterator(source, position, newElement);
|
||||
}
|
||||
|
||||
private static IEnumerable<T> InsertAtIterator<T>(IEnumerable<T> source, int position, T newElement) {
|
||||
int index = 0;
|
||||
foreach (T element in source) {
|
||||
if (index == position) yield return newElement;
|
||||
yield return element;
|
||||
index++;
|
||||
}
|
||||
if (index < position) throw new ArgumentOutOfRangeException(nameof(position));
|
||||
if (index == position) yield return newElement;
|
||||
}
|
||||
}
|
||||
|
|
@ -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; }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue