fork download
  1. use std::fmt;
  2.  
  3.  
  4. #[derive( PartialEq,Eq,Debug,Clone)]
  5. enum Couleur{
  6. Rouge,
  7. Verte,
  8. Blanche,
  9. Jaune,
  10. Bleue
  11. }
  12. impl Couleur{
  13. fn inc(self) -> Couleur{
  14. match self{
  15.  
  16. Couleur::Rouge =>{let r=Couleur::Verte;
  17. r},
  18. Couleur::Verte =>{let r=Couleur::Blanche;
  19. r},
  20. Couleur::Blanche =>{let r=Couleur::Jaune;
  21. r},
  22. Couleur::Jaune =>{let r=Couleur::Bleue;
  23. r},
  24. Couleur::Bleue =>Couleur::Rouge
  25. }
  26. }
  27. }
  28.  
  29. #[derive( PartialEq,Eq,Debug,Clone)]
  30. enum Nationalite{
  31. Anglais,
  32. Suedois,
  33. Danois,
  34. Norvegien,
  35. Allemand
  36. }
  37. impl Nationalite{
  38. fn inc(self) -> Nationalite{
  39. match self{
  40.  
  41. Nationalite::Anglais =>{let r=Nationalite::Suedois;
  42. r},
  43. Nationalite::Suedois =>{let r=Nationalite::Danois;
  44. r},
  45. Nationalite::Danois =>{let r=Nationalite::Norvegien;
  46. r},
  47. Nationalite::Norvegien =>{let r=Nationalite::Allemand;
  48. r},
  49. Nationalite::Allemand =>Nationalite::Anglais
  50. }
  51. }
  52. }
  53.  
  54. #[derive( PartialEq,Eq,Debug,Clone)]
  55. enum Boisson{
  56. The,
  57. Cafe,
  58. Lait,
  59. Biere,
  60. Eau
  61. }
  62.  
  63. impl Boisson{
  64. fn inc(self) -> Boisson{
  65. match self{
  66.  
  67. Boisson::The =>{let r=Boisson::Cafe;
  68. r},
  69. Boisson::Cafe =>{let r=Boisson::Lait;
  70. r},
  71. Boisson::Lait =>{let r=Boisson::Biere;
  72. r},
  73. Boisson::Biere =>{let r=Boisson::Eau;
  74. r},
  75. Boisson::Eau =>Boisson::The
  76. }
  77. }
  78. }
  79.  
  80. #[derive( PartialEq,Eq,Debug,Clone)]
  81. enum Cigarette{
  82. PallMall,
  83. Dunhills,
  84. Blends,
  85. BleueMasters,
  86. Prince,
  87. }
  88. impl Cigarette{
  89. fn inc(self) -> Cigarette{
  90. match self{
  91.  
  92. Cigarette::PallMall =>{let r=Cigarette::Dunhills;
  93. r},
  94. Cigarette::Dunhills =>{let r=Cigarette::Blends;
  95. r},
  96. Cigarette::Blends =>{let r=Cigarette::BleueMasters;
  97. r},
  98. Cigarette::BleueMasters =>{let r=Cigarette::Prince;
  99. r},
  100. Cigarette::Prince =>Cigarette::PallMall
  101. }
  102. }
  103. }
  104.  
  105. #[derive( PartialEq,Eq,Debug,Clone)]
  106. enum Animaux{
  107. Chien,
  108. Oiseaux,
  109. Chats,
  110. Chevaux,
  111. PoissonRouge
  112. }
  113. impl Animaux{
  114. fn inc(self) -> Animaux{
  115. match self{
  116.  
  117. Animaux::Chien =>{let r=Animaux::Oiseaux;
  118. r},
  119. Animaux::Oiseaux =>{let r=Animaux::Chats;
  120. r},
  121. Animaux::Chats =>{let r=Animaux::Chevaux;
  122. r},
  123. Animaux::Chevaux =>{let r=Animaux::PoissonRouge;
  124. r},
  125. Animaux::PoissonRouge =>Animaux::Chien
  126. }
  127. }
  128. }
  129.  
  130. #[derive(Debug,Clone)]
  131. struct Maison{
  132. couleur : Couleur,
  133. nationalite : Nationalite,
  134. boisson : Boisson,
  135. cigarette : Cigarette,
  136. animaux : Animaux
  137. }
  138.  
  139.  
  140.  
  141.  
  142. impl Maison{
  143.  
  144.  
  145. fn inc(self) -> Maison{
  146.  
  147.  
  148. match self{
  149.  
  150.  
  151. Maison {couleur : Couleur::Bleue,
  152. nationalite : Nationalite::Allemand,
  153. boisson : Boisson::Eau,
  154. cigarette : Cigarette::Prince,
  155. animaux : Animaux::PoissonRouge
  156. } =>
  157. {
  158. Maison{couleur : Couleur::Bleue.inc(), nationalite : Nationalite::Allemand.inc(), boisson : Boisson::Eau.inc(), cigarette : Cigarette::Prince.inc(), animaux : Animaux::PoissonRouge.inc()}
  159. },
  160.  
  161. Maison {couleur : Couleur::Bleue,
  162. nationalite : Nationalite::Allemand,
  163. boisson : Boisson::Eau,
  164. cigarette : Cigarette::Prince,
  165. animaux : e
  166. } =>
  167. {
  168. Maison{couleur : Couleur::Bleue.inc(), nationalite : Nationalite::Allemand.inc(), boisson : Boisson::Eau.inc(), cigarette : Cigarette::Prince.inc(), animaux : e.inc()}
  169. },
  170.  
  171.  
  172. Maison {couleur : Couleur::Bleue,
  173. nationalite : Nationalite::Allemand,
  174. boisson : Boisson::Eau,
  175. cigarette : d,
  176. animaux : e
  177. } =>
  178. {
  179. Maison{couleur : Couleur::Bleue.inc(), nationalite : Nationalite::Allemand.inc(), boisson : Boisson::Eau.inc(), cigarette : d.inc(), animaux : e}
  180. },
  181.  
  182. Maison {couleur : Couleur::Bleue,
  183. nationalite : Nationalite::Allemand,
  184. boisson : c,
  185. cigarette : d,
  186. animaux : e
  187. } =>
  188. {
  189. Maison{couleur : Couleur::Bleue.inc(), nationalite : Nationalite::Allemand.inc(), boisson : c.inc(), cigarette : d, animaux : e}
  190. },
  191.  
  192. Maison {couleur : Couleur::Bleue,
  193. nationalite : b,
  194. boisson : c,
  195. cigarette : d,
  196. animaux : e
  197. } =>
  198. {
  199. Maison{couleur : Couleur::Bleue.inc(), nationalite : b.inc(), boisson : c, cigarette : d, animaux : e}
  200. },
  201.  
  202.  
  203. Maison {couleur : a,
  204. nationalite : b,
  205. boisson : c,
  206. cigarette : d,
  207. animaux : e
  208. } =>
  209. {
  210. Maison{couleur : a.inc(), nationalite : b, boisson : c, cigarette : d, animaux : e}
  211. }
  212.  
  213.  
  214. }
  215.  
  216.  
  217. } // fn inc maison
  218.  
  219. } // impl Maison
  220.  
  221. fn origin() -> Maison{
  222. let m= Maison { couleur : Couleur::Rouge, nationalite : Nationalite::Anglais, boisson : Boisson::The, cigarette : Cigarette::PallMall, animaux : Animaux::Chien };
  223. m
  224. }
  225.  
  226. /*impl Iterator for Maison {
  227. type Item = Maison;
  228.   fn next(&mut self) -> Option<Maison> {
  229.   let n=(*self).inc();
  230.   Some(n)
  231.   }
  232. }*/
  233.  
  234. #[derive(Debug)]
  235. struct HypotheseVector(Vec<Maison>,Vec<Maison>,Vec<Maison>,Vec<Maison>,Vec<Maison>);
  236.  
  237.  
  238. fn full_possibility() -> HypotheseVector{
  239. let mut r=HypotheseVector(Vec::new(),Vec::new(),Vec::new(),Vec::new(),Vec::new());
  240.  
  241. let mut o=origin() ;
  242. let nb_boucle=5*5*5*5*5 ;
  243. for _ in 0..nb_boucle{
  244. //println!("> {:?}", o);
  245. match r{
  246. HypotheseVector(ref mut a,ref mut b,ref mut c,ref mut d,ref mut e) => {
  247. a.push(o.clone());
  248. b.push(o.clone());
  249. c.push(o.clone());
  250. d.push(o.clone());
  251. e.push(o.clone());
  252.  
  253. }
  254.  
  255. }
  256. o=o.inc();
  257.  
  258. }
  259.  
  260. r
  261. }
  262.  
  263. //1. L'Anglais vit dans la maison rouge.
  264. fn r1_anglais_rouge<'r> (input : &'r Maison ) -> bool {
  265. match *input{
  266. Maison { couleur : Couleur::Rouge, nationalite : Nationalite::Anglais, boisson : _, cigarette : _, animaux : _} => true,
  267. Maison { couleur : _, nationalite : Nationalite::Anglais, boisson : _, cigarette : _, animaux : _} => false,
  268. Maison { couleur : Couleur::Rouge, nationalite : _, boisson : _, cigarette : _, animaux : _} => false,
  269. _ => true
  270.  
  271. }
  272. }
  273.  
  274. //2. Le Suédois élève des chiens.
  275. fn r2_suedois_chien<'r> (input : &'r Maison ) -> bool {
  276. match *input{
  277. Maison { couleur : _, nationalite : Nationalite::Suedois, boisson : _, cigarette : _, animaux : Animaux::Chien} => true,
  278. Maison { couleur : _, nationalite : Nationalite::Suedois, boisson : _, cigarette : _, animaux :_} => false,
  279. Maison { couleur : _, nationalite : _, boisson : _, cigarette : _, animaux : Animaux::Chien} => false,
  280. _ => true
  281.  
  282. }
  283. }
  284.  
  285. //3. Le Danois boit du thé.
  286. fn r3_danois_the<'r> (input : &'r Maison ) -> bool {
  287. match *input{
  288. Maison { couleur : _, nationalite : Nationalite::Danois, boisson : Boisson::The, cigarette : _, animaux : _} => true,
  289. Maison { couleur : _, nationalite : Nationalite::Danois, boisson : _, cigarette : _, animaux : _} => false,
  290. Maison { couleur : _, nationalite : _, boisson : Boisson::The, cigarette : _, animaux : _} => false,
  291. _ => true
  292.  
  293. }
  294. }
  295. //5. Le propriétaire de la maison verte boit du café.
  296. fn r5_verte_cafe<'r> (input : &'r Maison ) -> bool {
  297. match *input{
  298. Maison { couleur : Couleur::Verte, nationalite : _, boisson : Boisson::Cafe, cigarette : _, animaux : _} => true,
  299. Maison { couleur : _, nationalite : _, boisson : Boisson::Cafe, cigarette : _, animaux : _} => false,
  300. Maison { couleur : Couleur::Verte, nationalite : _, boisson : _, cigarette : _, animaux : _}=> false,
  301. _ => true
  302.  
  303. }
  304. }
  305.  
  306. //6. Le fumeur de Pall Mall élève des oiseaux.
  307. fn r6_pallmall_oiseau<'r> (input : &'r Maison ) -> bool {
  308. match *input{
  309. Maison { couleur : _, nationalite : _, boisson : _, cigarette : Cigarette::PallMall, animaux : Animaux::Oiseaux} => true,
  310. Maison { couleur : _, nationalite : _, boisson : _, cigarette : _, animaux : Animaux::Oiseaux} => false,
  311. Maison { couleur : _, nationalite : _, boisson : _, cigarette : Cigarette::PallMall, animaux : _}=> false,
  312. _ => true
  313.  
  314. }
  315. }
  316.  
  317. //7. Le propriétaire de la maison jaune fume des Dunhills.
  318. fn r7_jaune_dunhills<'r> (input : &'r Maison ) -> bool {
  319. match *input{
  320. Maison { couleur : Couleur::Jaune, nationalite : _, boisson : _, cigarette : Cigarette::Dunhills, animaux : _} => true,
  321. Maison { couleur : Couleur::Jaune, nationalite : _, boisson : _, cigarette : _, animaux : _} => false,
  322. Maison { couleur : _, nationalite : _, boisson : _, cigarette : Cigarette::Dunhills, animaux : _}=> false,
  323. _ => true
  324.  
  325. }
  326. }
  327.  
  328. //12. L'homme qui fume des Blue Masters boit de la bière.
  329. fn r12_bluemaster_biere<'r> (input : &'r Maison ) -> bool {
  330. match *input{
  331. Maison { couleur :_ , nationalite : _ , boisson : Boisson::Biere , cigarette : Cigarette::BleueMasters , animaux : _} => true,
  332. Maison { couleur :_ , nationalite : _ , boisson : Boisson::Biere , cigarette : _ , animaux : _} => false,
  333. Maison { couleur :_ , nationalite : _ , boisson : _ , cigarette : Cigarette::BleueMasters , animaux : _} => false,
  334. _ => true
  335.  
  336. }
  337. }
  338.  
  339. //13. L'Allemand fume des Prince.
  340. fn r13_allemand_prince<'r> (input : &'r Maison ) -> bool {
  341. match *input{
  342. Maison { couleur :_ , nationalite : Nationalite::Allemand , boisson : _ , cigarette : Cigarette::Prince , animaux : _} => true,
  343. Maison { couleur :_ , nationalite : Nationalite::Allemand , boisson : _ , cigarette : _ , animaux : _} => false,
  344. Maison { couleur :_ , nationalite : _ , boisson : _ , cigarette : Cigarette::Prince , animaux : _} => false,
  345. _ => true
  346.  
  347. }
  348. }
  349.  
  350. //1. L'Anglais vit dans la maison rouge.
  351. //2. Le Suédois élève des chiens.
  352. //3. Le Danois boit du thé.
  353. //5. Le propriétaire de la maison verte boit du café.
  354. //6. Le fumeur de Pall Mall élève des oiseaux.
  355. //7. Le propriétaire de la maison jaune fume des Dunhills.
  356. //12. L'homme qui fume des Blue Masters boit de la bière.
  357. //13. L'Allemand fume des Prince.
  358.  
  359. //4. La maison verte est juste à gauche de la maison blanche.
  360. //8. L'homme qui vit dans la maison du centre boit du lait.
  361. //9. Le Norvégien vit dans la première maison.
  362. //10. L'homme qui fume des Blends vit à côté de celui qui élève des chats.
  363. //11. L'homme qui élève des chevaux vit à côté du fumeur de Dunhills.
  364. //14. Le Norvégien vit à côté de la maison bleue.
  365. //15. L'homme qui fume des Blends a un voisin qui boit de l'eau.
  366.  
  367.  
  368.  
  369. fn has_a_norvegien<'r> (input : &'r Maison ) -> bool {
  370. match *input{
  371. Maison { couleur :_ , nationalite : Nationalite::Norvegien , boisson : _ , cigarette : _ , animaux : _ } => true,
  372. _ => false
  373.  
  374. }
  375. }
  376. fn has_lait<'r> (input : &'r Maison ) -> bool {
  377. match *input{
  378. Maison { couleur :_ , nationalite : _ , boisson : Boisson::Lait , cigarette : _ , animaux : _ } => true,
  379. _ => false
  380.  
  381. }
  382. }
  383. fn has_bleue<'r> (input : &'r Maison ) -> bool {
  384. match *input{
  385. Maison { couleur :Couleur::Bleue , nationalite : _ , boisson : _ , cigarette : _ , animaux : _ } => true,
  386. _ => false
  387.  
  388. }
  389. }
  390.  
  391. fn has_couleur<'r> (input : &'r Maison, coul : Couleur ) -> bool {
  392. match *input{
  393. Maison { couleur : ref x , nationalite : _ , boisson : _ , cigarette : _ , animaux : _ } => coul==*x
  394.  
  395. }
  396. }
  397. fn has_nationalite<'r> (input : &'r Maison, coul : Nationalite ) -> bool {
  398. match *input{
  399. Maison { couleur : _ , nationalite : ref x , boisson : _ , cigarette : _ , animaux : _ } => coul==*x
  400.  
  401. }
  402. }
  403. fn has_boisson<'r> (input : &'r Maison, coul : Boisson ) -> bool {
  404. match *input{
  405. Maison { couleur : _ , nationalite : _ , boisson : ref x , cigarette : _ , animaux : _ } => coul==*x
  406.  
  407. }
  408. }
  409. fn has_cigarette<'r> (input : &'r Maison, coul : Cigarette ) -> bool {
  410. match *input{
  411. Maison { couleur : _ , nationalite : _ , boisson : _ , cigarette : ref x , animaux : _ } => coul==*x
  412.  
  413. }
  414. }
  415. fn has_animaux<'r> (input : &'r Maison, coul : Animaux ) -> bool {
  416. match *input{
  417. Maison { couleur : _ , nationalite : _ , boisson : _ , cigarette : _ , animaux : ref x } => coul==*x
  418.  
  419. }
  420. }
  421.  
  422. fn vec_has<F>(v : &Vec<Maison>,rule : F) -> bool
  423. where F: Fn(&Maison) -> bool{
  424.  
  425. for f in v.iter() {
  426. if rule(&f) {return true}
  427. }
  428.  
  429. false
  430.  
  431. }
  432.  
  433.  
  434. fn vec_string(v : &Vec<Maison>)-> String{
  435. let mut r=String::new();
  436.  
  437. for f in v.iter() {
  438. let ot=format!("{:?}\n",f);
  439. r.push_str(&ot);
  440. }
  441. r
  442.  
  443. }
  444.  
  445. impl fmt::Display for HypotheseVector {
  446. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  447. match *self{
  448. HypotheseVector(ref a,ref b,ref c,ref d,ref e) => {
  449. write!(f,"\n++++++Maison 1++++++\n {}\n++++++Maison 2++++++\n {}\n++++++Maison 3++++++\n {}\n++++++Maison 4++++++\n {}\n++++++Maison 5++++++\n {}",vec_string(a),vec_string (b),vec_string (c),vec_string (d), vec_string (e))
  450.  
  451. }
  452.  
  453. }
  454. }
  455. }
  456.  
  457.  
  458. impl HypotheseVector{
  459.  
  460. fn count_determinant ( &mut self ) -> usize{
  461. match *self{
  462. HypotheseVector(ref mut a,ref mut b,ref mut c,ref mut d,ref mut e) => {
  463. a.len()+b.len()+c.len()+d.len()+e.len()
  464.  
  465. }
  466.  
  467. }
  468.  
  469.  
  470. }
  471.  
  472. fn constraint_voisin_keep_unique_as_single<F>(&mut self,rule1 : F)
  473. where F: Fn(&Maison) -> bool{
  474.  
  475. match *self{
  476. HypotheseVector(ref mut a,ref mut b,ref mut c,ref mut d,ref mut e) => {
  477. let ba=vec_has(&a,|x: &Maison| rule1(x));
  478. let bb=vec_has(&b,|x: &Maison| rule1(x));
  479. let bc=vec_has(&c,|x: &Maison| rule1(x));
  480. let bd=vec_has(&d,|x: &Maison| rule1(x));
  481. let be=vec_has(&e,|x: &Maison| rule1(x));
  482.  
  483.  
  484. let the_one= match (ba,bb,bc,bd,be){
  485. (true,false,false,false,false) => Some(a),
  486. (false,true,false,false,false) => Some(b),
  487. (false,false,true,false,false) => Some(c),
  488. (false,false,false,true,false) => Some(d),
  489. (false,false,false,false,true) => Some(e),
  490. _ => None
  491. };
  492.  
  493. match the_one{
  494. Some(k)=>{
  495. k.retain(|x: &Maison| rule1(x));}
  496. None=> {}
  497. }
  498. }
  499.  
  500. }
  501.  
  502. }
  503.  
  504. fn constraint_voisin_keep_not_in_or_voisin_in<F,G>(&mut self,rule1 : F,rule2 : G)
  505. where F: Fn(&Maison) -> bool, G: Fn(&Maison) -> bool{
  506.  
  507. match *self{
  508. HypotheseVector(ref mut a,ref mut b,ref mut c,ref mut d,ref mut e) => {
  509. let ba=vec_has(&a,|x: &Maison| rule1(x));
  510. let bb=vec_has(&b,|x: &Maison| rule1(x));
  511. let bc=vec_has(&c,|x: &Maison| rule1(x));
  512. let bd=vec_has(&d,|x: &Maison| rule1(x));
  513. let be=vec_has(&e,|x: &Maison| rule1(x));
  514.  
  515. a.retain(|x: &Maison| !rule2(x) || bb);
  516. b.retain(|x: &Maison| !rule2(x) || ba || bc);
  517. c.retain(|x: &Maison| !rule2(x) || bb || bd);
  518. d.retain(|x: &Maison| !rule2(x) || bc || be);
  519. e.retain(|x: &Maison| !rule2(x) || bd );
  520. }
  521.  
  522. }
  523.  
  524. }
  525.  
  526. fn constraint_voisin_gauche_droite<F,G>(&mut self,rule1 : F,rule2 : G)
  527. where F: Fn(&Maison) -> bool, G: Fn(&Maison) -> bool{
  528.  
  529. match *self{
  530. HypotheseVector(ref mut a,ref mut b,ref mut c,ref mut d,ref mut e) => {
  531. let ba=vec_has(&a,|x: &Maison| rule1(x));
  532. let bb=vec_has(&b,|x: &Maison| rule1(x));
  533. let bc=vec_has(&c,|x: &Maison| rule1(x));
  534. let bd=vec_has(&d,|x: &Maison| rule1(x));
  535. //let be=vec_has(&e,|x: &Maison| rule1(x));
  536.  
  537. a.retain(|x: &Maison| !rule2(x));
  538. b.retain(|x: &Maison| !rule2(x) || ba);
  539. c.retain(|x: &Maison| !rule2(x) || bb);
  540. d.retain(|x: &Maison| !rule2(x) || bc);
  541. e.retain(|x: &Maison| !rule2(x) || bd );
  542.  
  543. //let da=vec_has(&a,|x: &Maison| rule2(x));
  544. let db=vec_has(&b,|x: &Maison| rule2(x));
  545. let dc=vec_has(&c,|x: &Maison| rule2(x));
  546. let dd=vec_has(&d,|x: &Maison| rule2(x));
  547. let de=vec_has(&e,|x: &Maison| rule2(x));
  548.  
  549. a.retain(|x: &Maison| !rule1(x) || db);
  550. b.retain(|x: &Maison| !rule1(x) || dc);
  551. c.retain(|x: &Maison| !rule1(x) || dd);
  552. d.retain(|x: &Maison| !rule1(x) || de);
  553. e.retain(|x: &Maison| !rule1(x));
  554. }
  555.  
  556. }
  557.  
  558. }
  559.  
  560.  
  561. fn constraint_voisin<F,G>(&mut self,rule1 : F,rule2 : G)
  562. where F: Fn(&Maison) -> bool, G: Fn(&Maison) -> bool{
  563.  
  564.  
  565. self.constraint_voisin_keep_not_in_or_voisin_in(|x: &Maison| rule1(x),|x: &Maison| rule2(x));
  566. self.constraint_voisin_keep_not_in_or_voisin_in(|x: &Maison| rule2(x),|x: &Maison| rule1(x));
  567. }
  568.  
  569. //4. La maison verte est juste à gauche de la maison blanche.
  570. //8. L'homme qui vit dans la maison du centre boit du lait.
  571. //9. Le Norvégien vit dans la première maison.
  572. //10. L'homme qui fume des Blends vit à côté de celui qui élève des chats.
  573. //11. L'homme qui élève des chevaux vit à côté du fumeur de Dunhills.
  574. //14. Le Norvégien vit à côté de la maison bleue.
  575. //15. L'homme qui fume des Blends a un voisin qui boit de l'eau.
  576.  
  577. //4. La maison verte est juste à gauche de la maison blanche.
  578. fn r4_maison_verte_gauche_blanche<'cl, 'a> ( &mut self ) -> (){
  579. self.constraint_voisin_gauche_droite(|x: &Maison| has_couleur(x,Couleur::Verte),|x: &Maison| has_couleur(x,Couleur::Blanche));
  580.  
  581.  
  582. }
  583.  
  584. //8. L'homme qui vit dans la maison du centre boit du lait.
  585. fn r8_centre_lait<'cl, 'a> ( &mut self ) -> (){
  586. match *self{
  587. HypotheseVector(ref mut a,ref mut b,ref mut c,ref mut d,ref mut e) => {
  588. a.retain(|x: &Maison| !has_lait(x));
  589. b.retain(|x: &Maison| !has_lait(x));
  590. c.retain(|x: &Maison| has_lait(x));
  591. d.retain(|x: &Maison| !has_lait(x));
  592. e.retain(|x: &Maison| !has_lait(x));
  593.  
  594. }
  595.  
  596. }
  597.  
  598.  
  599. }
  600.  
  601. //9. Le Norvégien vit dans la première maison.
  602. fn r9_norvegien_premiere<'cl, 'a> ( &mut self ) -> (){
  603. match *self{
  604. HypotheseVector(ref mut a,ref mut b,ref mut c,ref mut d,ref mut e) => {
  605. a.retain(|x: &Maison| has_a_norvegien(x));
  606. b.retain(|x: &Maison| !has_a_norvegien(x));
  607. c.retain(|x: &Maison| !has_a_norvegien(x));
  608. d.retain(|x: &Maison| !has_a_norvegien(x));
  609. e.retain(|x: &Maison| !has_a_norvegien(x));
  610.  
  611. }
  612.  
  613. }
  614. }
  615. //10. L'homme qui fume des Blends vit à côté de celui qui élève des chats.
  616. fn r10_blends_voisin_chats<'cl, 'a> ( &mut self ) -> (){
  617. self.constraint_voisin(|x: &Maison| has_cigarette(x,Cigarette::Blends),|x: &Maison| has_animaux(x,Animaux::Chats));
  618. }
  619. //11. L'homme qui élève des chevaux vit à côté du fumeur de Dunhills.
  620. fn r11_chevaux_voisin_dunhills<'cl, 'a> ( &mut self ) -> (){
  621. self.constraint_voisin(|x: &Maison| has_cigarette(x,Cigarette::Dunhills),|x: &Maison| has_animaux(x,Animaux::Chevaux));
  622. }
  623.  
  624. //14. Le Norvégien vit à côté de la maison bleue.
  625. fn r14_norvegien_cote_maison_bleue<'cl, 'a> ( &mut self ) -> (){
  626. self.constraint_voisin(|x: &Maison| has_a_norvegien(x),|x: &Maison| has_bleue(x));
  627. }
  628.  
  629. //15. L'homme qui fume des Blends a un voisin qui boit de l'eau.
  630. fn r15_blend_voisin_eau<'cl, 'a> ( &mut self ) -> (){
  631. self.constraint_voisin(|x: &Maison| has_cigarette(x,Cigarette::Blends),|x: &Maison| has_boisson(x,Boisson::Eau));
  632. }
  633.  
  634. fn apply<F>(&mut self,rule : F)
  635. where F: Fn(&Maison) -> bool{
  636.  
  637. match *self{
  638. HypotheseVector(ref mut a,ref mut b,ref mut c,ref mut d,ref mut e) => {
  639. a.retain(|x: &Maison| rule(x));
  640. b.retain(|x: &Maison| rule(x));
  641. c.retain(|x: &Maison| rule(x));
  642. d.retain(|x: &Maison| rule(x));
  643. e.retain(|x: &Maison| rule(x));
  644.  
  645. }
  646.  
  647. }
  648.  
  649. }
  650.  
  651. fn simple_constraints(&mut self){
  652. self.apply(r1_anglais_rouge);
  653. self.apply(r2_suedois_chien);
  654. self.apply(r3_danois_the);
  655. self.apply(r5_verte_cafe);
  656. self.apply(r6_pallmall_oiseau);
  657. self.apply(r7_jaune_dunhills);
  658. self.apply(r12_bluemaster_biere);
  659. self.apply(r13_allemand_prince);
  660.  
  661.  
  662. }
  663.  
  664.  
  665. fn other_constraintes(&mut self){
  666. self.r4_maison_verte_gauche_blanche();
  667. self.r8_centre_lait();
  668. self.r9_norvegien_premiere();
  669. self.r10_blends_voisin_chats();
  670. self.r11_chevaux_voisin_dunhills();
  671. self.r14_norvegien_cote_maison_bleue();
  672. self.r15_blend_voisin_eau();
  673.  
  674. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_couleur(x,Couleur::Verte));
  675. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_couleur(x,Couleur::Blanche));
  676. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_couleur(x,Couleur::Jaune));
  677. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_couleur(x,Couleur::Bleue));
  678. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_couleur(x,Couleur::Rouge));
  679.  
  680. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_nationalite(x,Nationalite::Suedois));
  681. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_nationalite(x,Nationalite::Danois));
  682. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_nationalite(x,Nationalite::Anglais));
  683. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_nationalite(x,Nationalite::Norvegien));
  684. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_nationalite(x,Nationalite::Allemand));
  685.  
  686. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_boisson(x,Boisson::Eau));
  687. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_boisson(x,Boisson::The));
  688. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_boisson(x,Boisson::Cafe));
  689. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_boisson(x,Boisson::Biere));
  690. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_boisson(x,Boisson::Lait));
  691.  
  692. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_cigarette(x,Cigarette::Dunhills));
  693. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_cigarette(x,Cigarette::PallMall));
  694. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_cigarette(x,Cigarette::Blends));
  695. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_cigarette(x,Cigarette::BleueMasters));
  696. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_cigarette(x,Cigarette::Prince));
  697.  
  698. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_animaux(x,Animaux::Oiseaux));
  699. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_animaux(x,Animaux::Chien));
  700. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_animaux(x,Animaux::Chevaux));
  701. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_animaux(x,Animaux::Chats));
  702. self.constraint_voisin_keep_unique_as_single(|x: &Maison| has_animaux(x,Animaux::PoissonRouge));
  703. }
  704. }
  705.  
  706.  
  707.  
  708. pub fn main()->(){
  709.  
  710. println!("Einstein enigma program");
  711.  
  712.  
  713. let mut h=full_possibility();
  714. println!("> determinant de base : {} ",h.count_determinant());
  715.  
  716. h.simple_constraints();
  717. //println!("> {}", h);
  718.  
  719. //println!("> determinant apres contraintes simples : {} ",h.count_determinant());
  720.  
  721. let mut count_apply=0;
  722.  
  723. loop{
  724. count_apply=count_apply+1;
  725. let prev_det=h.count_determinant();
  726. //println!("-------------------------------");
  727. //println!("-------------------------------");
  728. //println!("-------------------------------");
  729. //println!("-------------------------------");
  730. //println!("-------------------------------");
  731.  
  732. h.other_constraintes();
  733. //println!("{}", h);
  734. println!("determinant : {} : apres {} application des regles ",h.count_determinant(), count_apply);
  735.  
  736. if prev_det==h.count_determinant() {break;}
  737.  
  738. }
  739. println!("------------ FIN DU PROGRAMME -----------");
  740. println!("determinant : {} : apres {} application des regles ",h.count_determinant(), count_apply);
  741. println!("{}", h);
  742.  
  743.  
  744.  
  745. }
  746.  
  747.  
  748.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:2: error: invalid preprocessing directive #[
 #[derive( PartialEq,Eq,Debug,Clone)]
  ^
