fork download
  1. /**
  2.  * author: Serkan Eryilmaz
  3.  */
  4.  
  5. object Origami {
  6.  
  7. type OrigamiSheet = List[List[List[Int]]]
  8.  
  9. def foldLeft(sheet: OrigamiSheet): OrigamiSheet = {
  10. sheet.map{line =>
  11. val (left, right) = line.splitAt(line.length/2)
  12. left.reverse.zip(right).map{
  13. case (a,b) =>
  14. a.reverse ::: b
  15. }
  16. }
  17. }
  18.  
  19. def foldRight(sheet: OrigamiSheet): OrigamiSheet = {
  20. sheet.map{line =>
  21. val (left, right) = line.splitAt(line.length/2)
  22. right.reverse.zip(left).map{
  23. case (a,b) =>
  24. a.reverse ::: b
  25. }
  26. }
  27. }
  28.  
  29. def foldTop(sheet: OrigamiSheet): OrigamiSheet = {
  30. val (top, bottom) = sheet.splitAt(sheet.length/2)
  31. top.reverse.zip(bottom).map{
  32. case (topLine,bottomLine) =>
  33. topLine.zip(bottomLine).map{
  34. case (top, bot) =>
  35. top.reverse ::: bot
  36. }
  37. }
  38. }
  39.  
  40. def foldBottom(sheet: OrigamiSheet): OrigamiSheet = {
  41. val (top, bottom) = sheet.splitAt(sheet.length/2)
  42. bottom.reverse.zip(top).map{
  43. case (topLine,bottomLine) =>
  44. bottomLine.zip(topLine).map{
  45. case (a, b) =>
  46. b.reverse ::: a
  47. }
  48. }
  49. }
  50.  
  51. private def doFold(sheet: OrigamiSheet, fold: String): OrigamiSheet = {
  52. if (fold.length > 0) {
  53. fold.head match {
  54. case 'L' =>
  55. doFold(foldLeft(sheet), fold.tail)
  56. case 'R' =>
  57. doFold(foldRight(sheet), fold.tail)
  58. case 'T' =>
  59. doFold(foldTop(sheet), fold.tail)
  60. case 'B' =>
  61. doFold(foldBottom(sheet), fold.tail)
  62. case _ =>
  63. println("unrecognized character")
  64. sheet
  65. }
  66. } else {
  67. sheet
  68. }
  69. }
  70.  
  71. /**
  72.   *
  73.   * @param foldType
  74.   * @return
  75.   */
  76. def fold(foldType: String): OrigamiSheet = {
  77. val n = 1 << (foldType.length / 2)
  78. val initial = (for (i <- 0 until n) yield {
  79. (for (j <- 1 to n) yield List((i*n)+j)).toList
  80. }).toList
  81. doFold(initial, foldType)
  82. }
  83.  
  84. def show(list: OrigamiSheet) {
  85. println(list.flatten.flatten.mkString(", "))
  86. }
  87. }
  88.  
  89. object Main {
  90. def main(args: Array[String]) {
  91. import Origami._
  92. show(fold("TLRB"))
  93. }
  94. }
Success #stdin #stdout 0.3s 247424KB
stdin
Standard input is empty
stdout
15, 3, 2, 14, 13, 1, 4, 16, 12, 8, 5, 9, 10, 6, 7, 11