Skip to content

Instantly share code, notes, and snippets.

@anna-is-cute
Last active December 3, 2015 00:50
Show Gist options
  • Save anna-is-cute/de06b6d03b91391bd252 to your computer and use it in GitHub Desktop.
Save anna-is-cute/de06b6d03b91391bd252 to your computer and use it in GitHub Desktop.
Advent of Code 1.1
>> Make two temp cells
+++++[>++++++++<-] Put 40 (which is '(') in the fourth cell
, Read in the first byte of input
[
Compare the input to 40
[<+>-]+
>[<<-<+>>>-]
<<<[>>>+<<<-]
>[>-<[-]]
If/else the comparison
<[-]+
>[-]
>[
If the input was a (
>>+ Add one to the increments
<<<<-
>>[<+>-]
]
<[>+<-]
<[
If the input was a )
>>>>>+ Add one to the decrements
<<<<<-]
>>[-],]
We've counted all of the increments and decrements so let's copy the increments to the cell to the right of the decrements
<<[-]
>>>>>>[-]
<<[>>+<<<<<<+>>>>-]
<<<<[>>>>+<<<<-]
Now it's time to find out which is larger
>>>>>>>[-]
>[-] >[-]+ >[-] <<
<<<[>>+ >+ <<<-]
>>>[<<<+ >>>-]
<<<<[>>>>+ <<<<-]
>>>>[>-]> [< <<<<+ >>>[-] >>->]<+<
<[>- [>-]> [< <<<<+ >>>[-]+ >>->]<+< <-]
The fifth cell tells us whether the decrements are larger than the increments
So let's check if that's true
<<<<<<<[-]+
>[-]
>>>[
The decrements are larger so we subtract them from the increments after outputting a minus sign
>>>>[-] Move over to temp3 and clear it
>[-]< Move to temp4 and clear it and move back to temp3
+++++[>+++++++++<-]>.[-] Output a minus and clear the cell
<<<<< Move back to x
this is z equals z minus y
>>>[-]
<[<->>+<-]
>[<+>-]
We have now done the subtraction and all that's left to do is output the number at z
<< Move to z
Copy the number to print out to temp4
>>>[-]
>[-]
<<<<[>>>>+<+<<<-]
>>>[<<<+>>>-]
<<<<<-
>>>>[<<<+>>>-]
]
<<<[>>>+<<<-]
<[
Increments are larger than decrements so subtract them
>>>>>>>[-]
<<[>->+<<-]
>>[<<+>>-]
< Move to z
Copy number to temp4
>>[-]
>[-]
<<<[>>>+<+<<-]
>>[<<+>>-]>
<<<<<<<<<-]
>>>>>>>>> Move to temp4
Print out the number
[>>+>+<<<-]>>>[<<<+>>>-]<<+>[<->[>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]
++++++++[<++++++>-]>[<<+>>-]>[<<+>>-]<<]>]<[->>++++++++[<++++++>-]]<[.[-]<]<
>>+++++[>++++++++<-],[[<+>-]+>[<<-<+>>>-]<<<[>>>+<<<-]>[>-<[-]]<[-]+>[-]>[>>+<<<<->>[<+>-]]<[>+<-]<[>>>>>+<<<<<-]>>[-],]<<[-]>>>>>>[-]<<[>>+<<<<<<+>>>>-]<<<<[>>>>+<<<<-]>>>>>>>[-]>[-]>[-]+>[-]<<<<<[>>+>+<<<-]>>>[<<<+>>>-]<<<<[>>>>+<<<<-]>>>>[>-]>[<<<<<+>>>[-]>>->]<+<<[>-[>-]>[<<<<<+>>>[-]+>>->]<+<<-]<<<<<<<[-]+>[-]>>>[>>>>[-]>[-]<+++++[>+++++++++<-]>.[-]<<[-]<[<->>+<-]>[<+>-]>[-]>[-]<<<<[>>>>+<+<<<-]>>>[<<<+>>>-]<<<<<->>>>[<<<+>>>-]]<<<[>>>+<<<-]<[>>>>>>>[-]<<[>->+<<-]>>[<<+>>-]>[-]>[-]<<<[>>>+<+<<-]>>[<<+>>-]<<<<<<<<-]>>>>>>>>>[>>+>+<<<-]>>>[<<<+>>>-]<<+>[<->[>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]++++++++[<++++++>-]>[<<+>>-]>[<<+>>-]<<]>]<[->>++++++++[<++++++>-]]<[.[-]<]<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment