J’y ai pensé aussi, je l’ai fait. Je ne suis pas un pro du haskell, soyez bienveillant avec vos remarques ;-)
importData.List-- A value is an Int or an operation with two arguments and a result.dataValue=VInt|OpOperationValueValueIntdataOperation=Plus|Min|Mult|Div|Error-- Instance of Show class to be able to display Operation typeinstanceShowOperationwhereshowPlus="+"showMin="-"showMult="×ばつ"showDiv="÷"show_="Error"-- Instance of Show class for Value.instanceShowValuewhereshow(Va)=showashow(Opoab_)=" ("++(showa)++(showo)++(showb)++") "-- Take an Operation and a list of 2 minimal Values. Generate a new value with the first two terms.doOp::Operation->[Value]->[Value]doOpPlus(a:b:xs)=(OpPlusab(va+vb):xs)whereva=getVavb=getVb-- We don't want to have negative values, so check if a >= bdoOpMin(a:b:xs)|(getVa)>=(getVb)=(OpMinab((getVa)-(getVb)):xs)|otherwise=(OpErrorab(-1)):xsdoOpMult(a:b:xs)=(OpMultab(va*vb)):xswhereva=getVavb=getVb-- First check that divisor is not 0, then check if b can divide a without rest.doOpDiv(a:b:xs)|vb==0=(OpErrorab(-1)):xs|va`mod`vb==0=(OpDivab(va`div`vb)):xs|otherwise=(OpErrorab(-1)):xswhereva=getVavb=getVbsetVa=VagetV(Va)=agetV(Op___a)=a-- Do operations, take a list of Values and do all operations, drop error if a computation is not possibledoOps::[Value]->[[Value]]doOpsa=filter(dropError.head)$map(\x->doOpxa)[Plus,Min,Mult,Div]-- Check for error filter, return True if it's valid.dropError::Value->BooldropError(OpError___)=FalsedropError_=True-- En entrée une liste de values, en sortie une liste d'opérations filtrée.-- Check we have 4 numbers.-- From right to left-- - map setV x => generate a Value list with only V values.-- - compute all permutations-- - For each list of values apply all operations on first two values, we get list of list of list so concat to obtain list of list Values-- - We have 3 elements-- - For each element, we perform permutations and concat to get a list of list of Values.-- - loop twice to get list of list of Value with length == 1.-- - Then filter to check if result is 24.get24::[Int]->[[Value]]get24x|lengthx==4=filter(\x->(getV(headx))==24)$concat.mapdoOps$concat.mappermutations$concat.mapdoOps$concat.mappermutations$concat.mapdoOps$permutations$mapsetVx|otherwise=[]
[^] # Haskell
Posté par Anthony Jaguenaud . En réponse au journal résoudre "trouve 24". Évalué à 3. Dernière modification le 04 mars 2022 à 19:37.
J’y ai pensé aussi, je l’ai fait. Je ne suis pas un pro du haskell, soyez bienveillant avec vos remarques ;-)
Et les résultats :
Évidemment, on prend toute les permutations d’une même solution.