Say I have a pile of boxes:
AAA
BBB
I can create it in two steps:
BBB
↓
(empty)
---
AAA
↓
BBB
But with this:
AB
BA
I need three steps:
A
↓
(empty pile)
---
BB
↓
A
---
A
↓
B
BA
Your challenge
Given a pile of boxes, output the minimum number of steps needed to create that pile.
A step consists of dropping a row of boxes of the same type onto the current pile.
For example, AAA
can be dropped as a step, so can A A
, but A B
and
A
AAA
can't.
Input can be as a matrix, text, whatever. The input will always be a solid block - nothing like
BB
AAA
Will ever be inputted, so you don't have to parse spaces / empty values.
Scoring
This is code-golf, shortest wins!
Testcases
AB
BA
=> 3
A
B
C
D
E
=> 5
AAA
AAA
AAA
=> 3
ABCAB
DEACB
=> 7
POTATOS
DOBEYUM
=> 12