BogdanMihai2013

Deadfish Coder

Jan 1st, 2025 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | Source Code | 0 0
  1. import time
  2. import random
  3. def circular_difference(a, b, modulus=256):
  4.     diff = (b - a) % modulus
  5.     if diff > modulus // 2:
  6.         diff -= modulus
  7.     return diff
  8. def errorsingle (acc,end):
  9.     return abs(circular_difference(acc,end))
  10. def iDEADS (acc):
  11.     return (acc+1)%256
  12. def iDEAD (acc):
  13.     return list(map(iDEADS,acc))
  14. def dDEADS (acc):
  15.     return (acc-1)%256
  16. def dDEAD (acc):
  17.     return list(map(dDEADS,acc))
  18. def sDEADS (acc):
  19.     return (acc*acc)%256
  20. def sDEAD (acc):
  21.     return list(map(sDEADS,acc))
  22. def error (acc,ends):
  23.     err = list(map(errorsingle,acc,ends))
  24.     return sum(err)
  25. def advancedError1 (accc,ends):
  26.     # First layer of "Inteligence"
  27.     return min(error(iDEAD(accc),ends),error(dDEAD(accc),ends),error(sDEAD(accc),ends),error(accc,ends)*2)
  28. def advancedError2 (accc,ends):
  29.     # Second layer of "Inteligence" that somehow is worse
  30.     return min(advancedError1(iDEAD(accc),ends),advancedError1(dDEAD(accc),ends),advancedError1(sDEAD(accc),ends),advancedError1(accc,ends))
  31. def FindCommands(starts,ends,epochs):
  32.     acc=starts
  33.     program=""
  34.     currEpoch=0
  35.     while currEpoch<epochs:
  36.         currEpoch=currEpoch+1
  37.         bestName="i"
  38.         errforI=advancedError1(iDEAD(acc),ends)
  39.         bestError=errforI
  40.         errforD=advancedError1(dDEAD(acc),ends)
  41.         if errforD<bestError:
  42.             bestName="d"
  43.             bestError=errforD
  44.         errforS=advancedError1(sDEAD(acc),ends)
  45.         if errforS<bestError:
  46.             bestName="s"
  47.         print("Lowest error:"+bestName)
  48.         program=program+bestName
  49.         if bestName == "i":
  50.             acc = iDEAD(acc)
  51.         if bestName == "d":
  52.             acc = dDEAD(acc)
  53.         if bestName == "s":
  54.             acc = sDEAD(acc)
  55.         print("Program:"+program)
  56.         if (ends == acc):
  57.             return program
  58.         if (bestName == "d" and program[ len(program)-2]=="i"):
  59.             print("Trying another solution...")
  60.             if random.randint(0.0,1.0)>0.65:
  61.                 print("Trying decrementing...")
  62.                 acc = dDEAD(acc)
  63.                 program=program+"d"
  64.             else:
  65.                 print("Trying squaring...")
  66.                 acc = sDEAD(acc)
  67.                 program=program+"s"
  68.         time.sleep(0.1)
  69.     return "Solution not found"
  70. def compress (program):
  71.     layer=0
  72.     compressed=program
  73.     while layer<5:
  74.         layer=layer+1
  75.         compressed=compressed.replace("di","")
  76.         compressed=compressed.replace("id","")
  77.         compressed=compressed.replace("ssssss","sssss")
  78.     return compressed
  79. solutionfound=FindCommands([0,1,2],[1,0,1],100)
  80. print("Solution found:"+compress(solutionfound))
Advertisement
Add Comment
Please, Sign In to add comment