combo/pin/pin.go

121 lines
1.7 KiB
Go
Raw Normal View History

2020-08-30 00:46:11 +03:00
package pin
2024-01-23 18:20:00 +03:00
import (
2020-08-30 00:46:11 +03:00
"fmt"
2024-01-23 18:20:00 +03:00
//"strconv"
//"log"
2024-09-20 22:52:53 +03:00
"surdeus.su/core/cli/mtool"
2024-01-23 18:20:00 +03:00
"os"
2020-08-30 00:46:11 +03:00
)
2024-01-23 18:20:00 +03:00
var (
Lflag bool
lval int
2020-08-30 00:46:11 +03:00
delim rune = '\n'
nVal int
chrs []rune
2024-01-23 18:20:00 +03:00
Tool = mtool.T("pin").Func(Run).Desc(
"print all the possible PIN combinations made of custom characters",
).Usage(
"<tmpl_chars>",
)
2020-08-30 00:46:11 +03:00
)
2024-01-23 18:20:00 +03:00
func Pow(x, p int) int {
2020-08-30 00:46:11 +03:00
ret := 1
for i:=0 ; i<p ; i++ {
ret *= x
}
return ret
}
2024-01-23 18:20:00 +03:00
func GetPin(s []rune, l int, i int) string {
2020-08-30 00:46:11 +03:00
ret := ""
slen := len(s)
for j:=0 ; j<l ; j++ {
2020-09-17 21:28:29 +03:00
ret = string(s[ ( i/Pow(slen, j) ) % slen] ) + ret
2020-08-30 00:46:11 +03:00
}
return ret
}
2024-01-23 18:20:00 +03:00
func printPins(s []rune, l int) {
2020-08-30 00:46:11 +03:00
n := Pow(len(s), l)
for i:=0 ; i<n ; i++ {
pin := GetPin(s, l, i)
if Fits([]rune(pin)) {
fmt.Println(GetPin(s, l, i))
}
2020-08-30 00:46:11 +03:00
}
}
2024-01-23 18:20:00 +03:00
func Fits(s []rune) bool {
a := make([]int, len(chrs))
for i, v1 := range chrs {
for _, v2 := range s {
if v1 == v2 {
a[i]++
if a[i] > nVal {
return false
}
}
}
}
return true
}
2024-01-23 18:20:00 +03:00
func Run(flags *mtool.Flags) {
var (
length int
rFlag bool
)
flags.IntVar(
&lval, "min", 0,
"min length of the output pins",
)
flags.BoolVar(
&Lflag, "m", false,
"set the '-min' flag value to 1 (overrides it)",
)
2024-01-23 18:20:00 +03:00
flags.IntVar(
&length, "max", 0,
"max length of the output pins",
)
flags.IntVar(&nVal, "rep", 1, "max repeats of the rune." )
flags.BoolVar(&rFlag, "R", false, "make the maximum repeat equal to the length of input chars" )
args := flags.Parse()
if len(args) == 0 {
2024-01-23 18:20:00 +03:00
flags.Usage()
os.Exit(1)
}
chrs = []rune(args[0])
2020-08-30 00:46:11 +03:00
2024-01-23 18:20:00 +03:00
if length == 0 {
length = len(chrs)
2020-08-30 00:46:11 +03:00
}
if Lflag {
lval = 1
}
2024-01-23 18:20:00 +03:00
if rFlag {
nVal = len(chrs)
}
if lval != 0 {
2024-01-23 18:20:00 +03:00
if lval > length {
flags.Usage()
os.Exit(1)
}
2024-01-23 18:20:00 +03:00
for i := lval ; i<=length ; i++ {
printPins(chrs, i)
}
} else {
2024-01-23 18:20:00 +03:00
printPins(chrs, length)
}
2020-08-30 00:46:11 +03:00
}