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"
|
2020-12-13 16:42:32 +00:00
|
|
|
"math"
|
2016-12-04 15:55:02 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2016-12-19 16:19:18 +00:00
|
|
|
"strings"
|
2016-12-04 15:55:02 +00:00
|
|
|
)
|
|
|
|
|
2018-12-20 23:17:30 +00:00
|
|
|
// 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
|
2020-12-13 16:42:32 +00:00
|
|
|
|
|
|
|
SHRUG = "¯\\_(ツ)_/¯"
|
2018-12-20 23:17:30 +00:00
|
|
|
)
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// Gcd Finds the greatest common denominator
|
2019-12-12 14:24:43 +00:00
|
|
|
func Gcd(x, y int) int {
|
|
|
|
for y != 0 {
|
|
|
|
x, y = y, x%y
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// Lcm finds the least common multiple, using gcd
|
2019-12-12 14:24:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// AbsInt returns the absolute value of i
|
2019-12-10 15:17:50 +00:00
|
|
|
func AbsInt(i int) int {
|
|
|
|
if i < 0 {
|
|
|
|
return i * -1
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// ArgIsSet return true if an argument with the asked for key is present
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// GetArgValue returns the argument with the asked for key or ""
|
2016-12-19 16:19:18 +00:00
|
|
|
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
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// GetArgNumber returns the asked for argument position or ""
|
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
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// OptArgNumber returns either the asked for argument position or the passed default
|
2020-12-02 13:36:18 +00:00
|
|
|
func OptArgNumber(i int, def string) string {
|
|
|
|
if len(os.Args) > i {
|
|
|
|
return os.Args[i]
|
|
|
|
}
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// StdinToIntSlice reads from stdin and returns it as an int slice
|
2020-12-01 19:53:24 +00:00
|
|
|
func StdinToIntSlice() []int {
|
|
|
|
var ret []int
|
|
|
|
st := StdinToStringSlice()
|
|
|
|
for _, v := range st {
|
|
|
|
ret = append(ret, Atoi(v))
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// StdinToStringSlice reads from stdin and returns it as a string slice
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// StdinToCoordMap reads stdin and returns it as a CoordByteMap
|
2020-12-03 14:16:00 +00:00
|
|
|
func StdinToCoordMap() CoordByteMap {
|
|
|
|
return StringSliceToCoordByteMap(StdinToStringSlice())
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// Atoi is basically redundant
|
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
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// Itoa is basically redundant
|
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)
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// StdinToString reads from stdin and returns a string
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// FileToStringSlice takes a file and returns it as a slice of strings
|
2016-12-22 18:12:13 +00:00
|
|
|
func FileToStringSlice(fn string) []string {
|
|
|
|
return strings.Split(string(FileToBytes(fn)), "\n")
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// FileToString reads a file and returns it as a string
|
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))
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// FileToBytes reads a file and returns it as a slice of bytes
|
2016-12-18 15:32:37 +00:00
|
|
|
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
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// PrintProgress is for outputting a progress bar
|
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 {
|
2019-12-05 14:04:30 +00:00
|
|
|
fmt.Print(FILL_CHAR)
|
2016-12-20 17:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// StringPermutations takes a string and returns all permutations of it
|
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
|
|
|
|
}
|
2019-12-09 03:51:43 +00:00
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// IntPermutations takes a slice of ints and returns all permutations of it
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// IntSlicesAreEqual takes two int slices and returns if they are equal
|
2019-12-09 03:51:43 +00:00
|
|
|
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
|
|
|
|
}
|
2020-12-11 14:11:30 +00:00
|
|
|
|
2020-12-13 16:42:32 +00:00
|
|
|
// StringSliceContains takes a string slice and a string and return true
|
|
|
|
// if the string is in the slice
|
2020-12-11 14:11:30 +00:00
|
|
|
func StringSliceContains(h []string, n string) bool {
|
|
|
|
for _, v := range h {
|
|
|
|
if v == n {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-12-13 16:42:32 +00:00
|
|
|
|
|
|
|
// IsPrime takes a number and return true if that number is prime
|
|
|
|
func IsPrime(value int) bool {
|
|
|
|
for i := 2; i <= int(math.Floor(float64(value)/2)); i++ {
|
|
|
|
if value%i == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value > 1
|
|
|
|
}
|