2016-12-04 15:55:02 +00:00
|
|
|
package aoc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2016-12-09 15:59:13 +00:00
|
|
|
"fmt"
|
2016-12-17 16:00:39 +00:00
|
|
|
"io/ioutil"
|
2016-12-04 15:55:02 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2016-12-19 16:19:18 +00:00
|
|
|
"strings"
|
2016-12-04 15:55:02 +00:00
|
|
|
)
|
|
|
|
|
2016-12-19 16:19:18 +00:00
|
|
|
func ArgIsSet(a string) bool {
|
|
|
|
for i := range os.Args {
|
|
|
|
if os.Args[i] == a || strings.HasPrefix(os.Args[i], a+"=") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetArgValue(a string) string {
|
|
|
|
for i := range os.Args {
|
|
|
|
if strings.HasPrefix(os.Args[i], a+"=") {
|
|
|
|
return strings.TrimPrefix(os.Args[i], a+"=")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
2016-12-22 18:12:13 +00:00
|
|
|
}
|
2016-12-19 16:19:18 +00:00
|
|
|
|
2016-12-22 18:12:13 +00:00
|
|
|
func GetArgNumber(i int) string {
|
|
|
|
if len(os.Args) > i {
|
|
|
|
return os.Args[i]
|
|
|
|
}
|
|
|
|
return ""
|
2016-12-19 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-17 16:00:39 +00:00
|
|
|
func StdinToStringSlice() []string {
|
2016-12-04 15:55:02 +00:00
|
|
|
var input []string
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
input = append(input, scanner.Text())
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
2016-12-17 16:00:39 +00:00
|
|
|
func Atoi(i string) int {
|
2016-12-04 15:55:02 +00:00
|
|
|
var ret int
|
|
|
|
var err error
|
|
|
|
if ret, err = strconv.Atoi(i); err != nil {
|
|
|
|
log.Fatal("Invalid Atoi")
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2016-12-09 15:59:13 +00:00
|
|
|
|
2016-12-17 16:00:39 +00:00
|
|
|
func Itoa(i int) string {
|
2016-12-15 14:36:57 +00:00
|
|
|
return strconv.Itoa(i)
|
|
|
|
}
|
|
|
|
|
2016-12-17 16:00:39 +00:00
|
|
|
func StdinToString() string {
|
2016-12-09 15:59:13 +00:00
|
|
|
var input string
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
input += scanner.Text()
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:12:13 +00:00
|
|
|
func FileToStringSlice(fn string) []string {
|
|
|
|
return strings.Split(string(FileToBytes(fn)), "\n")
|
|
|
|
}
|
|
|
|
|
2016-12-17 16:00:39 +00:00
|
|
|
func FileToString(fn string) string {
|
2016-12-18 15:32:37 +00:00
|
|
|
return string(FileToBytes(fn))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FileToBytes(fn string) []byte {
|
2016-12-09 15:59:13 +00:00
|
|
|
var c []byte
|
2016-12-17 16:00:39 +00:00
|
|
|
var err error
|
|
|
|
c, err = ioutil.ReadFile(fn)
|
2016-12-09 15:59:13 +00:00
|
|
|
if err != nil {
|
2016-12-17 16:00:39 +00:00
|
|
|
fmt.Println("Unable to read file: " + fn)
|
2016-12-09 15:59:13 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-12-18 15:32:37 +00:00
|
|
|
return c
|
2016-12-09 15:59:13 +00:00
|
|
|
}
|
2016-12-15 14:36:57 +00:00
|
|
|
|
2016-12-20 17:05:43 +00:00
|
|
|
func PrintProgress(curr, total int) {
|
|
|
|
pct := int(float64(curr)/float64(total)) * 100
|
|
|
|
for i := 0; i < 100; i += 10 {
|
|
|
|
if pct > i {
|
|
|
|
fmt.Print(FillChar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:15:25 +00:00
|
|
|
func StringPermutations(str string) []string {
|
2016-12-28 14:48:23 +00:00
|
|
|
perms := stringPermHelper(str, 0)
|
|
|
|
var wrk []string
|
|
|
|
// Now de-dupe
|
|
|
|
for i := range perms {
|
|
|
|
var found bool
|
|
|
|
for j := range wrk {
|
|
|
|
if wrk[j] == perms[i] {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
wrk = append(wrk, perms[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wrk
|
2016-12-21 17:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stringPermHelper(str string, i int) []string {
|
|
|
|
ret := []string{str}
|
|
|
|
if i != len(str) {
|
|
|
|
r := []rune(str)
|
|
|
|
for j := i; j < len(r); j++ {
|
|
|
|
r[i], r[j] = r[j], r[i]
|
|
|
|
ret = append(ret, stringPermHelper(string(r), i+1)...)
|
|
|
|
r[i], r[j] = r[j], r[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2016-12-18 15:32:37 +00:00
|
|
|
// Some character code stuff for prettier output
|
|
|
|
const (
|
|
|
|
BorderNS = "\u2502"
|
|
|
|
BorderWE = "\u2500"
|
|
|
|
|
|
|
|
BorderNW = "\u250C"
|
|
|
|
BorderNE = "\u2510"
|
|
|
|
BorderSW = "\u2514"
|
|
|
|
BorderSE = "\u2518"
|
|
|
|
|
|
|
|
FillChar = "\u2588"
|
|
|
|
ClearScreen = "\033[H\033[2J"
|
|
|
|
)
|