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
|
|
|
)
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
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-03 14:16:00 +00:00
|
|
|
func StdinToCoordMap() CoordByteMap {
|
|
|
|
return StringSliceToCoordByteMap(StdinToStringSlice())
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-12-05 14:04:30 +00:00
|
|
|
fmt.Print(FILL_CHAR)
|
2016-12-20 17:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|