Add encode and decode
This commit is contained in:
parent
b14d51f749
commit
bf10724777
49
djpianalto.com/goff/exts/fun.go
Normal file
49
djpianalto.com/goff/exts/fun.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package exts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/dustinpianalto/disgoman"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func interleave(ctx disgoman.Context, args []string) {
|
||||||
|
if len(args) == 2 {
|
||||||
|
x, err := strconv.ParseInt(args[0], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
y, err := strconv.ParseInt(args[1], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var z = int64(0)
|
||||||
|
for i := 0; i < 64; i++ {
|
||||||
|
x_masked_i := x & (1 << i)
|
||||||
|
y_masked_i := y & (1 << i)
|
||||||
|
|
||||||
|
z |= x_masked_i << i
|
||||||
|
z |= y_masked_i << (i + 1)
|
||||||
|
}
|
||||||
|
ctx.Send(string(z))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func deinterleave(ctx disgoman.Context, args []string) {
|
||||||
|
if len(args) == 1 {
|
||||||
|
z, err := strconv.ParseInt(args[0], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var x = int64(0)
|
||||||
|
var y = int64(0)
|
||||||
|
i := 0
|
||||||
|
for z > 0 {
|
||||||
|
x |= (z & 1) << i
|
||||||
|
z >>= 1
|
||||||
|
y |= (z & 1) << i
|
||||||
|
z >>= 1
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
ctx.Send(fmt.Sprintf("(%v, %v)", x, y))
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -168,4 +168,22 @@ func AddCommandHandlers(h *disgoman.CommandManager) {
|
|||||||
RequiredPermissions: 0,
|
RequiredPermissions: 0,
|
||||||
Invoke: addReminderCommand,
|
Invoke: addReminderCommand,
|
||||||
})
|
})
|
||||||
|
_ = h.AddCommand(&disgoman.Command{
|
||||||
|
Name: "encode",
|
||||||
|
Aliases: []string{"e"},
|
||||||
|
Description: "Encode 2 numbers",
|
||||||
|
OwnerOnly: false,
|
||||||
|
Hidden: false,
|
||||||
|
RequiredPermissions: 0,
|
||||||
|
Invoke: interleave,
|
||||||
|
})
|
||||||
|
_ = h.AddCommand(&disgoman.Command{
|
||||||
|
Name: "decode",
|
||||||
|
Aliases: []string{"d"},
|
||||||
|
Description: "Decode 1 number into 2",
|
||||||
|
OwnerOnly: false,
|
||||||
|
Hidden: false,
|
||||||
|
RequiredPermissions: 0,
|
||||||
|
Invoke: deinterleave,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user