equal
deleted
inserted
replaced
1 package object abacus { |
1 package object abacus { |
2 |
2 |
|
3 import scala.annotation.tailrec |
3 import lib._ |
4 import lib._ |
4 |
5 |
5 // Abacus instructions |
6 // Abacus instructions |
6 sealed abstract class AInst |
7 sealed abstract class AInst |
7 case class Inc(n: Int) extends AInst |
8 case class Inc(n: Int) extends AInst |
49 case n => steps(step(cf), n - 1) |
50 case n => steps(step(cf), n - 1) |
50 } |
51 } |
51 |
52 |
52 def steps(regs: Regs, n: Int) : AConfig = steps(AConfig(0, regs), n) |
53 def steps(regs: Regs, n: Int) : AConfig = steps(AConfig(0, regs), n) |
53 |
54 |
54 def run(cf: AConfig) : AConfig = { |
55 @tailrec |
|
56 final def run(cf: AConfig) : AConfig = { |
55 if (cf.s >= p.length || cf.s < 0) cf else run(step(cf)) |
57 if (cf.s >= p.length || cf.s < 0) cf else run(step(cf)) |
56 } |
58 } |
57 |
59 |
58 def run(regs: Regs): Regs = run(AConfig(0, regs)).regs |
60 def run(regs: Regs): Regs = run(AConfig(0, regs)).regs |
59 } |
61 } |