--- a/solutions5/bfc.scala Wed Aug 12 00:56:20 2020 +0100
+++ b/solutions5/bfc.scala Sun Aug 23 14:39:58 2020 +0100
@@ -60,7 +60,6 @@
case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
- case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte))
case '[' =>
if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem)
case ']' =>
@@ -144,8 +143,7 @@
case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
- case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte))
- case '[' =>
+ case '[' =>
if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
case ']' =>
if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
@@ -172,14 +170,14 @@
// that is write(mem, mp, 0).
//
// The easiest way to modify a string in this way is to use the regular
-// expression """[^<>+-.,\[\]]""", which recognises everything that is
+// expression """[^<>+-.\[\]]""", which recognises everything that is
// not a bf-command and replace it by the empty string. Similarly the
// regular expression """\[-\]""" finds all occurences of [-] and
// by using the Scala method .replaceAll you can repplace it with the
// string "0" standing for the new bf-command.
def optimise(s: String) : String =
- s.replaceAll("""[^<>+-.,\[\]]""","").replaceAll("""\[-\]""", "0")
+ s.replaceAll("""[^<>+-.\[\]]""","").replaceAll("""\[-\]""", "0")
def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = {
@@ -191,7 +189,6 @@
case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1))
case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1))
case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
- case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte))
case '[' =>
if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
case ']' =>
@@ -244,7 +241,6 @@
case ('[' :: cs, acc) => splice(cs, ('[', 1) :: acc)
case (']' :: cs, acc) => splice(cs, (']', 1) :: acc)
case ('.' :: cs, acc) => splice(cs, ('.', 1) :: acc)
- case (',' :: cs, acc) => splice(cs, (',', 1) :: acc)
case ('0' :: cs, acc) => splice(cs, ('0', 1) :: acc)
case (c :: cs, Nil) => splice(cs, List((c, 1)))
case (c :: cs, (d, n) :: acc) =>
@@ -278,8 +274,7 @@
case '+' => (pc + 2, mp, write(mem, mp, sread(mem, mp) + (pg(pc + 1) - '@')))
case '-' => (pc + 2, mp, write(mem, mp, sread(mem, mp) - (pg(pc + 1) - '@')))
case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) }
- case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte))
- case '[' =>
+ case '[' =>
if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
case ']' =>
if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem)
@@ -307,3 +302,10 @@
}
+
+
+import CW10b._
+println(time_needed(1, run(load_bff("collatz.bf"))))
+println(time_needed(1, run2(load_bff("collatz.bf"))))
+println(time_needed(1, run3(load_bff("collatz.bf"))))
+println(time_needed(1, run4(load_bff("collatz.bf"))))