prog.cpp:29:2: error: invalid preprocessing directive #[
 #[derive( PartialEq,Eq,Debug,Clone)]
  ^
prog.cpp:54:2: error: invalid preprocessing directive #[
 #[derive( PartialEq,Eq,Debug,Clone)]
  ^
prog.cpp:80:2: error: invalid preprocessing directive #[
 #[derive( PartialEq,Eq,Debug,Clone)]
  ^
prog.cpp:105:2: error: invalid preprocessing directive #[
 #[derive( PartialEq,Eq,Debug,Clone)]
  ^
prog.cpp:130:2: error: invalid preprocessing directive #[
 #[derive(Debug,Clone)]
  ^
prog.cpp:234:2: error: invalid preprocessing directive #[
 #[derive(Debug)]
  ^
prog.cpp:243:11: error: too many decimal points in number
  for _ in 0..nb_boucle{
           ^
prog.cpp:264:23: warning: character constant too long for its type
   fn r1_anglais_rouge<'r> (input : &'r Maison ) -> bool {
                       ^
prog.cpp:275:23: warning: character constant too long for its type
   fn r2_suedois_chien<'r> (input : &'r Maison ) -> bool {
                       ^
prog.cpp:286:20: warning: character constant too long for its type
   fn r3_danois_the<'r> (input : &'r Maison ) -> bool {
                    ^
prog.cpp:296:20: warning: character constant too long for its type
   fn r5_verte_cafe<'r> (input : &'r Maison ) -> bool {
                    ^
prog.cpp:307:25: warning: character constant too long for its type
   fn r6_pallmall_oiseau<'r> (input : &'r Maison ) -> bool {
                         ^
prog.cpp:318:24: warning: character constant too long for its type
   fn r7_jaune_dunhills<'r> (input : &'r Maison ) -> bool {
                        ^
prog.cpp:329:27: warning: character constant too long for its type
   fn r12_bluemaster_biere<'r> (input : &'r Maison ) -> bool {
                           ^
prog.cpp:340:26: warning: character constant too long for its type
   fn r13_allemand_prince<'r> (input : &'r Maison ) -> bool {
                          ^
prog.cpp:369:22: warning: character constant too long for its type
   fn has_a_norvegien<'r> (input : &'r Maison ) -> bool {
                      ^
prog.cpp:376:15: warning: character constant too long for its type
   fn has_lait<'r> (input : &'r Maison ) -> bool {
               ^
prog.cpp:383:16: warning: character constant too long for its type
   fn has_bleue<'r> (input : &'r Maison ) -> bool {
                ^
prog.cpp:391:18: warning: character constant too long for its type
   fn has_couleur<'r> (input : &'r Maison, coul : Couleur ) -> bool {
                  ^
prog.cpp:397:22: warning: character constant too long for its type
   fn has_nationalite<'r> (input : &'r Maison, coul : Nationalite ) -> bool {
                      ^
prog.cpp:403:18: warning: character constant too long for its type
   fn has_boisson<'r> (input : &'r Maison, coul : Boisson ) -> bool {
                  ^
prog.cpp:409:20: warning: character constant too long for its type
   fn has_cigarette<'r> (input : &'r Maison, coul : Cigarette ) -> bool {
                    ^
prog.cpp:415:18: warning: character constant too long for its type
   fn has_animaux<'r> (input : &'r Maison, coul : Animaux ) -> bool {
                  ^
prog.cpp:578:36: warning: multi-character character constant [-Wmultichar]
  fn r4_maison_verte_gauche_blanche<'cl, 'a> ( &mut self ) -> (){
                                    ^
prog.cpp:585:20: warning: multi-character character constant [-Wmultichar]
  fn r8_centre_lait<'cl, 'a> ( &mut self ) -> (){
                    ^
prog.cpp:602:27: warning: multi-character character constant [-Wmultichar]
  fn r9_norvegien_premiere<'cl, 'a> ( &mut self ) -> (){
                           ^
prog.cpp:616:29: warning: multi-character character constant [-Wmultichar]
  fn r10_blends_voisin_chats<'cl, 'a> ( &mut self ) -> (){
                             ^
prog.cpp:620:33: warning: multi-character character constant [-Wmultichar]
  fn r11_chevaux_voisin_dunhills<'cl, 'a> ( &mut self ) -> (){
                                 ^
prog.cpp:625:37: warning: multi-character character constant [-Wmultichar]
  fn r14_norvegien_cote_maison_bleue<'cl, 'a> ( &mut self ) -> (){
                                     ^
prog.cpp:630:26: warning: multi-character character constant [-Wmultichar]
  fn r15_blend_voisin_eau<'cl, 'a> ( &mut self ) -> (){
                          ^
prog.cpp:1:1: error: 'use' does not name a type
 use std::fmt;
 ^
prog.cpp:12:6: error: expected initializer before 'Couleur'
 impl Couleur{
      ^
stdout
Standard output is empty