Moved some code in the IntCodeProgram around
This commit is contained in:
@@ -21,34 +21,6 @@ fun loadProgramData(inputPath: String): LongArray {
|
||||
return line(inputPath).split(",").map { it.toLong() }.toLongArray()
|
||||
}
|
||||
|
||||
class Instruction(
|
||||
val paramModes: Array<ParamMode>,
|
||||
val opcode: OpCode,
|
||||
val pos: Int
|
||||
) {
|
||||
|
||||
companion object {
|
||||
fun parse(input: Long, pointer: Int, opcodes: Map<Long, OpCode>): Instruction {
|
||||
val opcode = opcodes[input % 100] ?: throw IllegalArgumentException("Unknown opcode for operation $input")
|
||||
val paramModes = Array<ParamMode>(opcode.paramCount) { ParamMode.POSITION }
|
||||
for (i in 0 until opcode.paramCount) {
|
||||
val paramMode = (input % pow10(i + 3)) / pow10(i + 2)
|
||||
paramModes[i] = when (paramMode) {
|
||||
0L -> ParamMode.POSITION
|
||||
1L -> ParamMode.IMMEDIATE
|
||||
2L -> ParamMode.RELATIVE
|
||||
else -> throw IllegalStateException("Unknown parameter mode: $paramMode")
|
||||
}
|
||||
}
|
||||
return Instruction(paramModes, opcode, pointer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum class ParamMode {
|
||||
POSITION, IMMEDIATE, RELATIVE
|
||||
}
|
||||
|
||||
class IntCodeProgram(val data: LongArray, var inputs: LongArray, val opCodes: Map<Long, OpCode> = FULL_OPCODES,
|
||||
var pointer: Int = 0) {
|
||||
|
||||
@@ -117,22 +89,40 @@ class IntCodeProgram(val data: LongArray, var inputs: LongArray, val opCodes: Ma
|
||||
}
|
||||
}
|
||||
|
||||
fun pow10(n: Int): Int {
|
||||
return when (n) {
|
||||
0 -> 1
|
||||
1 -> 10
|
||||
2 -> 100
|
||||
3 -> 1000
|
||||
4 -> 10000
|
||||
5 -> 100000
|
||||
6 -> 1000000
|
||||
7 -> 10000000
|
||||
8 -> 100000000
|
||||
9 -> 1000000000
|
||||
else -> throw IllegalArgumentException("Int overflowing with pow10($n)")
|
||||
class Instruction(
|
||||
val paramModes: Array<ParamMode>,
|
||||
val opcode: OpCode,
|
||||
val pos: Int
|
||||
) {
|
||||
|
||||
companion object {
|
||||
fun parse(input: Long, pointer: Int, opcodes: Map<Long, OpCode>): Instruction {
|
||||
val opcode = opcodes[input % 100] ?: throw IllegalArgumentException("Unknown opcode for operation $input")
|
||||
val paramModes = Array(opcode.paramCount) { ParamMode.POSITION }
|
||||
for (i in 0 until opcode.paramCount) {
|
||||
val paramMode = (input % pow10(i + 3)) / pow10(i + 2)
|
||||
paramModes[i] = when (paramMode) {
|
||||
0L -> ParamMode.POSITION
|
||||
1L -> ParamMode.IMMEDIATE
|
||||
2L -> ParamMode.RELATIVE
|
||||
else -> throw IllegalStateException("Unknown parameter mode: $paramMode")
|
||||
}
|
||||
}
|
||||
return Instruction(paramModes, opcode, pointer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum class ParamMode {
|
||||
POSITION, IMMEDIATE, RELATIVE
|
||||
}
|
||||
|
||||
data class OpCode(val code: Long, val paramCount: Int, val operation: (instruction: Instruction, intCodeProgram: IntCodeProgram) -> OperationResult)
|
||||
|
||||
data class OperationResult(val moveToNextInstruction: Boolean = true, val output: Long? = null)
|
||||
|
||||
private val VOID = OperationResult()
|
||||
|
||||
fun add(instruction: Instruction, program: IntCodeProgram): OperationResult {
|
||||
program.setValue(instruction, 2, program.getValue(instruction, 0) + program.getValue(instruction, 1))
|
||||
return VOID
|
||||
@@ -187,10 +177,4 @@ fun equals(instruction: Instruction, program: IntCodeProgram): OperationResult {
|
||||
fun adjustRelativeBase(instruction: Instruction, program: IntCodeProgram): OperationResult {
|
||||
program.relativeBase += program.getValue(instruction, 0)
|
||||
return VOID
|
||||
}
|
||||
|
||||
data class OpCode(val code: Long, val paramCount: Int, val operation: (instruction: Instruction, intCodeProgram: IntCodeProgram) -> OperationResult)
|
||||
|
||||
data class OperationResult(val moveToNextInstruction: Boolean = true, val output: Long? = null)
|
||||
|
||||
private val VOID = OperationResult()
|
||||
}
|
||||
@@ -12,4 +12,20 @@ fun line(resourceFile: String): String {
|
||||
return lines(resourceFile).findFirst().get()
|
||||
}
|
||||
|
||||
fun pow10(n: Int): Int {
|
||||
return when (n) {
|
||||
0 -> 1
|
||||
1 -> 10
|
||||
2 -> 100
|
||||
3 -> 1000
|
||||
4 -> 10000
|
||||
5 -> 100000
|
||||
6 -> 1000000
|
||||
7 -> 10000000
|
||||
8 -> 100000000
|
||||
9 -> 1000000000
|
||||
else -> throw IllegalArgumentException("Int overflowing with pow10($n)")
|
||||
}
|
||||
}
|
||||
|
||||
class Utils
|
||||
|
||||
Reference in New Issue
Block a user