From bf10724777509a0860a9a8c3336ccabcc5c825e0 Mon Sep 17 00:00:00 2001 From: Dustin Pianalto Date: Fri, 12 Jun 2020 00:07:35 -0800 Subject: [PATCH] Add encode and decode --- djpianalto.com/goff/exts/fun.go | 49 ++++++++++++++++++++++++++++++++ djpianalto.com/goff/exts/init.go | 18 ++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 djpianalto.com/goff/exts/fun.go diff --git a/djpianalto.com/goff/exts/fun.go b/djpianalto.com/goff/exts/fun.go new file mode 100644 index 0000000..26cf1d7 --- /dev/null +++ b/djpianalto.com/goff/exts/fun.go @@ -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)) + } +} diff --git a/djpianalto.com/goff/exts/init.go b/djpianalto.com/goff/exts/init.go index 309d9b0..9d8bacd 100644 --- a/djpianalto.com/goff/exts/init.go +++ b/djpianalto.com/goff/exts/init.go @@ -168,4 +168,22 @@ func AddCommandHandlers(h *disgoman.CommandManager) { RequiredPermissions: 0, 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, + }) }