Last active
November 16, 2025 17:11
-
-
Save avih/3c7733e251e7edaf161f7f71c840ce69 to your computer and use it in GitHub Desktop.
infinite decimal counter in brainf*ck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //[ | |
| // infinite decimal counter in bf / by avih | |
| // with tips and improvements from ais523 and int-e | |
| // also: bonus version by int-e at the bottom | |
| //] | |
| ++++++++++[>>>>->+<[->[-]>+<<]>>[<<+>>-]<[-<++++++++++>>>->+<[- | |
| >[-]>+<<]>>[<<+>>-]<]<+>+<[->[-]>+<<]>>[<<+>>-]<[-<++++++++++>] | |
| <-[>>>]<<<[<++++++++[<+++++++>-]>[<+<->>-]<[->+<]<++.[-]<]<.] | |
| //[ (UNREACHABLE) COMMENTED VERSION | |
| // | |
| // runtime memory state: '\n' 0 0 0 LSD 0 0 ... D 0 0 ... MSD 0 0 | |
| // 2 tmp cells between each 2 digits | |
| // the tmps start as 0 and end as 0 before we move to another digit | |
| // | |
| // digits '0' to '9' are cell vals 10 to 1 respectively | |
| // ie reverse mapping: 58 minus val is the ascii digit | |
| // | |
| // comments above the code are overview | |
| // comments to the right are memory state after the line | |
| // | |
| // notation: | |
| // "D" ascii val | |
| // "digit" the location | |
| // "val" the original digit cell value | |
| // (==X) condition: current cell has value X | |
| // =Y set current cell to value Y | |
| // ADDR: head is at absolute ADDR | |
| // *A @B C consecutive mem values A B C | |
| // @ is the head position | |
| // * is the digit position we're working on | |
| //] | |
| +++++ +++++ // 0: @10 | |
| [ // 0: @10 | |
| >>>> // 4: *@(least significant digit / if any) | |
| // while (==1) {=10; right 3} | |
| // ie while (D=='9') {D='0'; head to next digit (more significant)} | |
| ->+<[->[-]>+<<] >>[<<+>>-]<[ // *(val min 1) @(bool val==1) 0 | |
| -<+ // *@val(1) 0 0 | |
| +++++ ++++ // plus 9 (*@val(10) 0 0) | |
| >>> // next digit | |
| ->+<[->[-]>+<<] >>[<<+>>-]< // (next*)(nextVal min 1) @nextVal==1 0 | |
| ]<+ // *@(first not '9' digit / if any) 0 0 | |
| // if (==0) {=10} | |
| // ie if we ended up past our most significant (rightmost) digit | |
| // then add new digit (set it to 10 which is '0') | |
| >+<[->[-]>+<<] >>[<<+>>-]<[ // *val @(bool val==0) 0 | |
| -< // *@0 0 0 | |
| +++++ +++++ // *@10 0 0 | |
| > // *10 @0 0 | |
| ]< // *@(val / maybe new '0') 0 0 | |
| // inc digit (1st non '9') and head to most significant digit | |
| - // dec val (inc digit) by 1 | |
| [>>>]<<< // skip rest / *@(most significant) | |
| // while (not 0) {print 58 minus val; head to prev digit (less signific)} | |
| [ // start/use: 0 0 *@val | |
| <+++++ +++[<+++++ ++>-]> // 56 0 *@val | |
| [-<+<->>]< // (56 min val) @val *0 | |
| [->+<]< ++ // @(58 min val) 0 *val | |
| . [-] // print and cleanup / end: @0 0 *val | |
| < // head to prev digit (less significant) | |
| ] | |
| // 1: | |
| < . // 0: print ('\n') | |
| ] | |
| //[ (UNREACHABLE) bonus version by int-e: ] | |
| >>+[[----------[+++++++++++[>>]>+<]+>>]<[-<-<<--<]>++[> | |
| ++++++++[<<++>++++++>-]<-.+<[>---<-]<]++++++++++.[-]>>] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment