Brainfuck, 579 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 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.
(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.
++>>++<<--
should output>>++<<
, and that wasn't covered. Please add more test cases. \$\endgroup\$+++>-<+>---<
? It can be shortened to avoid unnecessary pointer movement, but the expected output leaves it unchanged. My understanding based on looking at both the question and the answers is that Doorknob is cool with the spec being taken loosely; we must eliminate any no-op contiguous+-><
sequences as explicitly stated, and beyond that it is permissible to do extra minifying as in your example++>>++<<--
, and we can also make rearrangements as long as they don't change the functionality of the code, e.g.>+<+
into+>+<
. \$\endgroup\$+>-<->+<
. (This may be the trickiest and hardest one to implement.) Make sure you don't get any false positives, like+>-<+>-<
, which should not be removed." - this is kind of vague \$\endgroup\$