@Override
    public Object solve() {

        Row[][] rows = new Row[16][];
        
        for (int i = 0; i < 8; ++i) {
            pattern(i, 0,
                    rows[2 * i] = new Row[pow(3, i + 1)],
                    rows[2 * i + 1] = new Row[pow(3, i + 1)], 0);
        }
        
        for (int i = 1; i < 15; ++i) {
            for (int j = 0; j < rows[i - 1].length; ++j) {
                for (int k = 0; k < rows[i].length; ++k) {
                    if (isNext(rows[i - 1][j].pattern, rows[i][k].pattern, (i & 1) != 0)) {
                        rows[i - 1][j].next.add(rows[i][k]);
                    }
                }
            }
        }
        for (int i = 14; i-- > 0; ) {
            for (Row row : rows[i]) {
                row.count = 0;
                for (Row n : row.next) {
                    row.count += n.count;
                }
            }
        }
        
        return rows[0][0].count * 3;
    }