Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's my attempt. It sounds pretty easy but as usual the devil is in the detail.</p> <p>First, <code>fold</code>:</p> <pre><code> type Matrix = IndexedSeq[IndexedSeq[IndexedSeq[Int]]] def initial(folds: Int): Matrix = { val sideLen = math.pow(2, folds / 2).toInt (1 to sideLen * sideLen) map (Vector(_)) grouped sideLen toIndexedSeq } def leftFold (m: Matrix): Matrix = m map { r =&gt; val (a, b) = r splitAt (r.size / 2) (a.reverse, b).zipped map (_.reverse ++ _) } def rightFold(m: Matrix): Matrix = m map { r =&gt; val (a, b) = r splitAt (r.size / 2) (b.reverse, a).zipped map (_.reverse ++ _) } def topFold (m: Matrix): Matrix = leftFold(m.transpose).transpose def bottomFold(m: Matrix): Matrix = rightFold(m.transpose).transpose def fold(input: String): Seq[Int] = { def go(in: String, m: Matrix): Seq[Int] = in match { case "" =&gt; m(0)(0) case s =&gt; go(s.tail, s.head match { case 'L' =&gt; leftFold(m) case 'R' =&gt; rightFold(m) case 'T' =&gt; topFold(m) case 'B' =&gt; bottomFold(m) }) } go(input, initial(input.length)) } </code></pre> <p>Second, <code>unfold</code>:</p> <pre><code> def unfold(input: Seq[Int]): String = { val m0: Matrix = Vector(Vector(Vector(input: _*))) val sideLen = math.sqrt(input.length).toInt def go(m: Matrix, out: String): String = { val tl = m.head.head val bl = m.last.head val tr = m.head.last if (m.length == sideLen &amp;&amp; m.head.length == sideLen) out.reverse else if (tl.head == tl.last - 1) go(leftUnfold(m), out + 'L') else if (tr.head == tr.last + 1) go(rightUnfold(m), out + 'R') else if (tl.head == tl.last - sideLen) go(topUnfold(m), out + 'T') else if (bl.head == bl.last + sideLen) go(bottomUnfold(m), out + 'B') else sys.error("no fold found " + m) } go(m0, "") } def leftUnfold (m: Matrix): Matrix = m map { r =&gt; val (a, b) = r.map(e =&gt; e.splitAt(e.size / 2)).unzip a.map(_.reverse).reverse ++ b } def rightUnfold(m: Matrix): Matrix = m map { r =&gt; val (a, b) = r.map(e =&gt; e.splitAt(e.size / 2)).unzip b ++ a.map(_.reverse).reverse } def topUnfold (m: Matrix): Matrix = leftUnfold (m.transpose).transpose def bottomUnfold(m: Matrix): Matrix = rightUnfold(m.transpose).transpose </code></pre> <p>I ran a few tests and it passed...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload