2022-11-22 17:40:22 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "math"
|
|
|
|
|
|
|
|
func (operation *Operation) solution () (solution int64, err error) {
|
|
|
|
var subSolution int64
|
|
|
|
|
|
|
|
switch operation.opcode {
|
|
|
|
case OpcodeAdd:
|
|
|
|
for _, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
solution += subSolution
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeSubtract:
|
|
|
|
if len(operation.operands) == 1 {
|
|
|
|
solution, err = operation.operands[0].Solution()
|
|
|
|
solution *= -1
|
|
|
|
if err != nil { return }
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else {
|
|
|
|
solution -= subSolution
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeMultiply:
|
|
|
|
solution = 1
|
|
|
|
for _, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
solution *= subSolution
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeDivide:
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else if subSolution == 0 {
|
|
|
|
err = ErrorDivideByZero
|
|
|
|
} else {
|
|
|
|
solution /= subSolution
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodePower:
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else {
|
|
|
|
solution = integerPower(solution, subSolution)
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeRoot:
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else {
|
|
|
|
solution = integerRoot (
|
|
|
|
solution, subSolution)
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeModulo:
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else {
|
|
|
|
if subSolution == 0 {
|
|
|
|
err = ErrorDivideByZero
|
|
|
|
} else {
|
|
|
|
solution %= subSolution
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeOr:
|
|
|
|
for _, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
solution |= subSolution
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeNot:
|
|
|
|
if len(operation.operands) != 1 {
|
|
|
|
err = ErrorWrongOperandCount
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
subSolution, err = operation.operands[0].Solution()
|
|
|
|
solution = ^subSolution
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeAnd:
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else {
|
|
|
|
solution &= subSolution
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeXor:
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else {
|
|
|
|
solution ^= subSolution
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeLeftShift:
|
|
|
|
if len(operation.operands) != 2 {
|
|
|
|
err = ErrorWrongOperandCount
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var left, right int64
|
|
|
|
left, err = operation.operands[0].Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
right, err = operation.operands[1].Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if right < 0 {
|
|
|
|
err = ErrorNegativeShiftAmount
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
solution = left << right
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeRightShift:
|
|
|
|
if len(operation.operands) != 2 {
|
|
|
|
err = ErrorWrongOperandCount
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var left, right int64
|
|
|
|
left, err = operation.operands[0].Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
right, err = operation.operands[1].Solution()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if right < 0 {
|
|
|
|
err = ErrorNegativeShiftAmount
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
solution = left >> right
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeMean:
|
|
|
|
if len(operation.operands) == 0 { break }
|
|
|
|
for _, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.Solution()
|
|
|
|
if err != nil { break }
|
|
|
|
solution += subSolution
|
|
|
|
}
|
|
|
|
solution /= int64(len(operation.operands))
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
default:
|
|
|
|
err = ErrorUnknownOpcode
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (operation *Operation) inexactSolution () (solution float64, err error) {
|
|
|
|
var subSolution float64
|
|
|
|
|
|
|
|
switch operation.opcode {
|
|
|
|
case OpcodeAdd:
|
|
|
|
for _, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.InexactSolution()
|
|
|
|
if err != nil { return }
|
|
|
|
solution += subSolution
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeSubtract:
|
|
|
|
if len(operation.operands) == 1 {
|
|
|
|
solution, err = operation.operands[0].InexactSolution()
|
|
|
|
solution *= -1
|
|
|
|
if err != nil { return }
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.InexactSolution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else {
|
|
|
|
solution -= subSolution
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeMultiply:
|
|
|
|
solution = 1
|
|
|
|
for _, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.InexactSolution()
|
|
|
|
if err != nil { return }
|
|
|
|
solution *= subSolution
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeDivide:
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.InexactSolution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else if subSolution == 0 {
|
|
|
|
err = ErrorDivideByZero
|
|
|
|
} else {
|
|
|
|
solution /= subSolution
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodePower:
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.InexactSolution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else {
|
|
|
|
solution = math.Pow(solution, subSolution)
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeRoot:
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.InexactSolution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else {
|
|
|
|
solution = math.Pow(solution, 1 / subSolution)
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
case OpcodeModulo:
|
|
|
|
for index, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.InexactSolution()
|
|
|
|
if err != nil { return }
|
|
|
|
if index == 0 {
|
|
|
|
solution = subSolution
|
|
|
|
} else {
|
|
|
|
if subSolution == 0 {
|
|
|
|
err = ErrorDivideByZero
|
|
|
|
} else {
|
|
|
|
solution = math.Mod (
|
|
|
|
solution,
|
|
|
|
subSolution)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-23 20:24:45 -07:00
|
|
|
case
|
|
|
|
OpcodeOr, OpcodeNot, OpcodeAnd, OpcodeXor, OpcodeLeftShift,
|
|
|
|
OpcodeRightShift:
|
|
|
|
err = ErrorWrongType
|
2022-11-22 17:40:22 -07:00
|
|
|
|
|
|
|
case OpcodeMean:
|
|
|
|
if len(operation.operands) == 0 { break }
|
|
|
|
for _, operand := range operation.operands {
|
|
|
|
subSolution, err = operand.InexactSolution()
|
|
|
|
if err != nil { break }
|
|
|
|
solution += subSolution
|
|
|
|
}
|
|
|
|
solution /= float64(len(operation.operands))
|
2022-11-23 08:57:49 -07:00
|
|
|
|
2022-11-22 17:40:22 -07:00
|
|
|
default:
|
|
|
|
err = ErrorUnknownOpcode
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func integerPower (x, y int64) (result int64) {
|
|
|
|
if y == 0 {
|
|
|
|
result = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result = x
|
|
|
|
for index := int64(2); index <= y; index ++ {
|
|
|
|
result *= x
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func integerRoot (x, y int64) (result int64) {
|
|
|
|
// FIXME: find some algorithm for the nth root of an integer
|
|
|
|
result = int64(math.Pow(float64(x), 1 / float64(y)))
|
|
|
|
return
|
|
|
|
}
|