Advertisement
Swingstone

Untitled

Dec 7th, 2022
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.95 KB | None | 0 0
  1. package angabe
  2.  
  3. import scala.language.postfixOps
  4. import scala.sys.process.stringToProcess
  5.  
  6. object SnakePuzzle:
  7.  
  8.   def main(args: Array[String]): Unit =
  9.     //val results = showSteps(whiteBlueColorCycle)(Puzzle.StandardPuzzle)
  10.     println(checkValidity(snake))
  11.     val outputDirectory = "D:\\tmp\\sketchTest" // Modify this to fit your system. On Windows use something like "D:\\tmp\\sketchTest"
  12.     checkPrerequisites(outputDirectory)
  13.     //runSketchPdfLatex(results, outputDirectory)
  14.  
  15.   /////////////////////////////////////////////////////////////////////////////
  16.   // Aufgabe 1
  17.  
  18.   type Snake = List[Int]
  19.   val snake = List(3, 2, 2, 3, 2, 3, 2, 2, 3, 3, 2, 2, 2, 3, 3, 3, 3)
  20.  
  21.   def checkValidity(s: Snake): Boolean =
  22.     s.reduceRight(_+_)-(s.length-1) == 27
  23.  
  24.   /////////////////////////////////////////////////////////////////////////////
  25.   // Aufgabe 2
  26.   type Direction = (Int, Int, Int)
  27.   type Position = (Int, Int, Int)
  28.   type Section = List[Position]
  29.   type Solution = List[Section]
  30.  
  31.   def inCube(n: Int)(p: Position): Boolean =
  32.     if (p(0) >= 1 && p(0) <= n) then
  33.       if (p(1) >= 1 && p(1) <= n) then
  34.         if (p(2) >= 1 && p(2) <= n) then
  35.           true
  36.         else false
  37.       else false
  38.     else false
  39.  
  40.  
  41.   def section(start: Position, dir: Direction, len: Int): Section =
  42.     val newPos: Position = (start(0)+dir(0),start(1)+dir(1),start(2)+dir(2))
  43.     section(newPos,dir,len-1):::List(start)
  44.   def newDirs(dir: Direction): List[Direction] =
  45.  
  46.  
  47.   /////////////////////////////////////////////////////////////////////////////
  48.   // Aufgabe 3
  49.  
  50.   enum Puzzle(List[Section],(f: A->B),)
  51.  
  52.   /////////////////////////////////////////////////////////////////////////////
  53.   // Aufgabe 4
  54.  
  55.   def extend(p: Puzzle, soln: Solution, dir: Direction, len: Int): Option[Solution] = ???
  56.  
  57.   def solutions(p: Puzzle): List[Solution] = ???
  58.  
  59.   /////////////////////////////////////////////////////////////////////////////
  60.   // Aufgabe 5
  61.  
  62.   def steps(soln: Solution): List[Section] = ???
  63.  
  64.   def cube2sk(col: String, a: Position): List[String] = ???
  65.  
  66.   def showCubes(colorCycle: String => String)(ps: List[Position]): String = ???
  67.  
  68.   val colors = List("black", "blue", "brown", "cyan", "darkgray", "gray",
  69.     "green", "lightgray", "lime", "magenta", "olive", "orange",
  70.     "pink", "purple", "red", "teal", "violet", "white", "yellow")
  71.  
  72.   /////////////////////////////////////////////////////////////////////////////
  73.   // Hilfsfunktionen
  74.   import sys.process._
  75.   import scala.language.postfixOps
  76.   import java.io._
  77.  
  78.   // Setup view and guides
  79.   val prefixSketchCode = """put { view((6,6,12), (0,1,-10)) then perspective(20) } {
  80.    repeat {4, translate([0,0,1])}
  81.   {repeat {4, translate([0,1,0])} {line[style=dotted](0,0,0)(3,0,0)}}
  82.    repeat {4, translate([0,0,1])}
  83.   {repeat {4, translate([1,0,0])} {line[style=dotted](0,0,0)(0,3,0)}}
  84.    repeat {4, translate([0,1,0])}
  85.   {repeat {4, translate([1,0,0])} {line[style=dotted](0,0,0)(0,0,3)}}
  86.    """
  87.  
  88.   val postfixSketchCode = """}
  89.    global { set[scale=2,style=very thin] language tikz }
  90.    """
  91.  
  92.   def getCommandPostfix = if System.getProperty("os.name").startsWith("Windows") then ".exe"  else  ""
  93.  
  94.   def checkPrerequisites(path: String): Unit =
  95.     val dir = new File(path)
  96.     if !dir.exists() || !dir.isDirectory() then
  97.       println(s"Error: ${dir.getAbsolutePath} does not exist or is not a directory. Please make sure that the target directory exists and is empty.")
  98.       System.exit(1)
  99.     if dir.list().length > 0 then
  100.       println(s"Error: ${dir.getAbsolutePath} contains ${dir.list().length} files, please delete all files from the folder.")
  101.       System.exit(1)
  102.  
  103.     try
  104.       s"sketch${getCommandPostfix} -h" !
  105.     catch
  106.       case e: java.io.IOException =>
  107.         println(s"Error: Could not find sketch${getCommandPostfix}, is it in your PATH?")
  108.         System.exit(1)
  109.  
  110.     try
  111.       s"pdflatex${getCommandPostfix} --version" !
  112.     catch
  113.       case e: java.io.IOException =>
  114.         println(s"Error: Could not find pdflatex${getCommandPostfix}, is it in your PATH?")
  115.         System.exit(1)
  116.  
  117.   def runSketchPdfLatex(steps: List[String], path: String): Unit =
  118.     checkPrerequisites(path) // doublecheck but better save then sorry
  119.     val dir = new File(path)
  120.  
  121.     if steps.length > 100 then
  122.       println(s"Error: Executing this will result in many files (${steps.length}) remove this check if you really want to continue!")
  123.       System.exit(1)
  124.  
  125.     def printStep(id: Int, step: String): Int =
  126.       val longId = f"${id}%03d"
  127.       val file = new File(dir, longId)
  128.       val pw = new PrintWriter(file)
  129.       pw.write(prefixSketchCode)
  130.       pw.write(step)
  131.       pw.write(postfixSketchCode)
  132.       pw.close()
  133.  
  134.       s"sketch${getCommandPostfix} ${file.getAbsolutePath} -T -o ${file.getAbsolutePath}.tex" !
  135.  
  136.       s"pdflatex${getCommandPostfix} -output-directory ${path} ${file.getAbsolutePath}.tex" !
  137.  
  138.       id + 1
  139.     steps.foldRight(1)((s, i) => printStep(i, s))
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement