## Brainfuck, 603 bytes ,[<<+>>>>+<<[[<+>>+<-]++++++[>-------<-]>-[-[-[-[--------------[--[<+++++[>----- -<-]>+[--[<<[-]>>-]]<]>[>>-<<<<<[-]<[<]<<<[<]>>>>>>>[>[<]<[-[+<]]]+[->+]>>>>+>[< -]<[>+<-<]>]<]>[<<<[-]-[<]>>>>>>>>>>>[<]<<<<<<<[>[<]<[-[+<]]]+[-<+]<<<+<[>-<<<]> [-<+<]]]<]>[+>[-<]<[<<]<[-]>>]]<]+>[-[<-]<[>+>+<<-<]<[-]>+>]<<[>-]>[,>]<]<+<[>]> [>>>[<<<<[<+>->>>>>>]<<<]+>[<-]<[->>>>>>>[>>>[-<]>>>>]<]>]>[<<<[<+[-->>]>[-[.[-] ]<]<[-]]>>[<]>[<<++++++[>+++++++<-]>+>>[<<.>>-]<<++>-[<.>-]+++[<+++++>-]+<<<<<<+ >[<<[>->>>>>.[[-]<<<<]<<<+>>>]>[->->>>>[-]]]<[->+[>>>>>]>>[<]<<<<<<<<[[-]<]>[++. [-]>>>>>>>]<]>]>]>[>>>>>>>]+[-<<<<<[-]<<],] With formatting and some comments: , [ <<+>> >>+<< [ [<+> >+<-] ++++++[>-------<-] >- [ not plus - [ not comma - [ not minus - [ not period -------------- [ not less than -- [ not greater than <+++++[>------<-]>+ [ not open bracket -- [ not close bracket <<[-]>>- ] ] < ] > [ greater than >>-<< <<<[-]<[<]<<<[<] >>>>>>> [ >[<] <[-[+<]] ] +[->+] >>>>+>[<-] <[>+<-<] > ] < ] > [ less than <<<[-]-[<] >>>> >>>>>>>[<] <<<<<<< [ >[<] <[-[+<]] ] +[-<+] <<<+<[>-<<<] >[-<+<] ] ] < ] > [ minus +>[-<] <[<<] <[-]>> ] ] < ] +> [ plus -[<-] <[>+>+<<-<] <[-]>+> ] << [ comma or period or bracket >- ] >[,>] < ] comma or period or bracket or eof <+< [ start and end same cell > ] > [ >>> [ <<<<[<+>->>>>>>]<<< ] +> [ start left of end <- ] < [ start right of end ->>>>>>> [>>>[-<]>>>>] < ] > ] > [ <<< [ <+[-->>] >[-[.[-]]<] <[-] ] >>[<] > [ <<++++++[>+++++++<-]>+>> [<<.>>-] <<++>-[<.>-] +++[<+++++>-] +<<<<< <+> [ << [ go left >->>>>>. [[-]<<<<] <<<+>>> ] > [ toggle left right ->->>>>[-] ] ] < [ toggle right left ->+[>>>>>]>>[<] <<<<<<<< [ [-]< ] > [ go right ++.[-] >>>>>>> ] < ] > ] > ] >[>>>>>>>] +[-<<<<<[-]<<] , ] This uses the same approach as Keith Randall's solution, minifying all contiguous sequences of `+-<>` optimally by simulation. For example, `+++>-<+>---<` becomes `++++>----<` and `>+<+<<+>+<->>>>` becomes `+<+>>+>`. [Try it online.](http://brainfuck.tryitonline.net/#code=LFs8PCs-Pj4-Kzw8W1s8Kz4-KzwtXSsrKysrK1s-LS0tLS0tLTwtXT4tWy1bLVstWy0tLS0tLS0tLS0tLS0tWy0tWzwrKysrK1s-LS0tLS0tPC1dPitbLS1bPDxbLV0-Pi1dXTxdPls-Pi08PDw8PFstXTxbPF08PDxbPF0-Pj4-Pj4-Wz5bPF08Wy1bKzxdXV0rWy0-K10-Pj4-Kz5bPC1dPFs-KzwtPF0-XTxdPls8PDxbLV0tWzxdPj4-Pj4-Pj4-Pj5bPF08PDw8PDw8Wz5bPF08Wy1bKzxdXV0rWy08K108PDwrPFs-LTw8PF0-Wy08KzxdXV08XT5bKz5bLTxdPFs8PF08Wy1dPj5dXTxdKz5bLVs8LV08Wz4rPis8PC08XTxbLV0-Kz5dPDxbPi1dPlssPl08XTwrPFs-XT5bPj4-Wzw8PDxbPCs-LT4-Pj4-Pl08PDxdKz5bPC1dPFstPj4-Pj4-Pls-Pj5bLTxdPj4-Pl08XT5dPls8PDxbPCtbLS0-Pl0-Wy1bLlstXV08XTxbLV1dPj5bPF0-Wzw8KysrKysrWz4rKysrKysrPC1dPis-Pls8PC4-Pi1dPDwrKz4tWzwuPi1dKysrWzwrKysrKz4tXSs8PDw8PDwrPls8PFs-LT4-Pj4-LltbLV08PDw8XTw8PCs-Pj5dPlstPi0-Pj4-Wy1dXV08Wy0-K1s-Pj4-Pl0-Pls8XTw8PDw8PDw8W1stXTxdPlsrKy5bLV0-Pj4-Pj4-XTxdPl0-XT5bPj4-Pj4-Pl0rWy08PDw8PFstXTw8XSxd&input=KysrKysrWy0-KysrKysrPF0-LiAgIHByaW50cyBhICQKWy1dPCAgICAgICAgICAgICAgICAgIHJlc2V0cyB0YXBlCj4sWz4sXTxbLjxdICAgICAgICAgICByZXZlcnNlcyBOVUwgdGVybWluYXRlZCBpbnB1dCBzdHJpbmcKKystLT4-PDwgICAgICAgICAgICAgIGRvZXMgbm90aGluZw) (If a simulated cell's absolute value gets close to 256, there will be overflow issues.) The overall structure is while not EOF: while not EOF and next char not in ",.[]": process char print minified sequence (followed by the char in ",.[]" if applicable) The tape is divided into 7-cell nodes; at the beginning of the inner loop, the memory layout is `0 s 0 c 0 a b` where `s` is a boolean flag for start cell, `c` is the current character, `a` is the negative part of the simulated cell value (plus one), and `b` is the positive part of the simulated cell value. When the minified sequence is being printed, the memory layout is `d n e 0 0 a b` where `d` is a boolean flag for direction, `a` and `b` are as before (but become one/zero when printed), and `n` and `e` are only nonzero for the end node; `n` is related to how many times the node has been seen, and `e` is the value of the char that halted the inner loop (plus one). Originally I considered keeping track of more information per node: leftmost and rightmost node as boolean flags, and node's position in relation to the start and end nodes. But we can avoid that by looking at neighboring cells when needed, and by doing left and right scans in order to find the start node. When printing the minified sequence and deciding how to move the simulated pointer, we can take a general approach: start by moving away from the end node (in an arbitrary direction if start and end nodes are the same), turn around at leftmost and rightmost nodes, and stop based on the number of times the end node has been seen: 3 times if the start and end nodes are the same, otherwise 2.