Introduction
Braincopter is an esoteric Brainf*** derivative that encodes a Brainf*** program as an image. As a 2D language, it uses two additional commands; they rotate the instruction pointer (initially pointing right) clockwise and counterclockwise. Braincopter is very similar to Brainloller, except that instead of using distinct colours as different commands, it instead calculates the command based on the RGB value of each pixel.
Braincopter calculates the command for each pixel based on the formula
N = (65536 * R + 256 * G + B) % 11
, where N
is the command number. The
mapping from command numbers to Brainf*** commands is as follows:
0 >
1 <
2 +
3 -
4 .
5 ,
6 [
7 ]
8 rotate instruction pointer to the right (clockwise)
9 rotate instruction pointer to the left (counterclockwise)
10 NOP
Braincopter is useful for steganography, or the hiding of secret messages in something else, as the colour of each pixel in any given photo can be changed just slightly to give the desired operation. Such changed pictures are often indistinguishable from the originals.
Challenge
Write a program or function that takes an image and a string of brainf*** code as input and produces the original image with the brainf*** code encoded in it.
To do this, take each pixel in the original image and replace it with the RGB
value closest to the colour of the original pixel that evaluates to the correct
Braincopter instruction. Colour difference for the purposes of this challenge is
defined as abs(R1 - R2) + abs(G1 - G2) + abs(B1 - B2)
. In the case of a tie in
which two colours resolving to the same Braincopter command are equally close to
the original colour, either can be chosen.
For example, if the original colour is #FF8040
and needs to be modified to
produce a '1' instruction in Braincopter, #FF7F40
should be chosen.
Braincopter will exit when the instruction pointer runs off the edge of the image, so we'll keep it on the image by using the 8 and 9 commands (rotate instruction pointer clockwise and counterclockwise respectively). It's easiest to explain the format of the encoding with an example.
For the input +[[->]-[-<]>-]>.>>>>.<<<<-.>>-.>.<<.>>>>-.<<<<<++.>>++.
and an
8x9 image, the instructions will be laid out like this (using Brainf*** commands
instead of the Braincopter equivalents, and unicode representations of the turns
):
+ [ [ - > ] - ↲
↳ - > ] < - [ ↲
↳ ] > . > > > ↲
↳ < < < < . > ↲
↳ - . > > - . ↲
↳ > . < < . > ↲
↳ > > > - . < ↲
↳ + + < < < < ↲
↳ . > > + + . N
(Where N is a NOP.) As you can see, control flow travels from left to right until it hits the first set of clockwise turns, then travels right to left across the second row, et cetera. Your program must use this control flow. The brainf*** input will always be able to fit in the image; however, you may not assume it will always fit the image exactly. If it's too small, pad it with NOPs; the Braincopter code must still have the clockwise/counterclockwise turns on either side, though.
You may assume that the Brainf*** input will contain only the eight characters
><+-,.[]
.
Use the official Braincopter interpreter to test the outputs of your program or function.
Test cases
Input:
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
Example output:
Braincopter output:
Hello World!
Input:
>++++++++++[<++++++++++>-]>>>>>>>>>>>>>>>>++++[>++++<-]>[<<<<<<<++>+++>++++>++++++>+++++++>+++++++>++++>-]<++<+++++<++++++++++<+++++++++<++++++<<<<<<<<<<<<<[>+>+>[-]>>>>[-]>[-]<<<<<<<[>>>>>>+>+<<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]+>---[<->[-]]<[>>>>>>.>.>..<<<<<<<<<<<<+<<[-]>>>>>>-]<<<<<[>>>>>+>+<<<<<<-]>>>>>[<<<<<+>>>>>-]+>-----[<->[-]]<[>>>>>>>>>>.<.<..<<<<<<<<<<<<+<[-]>>>>>-]<+>[-]>[-]>[-]<<<[>+>+>+<<<-]>[<+>-]+>----------[<->[-]]<[<<+>[-]>-]>[-]>[-]<<<<[>>+>+>+<<<<-]>>[<<+>>-]+>----------[<->[-]]<[<<<+>[-]>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>[<<<<+>>>>-]+>[<->[-]]<[[-]>[-]<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<[>++++++++[<++++++>-]<.-.[-]][-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<[>++++++++[<++++++>-]<.[-]][-]>[-]<<[>+>+<<-]>>[<<+>>-]++++++++[<++++++>-]<.[-]]>>>>.<<<<<<<<<<<-]
Output:
Braincopter output:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17...
Input:
>>+[>>[>]>+>,[>>++++[>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[>[>>]>>>]>[>>]>>>[>[>>]>>>]>[>]<-<[<]<<<<<[<<]>[<<<<<[<<]>]<<<<<[<<]>[<<<<<[<<]>]<<[<]<[<]<]<]>[->>[>]>[>]>>>>>[>[>>]>>>]>[>>]>>>[>[>>]>>>]>[>]+[<]<<<<<[<<]>[<<<<<[<<]>]<<<<<[<<]>[<<<<<[<<]>]<<[<]<[<]<]>>[>]>[>]>>>>>[>[>>]>>>]>[>>]>>>[>[>>]>>>]>]<<<<<<[<<]>[<<<<<[<<]>]<<<<<[<<]>[<<<<<[<<]>]<<[<]>[<+>-]>[>]>>>>>[>[>>]>>>]<<]<<<[<<]>[<<<<<[<<]>]<<[<]<[<]<]>>[>]>]
Output:
Braincopter output:
This is a Brainf*** self-interpreter. It separates code from input by !
;
for example, the input ,[.,]!Hello, World!\0
will output Hello, World!
,
assuming the \0
was replaced by a null byte.
Scoring
This is code-golf, so the shortest answer wins. Happy golfing!