No sé si a alguien le va a interesar esto (¡o si es el lugar correcto para publicarlo!). Pasé un par de horas intentando escribir un script</strkeep><strkeep> que resolviera el puzzle 'Safe Cracker' del periódico. Mi código funcionó pero, como verás, es muy torpe y no está bien escrito (¡además, algunas de mis matemáticas son sospechosas!). No quiero que me critiquen el código porque sólo era una prueba para ver si podía hacerlo, pero me interesaría mucho ver si alguien ha hecho un trabajo mejor.
set ListNoZeros to {}
set ListNoRepeats1 to {}
set ListNoRepeats2 to {}
set ListNoRepeats3 to {}
set ListNoRepeats4 to {}
set ListNoRepeats5 to {}
set ListNoRepeats6 to {}
set ListNoRepeats7 to {}
set ListNoRepeats8 to {}
set ListNoRepeats9 to {}
set ListNoRepeats10 to {}
set ListNoRepeats11 to {}
set ListNoRepeats12 to {}
set ListNoRepeats13 to {}
set ListNoRepeats14 to {}
set ListNoRepeats15 to {}
set ListNoRepeats16 to {}
set ListAnswer to {}
repeat with a from 1234 to 9876
--split the number into seperate digits
set theNumber to a as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
-- Remove all zeros
if Num1 is not 0 and Num2 is not 0 and Num3 is not 0 and Num4 is not 0 then copy a to the end of ListNoZeros -- Number of posibilities = 6357
end repeat
-- Is each number different? (Testing 1 & 2)
repeat with b from 1 to length of ListNoZeros
set theCurrentListItem to item b of ListNoZeros
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if Num1 / Num2 is not equal to 1 then copy theCurrentListItem to the end of ListNoRepeats1 -- Number of posibilities = 5790
end repeat
-- Is each number different? (Testing 1 & 3)
repeat with c from 1 to length of ListNoRepeats1
set theCurrentListItem to item c of ListNoRepeats1
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if Num1 / Num3 is not equal to 1 then copy theCurrentListItem to the end of ListNoRepeats2 -- Number of posibilities = 5160
end repeat
-- Is each number different? (Testing 1 & 4)
repeat with d from 1 to length of ListNoRepeats2
set theCurrentListItem to item d of ListNoRepeats2
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if Num1 / Num4 is not equal to 1 then copy theCurrentListItem to the end of ListNoRepeats3 -- Number of posibilities = 4588
end repeat
-- Is each number different? (Testing 2 & 3)
repeat with e from 1 to length of ListNoRepeats3
set theCurrentListItem to item e of ListNoRepeats3
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if Num2 / Num3 is not equal to 1 then copy theCurrentListItem to the end of ListNoRepeats4 -- Number of posibilities = 4028
end repeat
-- Is each number different? (Testing 2 & 4)
repeat with f from 1 to length of ListNoRepeats4
set theCurrentListItem to item f of ListNoRepeats4
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if Num2 / Num4 is not equal to 1 then copy theCurrentListItem to the end of ListNoRepeats5 -- Number of posibilities = 3526
end repeat
-- Is each number different? (Testing 3 & 4)
repeat with g from 1 to length of ListNoRepeats5
set theCurrentListItem to item g of ListNoRepeats5
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if Num3 / Num4 is not equal to 1 then copy theCurrentListItem to the end of ListNoRepeats6 -- Number of posibilities = 3024
end repeat
-- The 4th is 3 more than the 3rd
repeat with h from 1 to length of ListNoRepeats6
set theCurrentListItem to item h of ListNoRepeats6
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if Num4 = Num3 + (3) then copy theCurrentListItem to the end of ListNoRepeats7 -- Number of posibilities = 252
end repeat
-- The 1st and the 3rd differ by 4. First (1st > 3rd)
repeat with i from 1 to length of ListNoRepeats7
set theCurrentListItem to item i of ListNoRepeats7
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if (Num3 - Num1) = 4 then copy theCurrentListItem to the end of ListNoRepeats8 -- Number of posibilities = 12
end repeat
-- The 1st and the 3rd differ by 4. Second (1st < 3rd)
repeat with j from 1 to length of ListNoRepeats7 -- Using this list twice.....
set theCurrentListItem to item j of ListNoRepeats7
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if (Num1 - Num3) = 4 then copy theCurrentListItem to the end of ListNoRepeats9 -- Number of posibilities = 30
end repeat
-- Join these 2 lists together
set ListNoRepeats10 to ListNoRepeats8 & ListNoRepeats9 -- Number of posibilities = 42
-- The sum of the middle two is divisible by 5
repeat with k from 1 to length of ListNoRepeats10
set theCurrentListItem to item k of ListNoRepeats10
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if (Num2 + Num3) = 5 or (Num2 + Num3) = 10 then copy theCurrentListItem to the end of ListNoRepeats11 -- Number of posibilities = 7
end repeat
-- Exactly one of the first 2 is odd (First & Second)
repeat with l from 1 to length of ListNoRepeats11
set theCurrentListItem to item l of ListNoRepeats11
--split the number into seperate digits
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if Num1 mod 2 = 0 and Num2 mod 2 = 1 then copy theCurrentListItem to the end of ListNoRepeats12
if Num1 mod 2 = 1 and Num2 mod 2 = 0 then copy theCurrentListItem to the end of ListNoRepeats13
-- Join these 2 lists together
set ListNoRepeats14 to ListNoRepeats12 & ListNoRepeats13 -- Number of posibilities = 3
end repeat
-- 2nd or 4th are square (but not both)
repeat with m from 1 to length of ListNoRepeats14
set theCurrentListItem to item m of ListNoRepeats14
set theNumber to theCurrentListItem as string
set String1 to characters 1 thru 1 of theNumber
set String2 to characters 2 thru 2 of theNumber
set String3 to characters 3 thru 3 of theNumber
set String4 to characters 4 thru 4 of theNumber
--Convert back to number
set Num1 to String1 as number
set Num2 to String2 as number
set Num3 to String3 as number
set Num4 to String4 as number
if Num2 = 1 or Num2 = 4 or Num2 = 9 then copy theCurrentListItem to the end of ListNoRepeats15
if Num4 = 1 or Num4 = 4 or Num4 = 9 then copy theCurrentListItem to the end of ListNoRepeats16
-- Join these 2 lists together
set ListAnswer to ListNoRepeats15 & ListNoRepeats16
end repeat
display dialog "The Correct Code is - " & ListAnswer ```