Dark black ink has splattered all over your white sheet of printer paper! The obvious solution is to fold the paper so black and white parts meet and both become grey as the ink diffuses. Then unfold and refold until your paper is all equally grey.
Finding the best way to make these folds is your task in this coding challenge. This 40×20 grid of ones and zeros represents your ink splattered paper. Zeros are paper and ones are ink.
1 1 0 1 0 1 1 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 1 1 0 1 0 1
0 1 1 1 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 1 1 1 1 0 0 0 0 1 0
1 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 0 1 0 1 1 1 1 1 0 0 1 0 0 0 0 1 0 1 0
1 0 1 0 1 1 0 1 1 1 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 0 0 1 1 0 0 1
1 0 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 0 1 1 1 0 0 1 0 1 1 0 1 0 0 1 1 0 1 1 0 0 0 0
1 1 1 0 0 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 0 1 0 0 0 0 1 1 1 0 0
0 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 1 0 1 0 1 0 0 1 1 0 0 1 1 0 0 0 1 1 0
0 1 0 0 1 1 1 0 1 0 0 0 0 1 1 0 1 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0 0 0 1 1 1 1 0 1
0 1 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 1 0 1 1 1 0 1 0 1 0 1 1
1 1 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 0 0 0 0 0 1 1 1 0 1 0 0 0
1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 0 0 1 0 1 1 1 0 1 0 1 1 1 1 1 1 0 0
0 1 0 1 0 1 1 1 1 0 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 0 0 0 1 1 0 1 0 1 1 0 1 1 0 1
0 0 1 1 0 1 0 0 0 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0 1 1 0 1 1 0 1 0 0 0 0 1 1 0 0 0
1 0 0 1 1 0 1 0 1 0 0 1 0 1 0 1 1 1 0 0 1 0 1 1 0 1 1 1 1 0 1 0 0 1 0 1 1 1 1 1
1 0 1 1 0 0 0 1 1 0 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 1 0 1 0 0 0 1 0 0 1 1 0 1
1 1 1 0 1 0 0 1 1 0 0 0 0 1 0 0 1 0 0 1 1 1 1 0 1 1 0 1 0 0 0 0 1 0 0 1 1 0 0 1
0 0 0 1 1 1 0 0 1 1 0 1 0 0 1 1 1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0
1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 0 1 0 0 1 0 0 0 1 0 0 1 1 1 1 0 0 0 1 1 0 1 0
0 0 0 1 0 1 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 0 0 0 1 1 1 0 1 1 1 1 0 1 1 1 0 0 1 1
1 0 0 1 1 1 0 0 1 1 1 1 1 0 1 1 0 1 0 0 1 0 0 0 0 1 0 0 1 0 1 1 1 1 1 0 1 0 0 1
Only horizontal and vertical folds along the spaces between lines and columns are valid. When a fold is made the pairs of overlapping values are averaged. Folds are done one at a time and always unfolded. Folds only change the ink distribution, not the size of the paper.
Rn denotes folding the left edge of the grid to the right, starting after the nth column. Dn denotes folding the top edge of the grid downward fold, starting after the nth row. (n is 1-indexed, folds for n < 1 and n > row/column count are undefined.)
Example
Given this grid
0 1 1 1
0 0 0 0
0 0 0 0
a D1 fold means "fold the entire top row down then unfold".
0 0.5 0.5 0.5
0 0.5 0.5 0.5
0 0 0 0
Then an R2 will produce
0.25 0.5 0.5 0.25
0.25 0.5 0.5 0.25
0 0 0 0
and another R2 will not change anything.
Goal
Your goal is to write an algorithm that finds the best ink-spreading folding sequence for the 40×20 grid using exactly 8 folds. The folds may be any combination of Rs or Ds.
Scoring
The score of your submission is the sum of the absolute differences between each of your final grid's values and the grid's average (which is 0.5 in this case). Lower scores are better. A score of 0 would indicate a 0.5 in every spot, which is optimal. The grid's current score is maxed out at 400.
You must report your 8-step folding sequence with your answer (something like R3 D14 R15 D9 R26 R5 R3 D5). I need it to verify that your algorithm does what it claims. Naturally you should not copy someone else's sequence. Sequences only belong to the person who first created them.
Clarifications
It would be great if your algorithm tried to optimize any grid but it does not have to. Optimizing the given 40×20 grid is the only requirement.
Officially, all you need to win is the smallest-scoring 8-step folding sequence that has not already been posted. But I won't look too kindly on you if all you're doing is tinkering with other people's hard-earned solutions. This challenge is intended to inspire creativity, not underhandedness.
The grid should never contain negative numbers.