diff -r 7e00d2b13b04 -r 25d9c3b2bc99 solutions5/bf.scala --- a/solutions5/bf.scala Wed Aug 12 00:56:20 2020 +0100 +++ b/solutions5/bf.scala Sun Aug 23 14:39:58 2020 +0100 @@ -99,6 +99,13 @@ if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) case ']' => if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) + + // new commands + case '@' => (pc + 1, mp, write(mem, sread(mem, mp), sread(mem, mp - 1))) + case '*' => (pc + 1, mp, write(mem, mp, sread(mem, mp) * sread(mem, mp -1))) + case '#' => { println(s"$mp: ${sread(mem, mp)}"); (pc + 1, mp, mem) } + + case _ => (pc + 1, mp, mem) } compute(prog, new_pc, new_mp, new_mem) @@ -121,7 +128,7 @@ // clears the 0-cell //run("[-]", Map(0 -> 100)) // Map will be 0 -> 0 -// copies content of the 0-cell to 1-cell +// moves content of the 0-cell to 1-cell //run("[->+<]", Map(0 -> 10)) // Map will be 0 -> 0, 1 -> 10 @@ -249,3 +256,25 @@ } +def time_needed[T](n: Int, code: => T) = { + val start = System.nanoTime() + for (i <- 0 until n) code + val end = System.nanoTime() + (end - start)/(n * 1.0e9) +} + +import CW10a._ + +// draws the Sierpinski triangle +run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[ + ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< + ]>.>+[>>]>+]""") + + +println(run("""++++++++++>+***""")) + +println(run("""+++>+!+!+!+!+!""")) + + +println(time_needed(2, run(load_bff("a.bf")))) +