Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
85
Task/Set-puzzle/C-sharp/set-puzzle.cs
Normal file
85
Task/Set-puzzle/C-sharp/set-puzzle.cs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using static System.Linq.Enumerable;
|
||||
|
||||
public static class SetPuzzle
|
||||
{
|
||||
static readonly Feature[] numbers = { (1, "One"), (2, "Two"), (3, "Three") };
|
||||
static readonly Feature[] colors = { (1, "Red"), (2, "Green"), (3, "Purple") };
|
||||
static readonly Feature[] shadings = { (1, "Open"), (2, "Striped"), (3, "Solid") };
|
||||
static readonly Feature[] symbols = { (1, "Oval"), (2, "Squiggle"), (3, "Diamond") };
|
||||
|
||||
private readonly struct Feature
|
||||
{
|
||||
public Feature(int value, string name) => (Value, Name) = (value, name);
|
||||
public int Value { get; }
|
||||
public string Name { get; }
|
||||
public static implicit operator int(Feature f) => f.Value;
|
||||
public static implicit operator Feature((int value, string name) t) => new Feature(t.value, t.name);
|
||||
public override string ToString() => Name;
|
||||
}
|
||||
|
||||
private readonly struct Card : IEquatable<Card>
|
||||
{
|
||||
public Card(Feature number, Feature color, Feature shading, Feature symbol) =>
|
||||
(Number, Color, Shading, Symbol) = (number, color, shading, symbol);
|
||||
|
||||
public Feature Number { get; }
|
||||
public Feature Color { get; }
|
||||
public Feature Shading { get; }
|
||||
public Feature Symbol { get; }
|
||||
|
||||
public override string ToString() => $"{Number} {Color} {Shading} {Symbol}(s)";
|
||||
public bool Equals(Card other) => Number == other.Number && Color == other.Color && Shading == other.Shading && Symbol == other.Symbol;
|
||||
}
|
||||
|
||||
public static void Main() {
|
||||
Card[] deck = (
|
||||
from number in numbers
|
||||
from color in colors
|
||||
from shading in shadings
|
||||
from symbol in symbols
|
||||
select new Card(number, color, shading, symbol)
|
||||
).ToArray();
|
||||
var random = new Random();
|
||||
|
||||
Deal(deck, 9, 4, random);
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Deal(deck, 12, 6, random);
|
||||
}
|
||||
|
||||
static void Deal(Card[] deck, int size, int target, Random random) {
|
||||
List<(Card a, Card b, Card c)> sets;
|
||||
do {
|
||||
Shuffle(deck, random.Next);
|
||||
sets = (
|
||||
from i in 0.To(size - 2)
|
||||
from j in (i + 1).To(size - 1)
|
||||
from k in (j + 1).To(size)
|
||||
select (deck[i], deck[j], deck[k])
|
||||
).Where(IsSet).ToList();
|
||||
} while (sets.Count != target);
|
||||
Console.WriteLine("The board:");
|
||||
foreach (Card card in deck.Take(size)) Console.WriteLine(card);
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Sets:");
|
||||
foreach (var s in sets) Console.WriteLine(s);
|
||||
}
|
||||
|
||||
static void Shuffle<T>(T[] array, Func<int, int, int> rng) {
|
||||
for (int i = 0; i < array.Length; i++) {
|
||||
int r = rng(i, array.Length);
|
||||
(array[r], array[i]) = (array[i], array[r]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool IsSet((Card a, Card b, Card c) t) =>
|
||||
AreSameOrDifferent(t.a.Number, t.b.Number, t.c.Number) &&
|
||||
AreSameOrDifferent(t.a.Color, t.b.Color, t.c.Color) &&
|
||||
AreSameOrDifferent(t.a.Shading, t.b.Shading, t.c.Shading) &&
|
||||
AreSameOrDifferent(t.a.Symbol, t.b.Symbol, t.c.Symbol);
|
||||
|
||||
static bool AreSameOrDifferent(int a, int b, int c) => (a + b + c) % 3 == 0;
|
||||
static IEnumerable<int> To(this int start, int end) => Range(start, end - start - 1);
|
||||
}
|
||||
42
Task/Set-puzzle/Factor/set-puzzle.factor
Normal file
42
Task/Set-puzzle/Factor/set-puzzle.factor
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
USING: arrays backtrack combinators.short-circuit formatting
|
||||
fry grouping io kernel literals math.combinatorics math.matrices
|
||||
prettyprint qw random sequences sets ;
|
||||
IN: rosetta-code.set-puzzle
|
||||
|
||||
CONSTANT: deck $[
|
||||
[
|
||||
qw{ red green purple } amb-lazy
|
||||
qw{ one two three } amb-lazy
|
||||
qw{ oval squiggle diamond } amb-lazy
|
||||
qw{ solid open striped } amb-lazy 4array
|
||||
] bag-of
|
||||
]
|
||||
|
||||
: valid-category? ( seq -- ? )
|
||||
{ [ all-equal? ] [ all-unique? ] } 1|| ;
|
||||
|
||||
: valid-set? ( seq -- ? )
|
||||
[ valid-category? ] column-map t [ and ] reduce ;
|
||||
|
||||
: find-sets ( seq -- seq )
|
||||
3 <combinations> [ valid-set? ] filter ;
|
||||
|
||||
: deal-hand ( m n -- seq valid? )
|
||||
[ deck swap sample ] dip over find-sets length = ;
|
||||
|
||||
: find-valid-hand ( m n -- seq )
|
||||
[ f ] 2dip '[ drop _ _ deal-hand not ] loop ;
|
||||
|
||||
: set-puzzle ( m n -- )
|
||||
[ find-valid-hand ] 2keep
|
||||
[ "Dealt %d cards:\n" printf simple-table. nl ]
|
||||
[
|
||||
"Containing %d sets:\n" printf find-sets
|
||||
{ { " " " " " " " " } } join simple-table. nl
|
||||
] bi-curry* bi ;
|
||||
|
||||
: main ( -- )
|
||||
9 4 set-puzzle
|
||||
12 6 set-puzzle ;
|
||||
|
||||
MAIN: main
|
||||
45
Task/Set-puzzle/Prolog/set-puzzle.pro
Normal file
45
Task/Set-puzzle/Prolog/set-puzzle.pro
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
do_it(N) :-
|
||||
card_sets(N, Cards, Sets),
|
||||
!,
|
||||
format('Cards: ~n'),
|
||||
maplist(print_card, Cards),
|
||||
format('~nSets: ~n'),
|
||||
maplist(print_set, Sets).
|
||||
|
||||
print_card(Card) :- format(' ~p ~p ~p ~p~n', Card).
|
||||
print_set(Set) :- maplist(print_card, Set), nl.
|
||||
|
||||
n(9,4).
|
||||
n(12,6).
|
||||
|
||||
card_sets(N, Cards, Sets) :-
|
||||
n(N,L),
|
||||
repeat,
|
||||
random_deal(N, Cards),
|
||||
setof(Set, is_card_set(Cards, Set), Sets),
|
||||
length(Sets, L).
|
||||
|
||||
random_card([C,S,N,Sh]) :-
|
||||
random_member(C, [red, green, purple]),
|
||||
random_member(S, [oval, squiggle, diamond]),
|
||||
random_member(N, [one, two, three]),
|
||||
random_member(Sh, [solid, open, striped]).
|
||||
|
||||
random_deal(N, Cards) :-
|
||||
length(Cards, N),
|
||||
maplist(random_card, Cards).
|
||||
|
||||
is_card_set(Cards, Result) :-
|
||||
select(C1, Cards, Rest),
|
||||
select(C2, Rest, Rest2),
|
||||
select(C3, Rest2, _),
|
||||
|
||||
match(C1, C2, C3),
|
||||
sort([C1,C2,C3], Result).
|
||||
|
||||
match([],[],[]).
|
||||
match([A|T1],[A|T2],[A|T3]) :-
|
||||
match(T1,T2,T3).
|
||||
match([A|T1],[B|T2],[C|T3]) :-
|
||||
dif(A,B), dif(B,C), dif(A,C),
|
||||
match(T1,T2,T3).
|
||||
Loading…
Add table
Add a link
Reference in a new issue