/examples/rot13.bf

https://github.com/darrenclark/mindfudge · Brainfuck · 30 lines · 29 code · 1 blank · 0 comment · 26 complexity · 7c713c518c30fd70a1233f6e0cf252ce MD5 · raw file

  1. Source: http://en(dot)wikipedia(dot)org/wiki/Brainfuck
  2. -,+[ Read first character and start outer character reading loop
  3. -[ Skip forward if character is 0
  4. >>++++[>++++++++<-] Set up divisor (32) for division loop
  5. (MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero)
  6. <+<-[ Set up dividend (x minus 1) and enter division loop
  7. >+>+>-[>>>] Increase copy and remainder / reduce divisor / Normal case: skip forward
  8. <[[>+<-]>>+>] Special case: move remainder back to divisor and increase quotient
  9. <<<<<- Decrement dividend
  10. ] End division loop
  11. ]>>>[-]+ End skip loop; zero former divisor and reuse space for a flag
  12. >--[-[<->+++[-]]]<[ Zero that flag unless quotient was 2 or 3; zero quotient; check flag
  13. ++++++++++++<[ If flag then set up divisor (13) for second division loop
  14. (MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero)
  15. >-[>+>>] Reduce divisor; Normal case: increase remainder
  16. >[+[<+>-]>+>>] Special case: increase remainder / move it back to divisor / increase quotient
  17. <<<<<- Decrease dividend
  18. ] End division loop
  19. >>[<+>-] Add remainder back to divisor to get a useful 13
  20. >[ Skip forward if quotient was 0
  21. -[ Decrement quotient and skip forward if quotient was 1
  22. -<<[-]>> Zero quotient and divisor if quotient was 2
  23. ]<<[<<->>-]>> Zero divisor and subtract 13 from copy if quotient was 1
  24. ]<<[<<+>>-] Zero divisor and add 13 to copy if quotient was 0
  25. ] End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3)
  26. <[-] Clear remainder from first division if second division was skipped
  27. <.[-] Output ROT13ed character from copy and clear it
  28. <-,+ Read next character
  29. ] End character reading loop