Advertisement
Guest User

santa

a guest
Feb 3rd, 2015
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.68 KB | None | 0 0
  1. import std.stdio;
  2. import std.range;
  3. import std.algorithm;
  4. import std.exception;
  5. import std.string;
  6.  
  7. import std.conv;
  8.  
  9. enum string[] names = ["Luke Skywalker",
  10.                         "Leia Skywalker",
  11.                         "Toula Portokalos",
  12.                         "Gus Portokalos",
  13.                         "Bruce Wayne",
  14.                         "Virgil Brigman",
  15.                         "Lindsey Brigman"];
  16.  
  17. class Person {
  18.   static int instances = 0;
  19.  
  20.   string name;
  21.   bool hasGiven;
  22.   bool hasReceived;
  23.  
  24.   override string toString() {
  25.      return "Person(%s,%s,%s)".format(name, hasGiven, hasReceived);
  26.   };
  27.  
  28.   this(string s ) {
  29.      name = s;
  30.   };
  31.  
  32.   bool isFamily( Person other) {
  33.      return familyName(name) == familyName(other.name);
  34.   };
  35. }
  36.  
  37. string familyName( string s ) {
  38.   return s.split(" ")[1];
  39. }
  40.    
  41. bool isFamily( string a , string b) {
  42.   return familyName(a) == familyName(b);
  43. }
  44.  
  45. class Gift {
  46.   Person giver;
  47.   Person recipient;
  48.  
  49.   this( Person g, Person r ) {
  50.      giver = g;
  51.      recipient = r;
  52.   };
  53.  
  54.   override string toString() {
  55.      return format("Gift(%s,%s)", giver, recipient);
  56.   }
  57. }
  58.  
  59. auto people = names.map!( x => new Person(x)).array;
  60.    
  61. void makeGift( ref Gift g) {
  62.   enforce( !g.giver.hasGiven, "Logic error");
  63.   enforce( !g.recipient.hasReceived, "Logic error");
  64.   g.giver.hasGiven        = true;
  65.   g.recipient.hasReceived = true;
  66. }
  67.  
  68. void takeBackGift( ref Gift g) {
  69.   enforce( g.giver.hasGiven, "Logic error");
  70.   enforce( g.recipient.hasReceived, "Logic error");
  71.   g.giver.hasGiven = false;
  72.   g.recipient.hasReceived = false;
  73. }
  74.  
  75. Gift[] emptyGift;
  76.  
  77. Gift[] possibleMoves(int movesMade) {
  78.   if (movesMade>=people.length) {
  79.      return emptyGift;
  80.   } else {
  81.      auto giver = people[movesMade];
  82.      enforce(!giver.hasGiven, "Logic error");
  83.      auto ret = people
  84.         .filter!( x => !x.hasReceived && !giver.isFamily(x) )
  85.         .map!( x => new Gift( giver, x))
  86.         .array;
  87.      return ret;
  88.   }
  89. }
  90.  
  91. bool isSolved() {
  92.   return people.all!"a.hasReceived && a.hasGiven";
  93. }
  94.  
  95. void main()
  96. {
  97.   Gift[][] movesRemaining;
  98.   Gift[]   movesMade;
  99.   movesRemaining ~= possibleMoves(0);
  100.   void dump() {
  101.      foreach( m  ; movesMade) {
  102.         writeln(m);
  103.      }
  104.   }
  105.   auto solutions = 0;
  106.   while( !movesRemaining.empty) {
  107.      if (movesRemaining[$-1].empty) {
  108.         movesRemaining.length -= 1;
  109.         if (movesMade.empty) continue;
  110.         takeBackGift( movesMade[$-1]);
  111.         movesMade.length      -= 1;
  112.      } else {
  113.         auto moveToMake = movesRemaining[$-1][$-1];
  114.         movesRemaining[$-1].length -= 1;
  115.         makeGift(moveToMake);
  116.         movesMade      ~= moveToMake;
  117.         movesRemaining ~= possibleMoves(to!int(movesMade.length));
  118.      }
  119.      if (isSolved()) {
  120.         writeln("\nSolved");
  121.         foreach( ref q ; movesMade) {
  122.           writeln(q);
  123.         }
  124.         solutions++;
  125.      }
  126.   }
  127.   writefln("Found %s solutions", solutions);
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement