adventofcode/helpers.go
Brian Buller 47b656d34a 2018 Day 20 Done
* Also some cleanup on helpers.go
2018-12-20 17:17:30 -06:00

144 lines
2.4 KiB
Go

package aoc
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
)
// Some handy Constants
const (
BORDER_NS = "\u2502"
BORDER_WE = "\u2500"
BORDER_NW = "\u250C"
BORDER_NE = "\u2510"
BORDER_SW = "\u2514"
BORDER_SE = "\u2518"
FILL_CHAR = "\u2588"
CLEAR_SCREEN = "\033[H\033[2J"
MAX_INT = int(^uint(0) >> 1)
MIN_INT = -MAX_INT - 1
)
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 ""
}
func GetArgNumber(i int) string {
if len(os.Args) > i {
return os.Args[i]
}
return ""
}
func StdinToStringSlice() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}
func Itoa(i int) string {
return strconv.Itoa(i)
}
func StdinToString() string {
var input string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input += scanner.Text()
}
return input
}
func FileToStringSlice(fn string) []string {
return strings.Split(string(FileToBytes(fn)), "\n")
}
func FileToString(fn string) string {
return string(FileToBytes(fn))
}
func FileToBytes(fn string) []byte {
var c []byte
var err error
c, err = ioutil.ReadFile(fn)
if err != nil {
fmt.Println("Unable to read file: " + fn)
os.Exit(1)
}
return c
}
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)
}
}
}
func StringPermutations(str string) []string {
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
}
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
}