adventofcode/helpers/helpers.go

213 lines
3.5 KiB
Go
Raw Normal View History

package aoc
import (
"bufio"
2016-12-09 15:59:13 +00:00
"fmt"
2016-12-17 16:00:39 +00:00
"io/ioutil"
"log"
"os"
"strconv"
2016-12-19 16:19:18 +00:00
"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
)
2019-12-12 14:24:43 +00:00
// Find the greatest common denominator
func Gcd(x, y int) int {
for y != 0 {
x, y = y, x%y
}
return x
}
// Find the least common multiple, using gcd
func Lcm(a, b int, integers ...int) int {
result := a * b / Gcd(a, b)
for i := 0; i < len(integers); i++ {
result = Lcm(result, integers[i])
}
return result
}
2019-12-10 15:17:50 +00:00
func AbsInt(i int) int {
if i < 0 {
return i * -1
}
return i
}
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-19 16:19:18 +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 {
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 {
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
}
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(FILL_CHAR)
2016-12-20 17:05:43 +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
}
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
}
2019-12-09 03:51:43 +00:00
func IntPermutations(inp []int) [][]int {
perms := intPermHelper(inp, 0)
var wrk [][]int
// Now de-dupe
for i := range perms {
var found bool
for j := range wrk {
if IntSlicesAreEqual(perms[i], wrk[j]) {
found = true
break
}
}
if !found {
wrk = append(wrk, perms[i])
}
}
return wrk
}
func intPermHelper(inp []int, i int) [][]int {
ret := [][]int{inp}
if i != len(inp) {
r := make([]int, len(inp))
copy(r, inp)
for j := i; j < len(r); j++ {
r[i], r[j] = r[j], r[i]
ret = append(ret, intPermHelper(r, i+1)...)
r[i], r[j] = r[j], r[i]
}
}
return ret
}
func IntSlicesAreEqual(s1 []int, s2 []int) bool {
if len(s1) != len(s2) {
return false
}
for k := range s1 {
if s1[k] != s2[k] {
return false
}
}
return true
}