Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package angabe
- import scala.language.postfixOps
- import scala.sys.process.stringToProcess
- object SnakePuzzle:
- def main(args: Array[String]): Unit =
- //val results = showSteps(whiteBlueColorCycle)(Puzzle.StandardPuzzle)
- println(checkValidity(snake))
- val outputDirectory = "D:\\tmp\\sketchTest" // Modify this to fit your system. On Windows use something like "D:\\tmp\\sketchTest"
- checkPrerequisites(outputDirectory)
- //runSketchPdfLatex(results, outputDirectory)
- /////////////////////////////////////////////////////////////////////////////
- // Aufgabe 1
- type Snake = List[Int]
- val snake = List(3, 2, 2, 3, 2, 3, 2, 2, 3, 3, 2, 2, 2, 3, 3, 3, 3)
- def checkValidity(s: Snake): Boolean =
- s.reduceRight(_+_)-(s.length-1) == 27
- /////////////////////////////////////////////////////////////////////////////
- // Aufgabe 2
- type Direction = (Int, Int, Int)
- type Position = (Int, Int, Int)
- type Section = List[Position]
- type Solution = List[Section]
- def inCube(n: Int)(p: Position): Boolean =
- if (p(0) >= 1 && p(0) <= n) then
- if (p(1) >= 1 && p(1) <= n) then
- if (p(2) >= 1 && p(2) <= n) then
- true
- else false
- else false
- else false
- def section(start: Position, dir: Direction, len: Int): Section =
- val newPos: Position = (start(0)+dir(0),start(1)+dir(1),start(2)+dir(2))
- section(newPos,dir,len-1):::List(start)
- def newDirs(dir: Direction): List[Direction] =
- /////////////////////////////////////////////////////////////////////////////
- // Aufgabe 3
- enum Puzzle(List[Section],(f: A->B),)
- /////////////////////////////////////////////////////////////////////////////
- // Aufgabe 4
- def extend(p: Puzzle, soln: Solution, dir: Direction, len: Int): Option[Solution] = ???
- def solutions(p: Puzzle): List[Solution] = ???
- /////////////////////////////////////////////////////////////////////////////
- // Aufgabe 5
- def steps(soln: Solution): List[Section] = ???
- def cube2sk(col: String, a: Position): List[String] = ???
- def showCubes(colorCycle: String => String)(ps: List[Position]): String = ???
- val colors = List("black", "blue", "brown", "cyan", "darkgray", "gray",
- "green", "lightgray", "lime", "magenta", "olive", "orange",
- "pink", "purple", "red", "teal", "violet", "white", "yellow")
- /////////////////////////////////////////////////////////////////////////////
- // Hilfsfunktionen
- import sys.process._
- import scala.language.postfixOps
- import java.io._
- // Setup view and guides
- val prefixSketchCode = """put { view((6,6,12), (0,1,-10)) then perspective(20) } {
- repeat {4, translate([0,0,1])}
- {repeat {4, translate([0,1,0])} {line[style=dotted](0,0,0)(3,0,0)}}
- repeat {4, translate([0,0,1])}
- {repeat {4, translate([1,0,0])} {line[style=dotted](0,0,0)(0,3,0)}}
- repeat {4, translate([0,1,0])}
- {repeat {4, translate([1,0,0])} {line[style=dotted](0,0,0)(0,0,3)}}
- """
- val postfixSketchCode = """}
- global { set[scale=2,style=very thin] language tikz }
- """
- def getCommandPostfix = if System.getProperty("os.name").startsWith("Windows") then ".exe" else ""
- def checkPrerequisites(path: String): Unit =
- val dir = new File(path)
- if !dir.exists() || !dir.isDirectory() then
- println(s"Error: ${dir.getAbsolutePath} does not exist or is not a directory. Please make sure that the target directory exists and is empty.")
- System.exit(1)
- if dir.list().length > 0 then
- println(s"Error: ${dir.getAbsolutePath} contains ${dir.list().length} files, please delete all files from the folder.")
- System.exit(1)
- try
- s"sketch${getCommandPostfix} -h" !
- catch
- case e: java.io.IOException =>
- println(s"Error: Could not find sketch${getCommandPostfix}, is it in your PATH?")
- System.exit(1)
- try
- s"pdflatex${getCommandPostfix} --version" !
- catch
- case e: java.io.IOException =>
- println(s"Error: Could not find pdflatex${getCommandPostfix}, is it in your PATH?")
- System.exit(1)
- def runSketchPdfLatex(steps: List[String], path: String): Unit =
- checkPrerequisites(path) // doublecheck but better save then sorry
- val dir = new File(path)
- if steps.length > 100 then
- println(s"Error: Executing this will result in many files (${steps.length}) remove this check if you really want to continue!")
- System.exit(1)
- def printStep(id: Int, step: String): Int =
- val longId = f"${id}%03d"
- val file = new File(dir, longId)
- val pw = new PrintWriter(file)
- pw.write(prefixSketchCode)
- pw.write(step)
- pw.write(postfixSketchCode)
- pw.close()
- s"sketch${getCommandPostfix} ${file.getAbsolutePath} -T -o ${file.getAbsolutePath}.tex" !
- s"pdflatex${getCommandPostfix} -output-directory ${path} ${file.getAbsolutePath}.tex" !
- id + 1
- steps.foldRight(1)((s, i) => printStep(i, s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement