Related: Multiply Quaternions
Challenge
Given a string made of ijk
, interpret it as the product of imaginary units of quaternion and simplify it into one of the eight possible values 1
, -1
, i
, -i
, j
, -j
, k
, -k
.
The evaluation rules are as follows:
$$ ii = jj = kk = -1 \\ ij = k, \quad jk = i, \quad ki = j \\ ji = -k, \quad kj = -i, \quad ik = -j $$
The multiplication of quaternions is associative but not commutative. This means that you may do the simplification in any order, but you cannot reorder the items.
For the I/O format, function parameter and return from a function should be done as a string (list of chars or list of codepoints is also OK). You may assume the input is not empty. You may output any amount of leading and trailing whitespace (spaces, tabs, newlines).
Test cases
i -> i
j -> j
k -> k
ij -> k
ji -> -k
kk -> -1
ijkj -> -j
iikkkkkjj -> k
kikjkikjk -> -k
kjijkjikijkjiki -> 1
jikjjikjikjikjjjj -> j
(-1)i = i(-1) = -i
, and similarly forj
andk
. \$\endgroup\$