Commit 2b53c7a0 authored by Kevin Turner's avatar Kevin Turner

this still enforces the instruction signature, but is much more compact

parent 0ac18e46
......@@ -2,89 +2,25 @@ package net.keturn.advent2018
@Suppress("EnumEntryName", "SpellCheckingInspection")
enum class ProcessorInstructions {
addr {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = registers[a] + registers[b]
}
},
addi {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = registers[a] + b
}
},
multr {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = registers[a] * registers[b]
}
},
multi {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = registers[a] * b
}
},
banr {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = registers[a] and registers[b]
}
},
bani {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = registers[a] and b
}
},
borr {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = registers[a] or registers[b]
}
},
bori {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = registers[a] or b
}
},
setr {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = registers[a]
}
},
seti {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = a
}
},
gtir {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = if (a > registers[b]) 1 else 0
}
},
gtri {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = if (registers[a] > b) 1 else 0
}
},
gtrr {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = if (registers[a] > registers[b]) 1 else 0
}
},
eqir {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = if (a == registers[b]) 1 else 0
}
},
eqri {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = if (registers[a] == b) 1 else 0
}
},
eqrr {
override fun invoke(registers: IntArray, a: Int, b: Int, c: Int) {
registers[c] = if (registers[a] == registers[b]) 1 else 0
}
};
abstract operator fun invoke(registers: IntArray, a: Int, b: Int, c: Int)
enum class ProcessorInstructions(private val f: (IntArray, Int, Int, Int) -> Unit) {
addr({ registers, a, b, c -> registers[c] = registers[a] + registers[b] }),
addi({ registers, a, b, c -> registers[c] = registers[a] + b }),
multr({ registers, a, b, c -> registers[c] = registers[a] * registers[b] }),
multi({ registers, a, b, c -> registers[c] = registers[a] * b }),
banr({ registers, a, b, c -> registers[c] = registers[a] and registers[b] }),
bani({ registers, a, b, c -> registers[c] = registers[a] and b }),
borr({ registers, a, b, c -> registers[c] = registers[a] or registers[b] }),
bori({ registers, a, b, c -> registers[c] = registers[a] or b }),
setr({ registers, a, _, c -> registers[c] = registers[a] }),
seti({ registers, a, _, c -> registers[c] = a }),
gtir({ registers, a, b, c -> registers[c] = if (a > registers[b]) 1 else 0 }),
gtri({ registers, a, b, c -> registers[c] = if (registers[a] > b) 1 else 0 }),
gtrr({ registers, a, b, c -> registers[c] = if (registers[a] > registers[b]) 1 else 0 }),
eqir({ registers, a, b, c -> registers[c] = if (a == registers[b]) 1 else 0 }),
eqri({ registers, a, b, c -> registers[c] = if (registers[a] == b) 1 else 0 }),
eqrr({ registers, a, b, c -> registers[c] = if (registers[a] == registers[b]) 1 else 0 });
operator fun invoke(registers: IntArray, a: Int, b: Int, c: Int) = f(registers, a, b, c)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment