Mines2021.ml 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. let numero_interieur z = let x = z/8 and y = z mod 8 in
  2. (y <= 6 && x <= 6 && (abs(x-3) + abs(y-3)) <= 4 && x>=0 && y >= 0);;
  3. let rec intervalle a b = match a with
  4. | a when a = b -> [a]
  5. | a -> a::(intervalle (a+1) b);;
  6. let numeros_europeens = List.filter numero_interieur (intervalle 0 53);;
  7. type motif = int;;
  8. type ponctuel = motif;;
  9. let numero_vers_ponctuel (z:int) :ponctuel =
  10. (1 lsl z);;
  11. let rec numeros_vers_motifs (l:int list) :motif = match l with
  12. | [] -> 0
  13. | h::t -> (numero_vers_ponctuel h)lor(numeros_vers_motifs t);;
  14. let motif_europeen = numeros_vers_motifs numeros_europeens;;
  15. let est_ponctuel (m:motif) :bool = (m land (m-1)) = 0 && (m>0);;
  16. let inclus (m:motif) (p:ponctuel) :bool = (m land p) <> 0;;
  17. let valide (p:ponctuel):bool = inclus motif_europeen p;;
  18. let voisin_g (p:ponctuel) : ponctuel = let res = p lsl 1 in
  19. if valide res then res else 0;;
  20. let voisin_d (p:ponctuel) : ponctuel = let res = p lsr 1 in
  21. if valide res then res else 0;;
  22. let voisin_h (p:ponctuel) : ponctuel = let res = p lsl 8 in
  23. if valide res then res else 0;;
  24. let voisin_b (p:ponctuel) : ponctuel = let res = p lsr 8 in
  25. if valide res then res else 0;;
  26. let voisins p = [voisin_g p ;voisin_d p ;voisin_h p ;voisin_b p];;
  27. let retirer_ponctuel_dans_motif (m:motif) (p:ponctuel) :motif = m land (lnot p);;
  28. let existence_ponctuel_p_dans_motif (m:motif) (p:ponctuel) :bool = (m land p) =p;;
  29. (*let ajouter_ponctuel;;*)
  30. let coup_simple ((m,p) :motif*ponctuel) =
  31. let coup_voisin =
  32. let case_suivante = voisins p in
  33. let case_dapres = voisins case_suivante in
  34. if (inclus m case_suivante) && not (inclus m case_dapres) && case_dapres <> 0 then
  35. [m-p-case_suivante+case_dapres;case_dapres]
  36. else
  37. []
  38. in List.flatten (List.map coup voisins)
  39. ;;
  40. let coup_compose (c:motif*ponctuel) =
  41. let rec aux l =
  42. if l = [] then []
  43. else
  44. let nv_couples = List.flatten (List.map coup_simple l) in
  45. nv_couples@(aux nv_couples)
  46. in aux [c]
  47. ;;
  48. let mouvements (m:motif) : motif list =
  49. let rec aux (p:ponctuel) =
  50. if p = 1 lsl 53 then []
  51. else (if inclus m p then coup_compose (m,p) else [])
  52. @(aux (2*p))
  53. in List.map fst (aux 4)
  54. ;;
  55. let add_and_mem d (s:int) (m:motif) : bool =
  56. let dedans = Hashtbl.mem d m in
  57. if not dedans then Hashtbl.add d m s;
  58. dedans
  59. ;;
  60. let rec strate d (s:int) (l:motif list): motif list =
  61. let nv_motifs = List.flatten (List.map mouvements l) in
  62. if add_and_mem d s nv_motifs then
  63. strate d (s+1) nv_motifs
  64. else
  65. []
  66. ;;