Skip to content

Instantly share code, notes, and snippets.

@dezgeg
Created October 22, 2011 18:37
Show Gist options
  • Save dezgeg/1306340 to your computer and use it in GitHub Desktop.
Save dezgeg/1306340 to your computer and use it in GitHub Desktop.
Brainfuck-shakkilauta
Variables: first digit; second digit; multiplier; scratch1; scratch2
,------------------------------------------------ convert 1st digit from ascii
>, read second digit
---------- subtract '\n'
[ if not newline; calculate 10*digit1 plus digit2 to digit1
-------------------------------------- convert from ascii
>++++++++++ set multiplier to 10
[- loop while multiplier not zero
<<[->>>+>+<<<<] destructively copy 1st digit to scratch1 & scratch2
>>>[-<<+>>] destructively add scratch1 to 2nd digit
>[-<<<<+>>>>] destructively move scratch2 to first digit again
<< back to multiplier
] result now in digit2
<<[-] zero digit1
>[-<+>] destructively move digit2 to digit1
]
Vars for printing: n; '0'; outer index; inner index; b; scratch/'perform else' flag; scratch2; 'n is odd' flag; scratch3; scratch4
Initialize with: n; '0'; n; n; zero; zero
<[->+>>+>+ copy n to n; inner index; outer index
>>>>> +<[->-<]> scratch3 = NOT 'n is odd'
[<+>-] <<<<< copy scratch3 to 'n is odd'
<<<<]
>>++++++++++++++++++++++++++++++++++++++++++++++++
>
[ loop outer index
>[ loop inner index
> at b
>+< set 'perform else' flag
if b == 1 [ <<<+.->>> print 1
- clear b
>- clear 'perform else'
] > [ if 'perform else' flag set
<<<<.>>>> print 0
<+> set b to 1
- clear flag for next iteration
>
]
<<<- dec inner index
]
<<< now at n
[->>>+>>+<<<<<] copy destructively n to inner index and scratch
>>>>> [-<<<<<+>>>>>] copy destructively scratch1 to n
++++++++++.[-] print newline
>>[->+>+<<] copy 'is odd' flag to scratch 3 & 4
>-[if n is even
<<+<<[->>-<<]>> scratch2 = NOT b
[-<<+>>] move scratch2 to b
>>++[-]
]
>[-<<+>>] move scratch4 to 'is odd' flag
<<<<<<<- dec outer index
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment