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"
|
2023-12-11 14:43:32 +00:00
|
|
|
CLEAR_LINE = "\033[1K"
|
2018-12-20 23:17:30 +00:00
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2024-12-07 14:09:00 +00:00
|
|
|
func CheckErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 14:09:58 +00:00
|
|
|
// Fact returns the factorial of the given int
|
|
|
|
func Fact(x int) int {
|
|
|
|
var ret int
|
|
|
|
for i := 0; i <= x; i++ {
|
|
|
|
ret += i
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abs returns the absolute value of the given int
|
|
|
|
func Abs(x int) int {
|
|
|
|
if x < 0 {
|
|
|
|
return x * -1
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-17 19:06:03 +00:00
|
|
|
// StdinToString reads from stdin and returns a string
|
|
|
|
func StdinToString() string {
|
|
|
|
var input string
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
input += scanner.Text()
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2022-12-11 21:13:28 +00:00
|
|
|
// Atof parses a float out of a string
|
|
|
|
func Atof(i string) float64 {
|
|
|
|
var ret float64
|
|
|
|
var err error
|
|
|
|
if ret, err = strconv.ParseFloat(i, 64); err != nil {
|
|
|
|
log.Fatalf("Invalid Atof: %s\n%v", i, err)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-12-09 15:41:02 +00:00
|
|
|
// Atoi goes through i and removes and characters that aren't numeric (or -)
|
2023-12-04 14:54:52 +00:00
|
|
|
// Then runs it through the normal Atoi
|
2016-12-17 16:00:39 +00:00
|
|
|
func Atoi(i string) int {
|
2023-12-04 14:54:52 +00:00
|
|
|
var wrk string
|
|
|
|
for _, bt := range i {
|
2023-12-09 15:41:02 +00:00
|
|
|
if bt == '-' || (bt >= '0' && bt <= '9') {
|
2023-12-04 14:54:52 +00:00
|
|
|
wrk = fmt.Sprintf("%s%s", wrk, string(bt))
|
|
|
|
}
|
|
|
|
}
|
2016-12-04 15:55:02 +00:00
|
|
|
var ret int
|
|
|
|
var err error
|
2023-12-04 14:54:52 +00:00
|
|
|
if ret, err = strconv.Atoi(wrk); err != nil {
|
2022-12-05 15:47:05 +00:00
|
|
|
log.Fatalf("Invalid Atoi: %s\n%v", i, err)
|
2016-12-04 15:55:02 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-12-11 21:13:28 +00:00
|
|
|
// Atoui is the same as Atoi, but with unsigned ints
|
|
|
|
func Atoui(i string) uint64 {
|
|
|
|
var ret uint64
|
|
|
|
var err error
|
|
|
|
if ret, err = strconv.ParseUint(i, 10, 64); err != nil {
|
|
|
|
log.Fatalf("Invalid Atoi: %s\n%v", i, err)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-12-30 13:41:24 +00:00
|
|
|
pct := int(float64(curr) / float64(total) * 100)
|
|
|
|
fmt.Print("[")
|
2016-12-20 17:05:43 +00:00
|
|
|
for i := 0; i < 100; i += 10 {
|
|
|
|
if pct > i {
|
2019-12-05 14:04:30 +00:00
|
|
|
fmt.Print(FILL_CHAR)
|
2021-12-30 13:41:24 +00:00
|
|
|
} else {
|
|
|
|
fmt.Print(" ")
|
2016-12-20 17:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-30 13:41:24 +00:00
|
|
|
fmt.Print("]")
|
2016-12-20 17:05:43 +00:00
|
|
|
}
|
|
|
|
|
2024-12-22 14:16:42 +00:00
|
|
|
func ClearProgress() {
|
|
|
|
fmt.Print("\b\b\b\b\b\b\b\b\b\b\b\b")
|
|
|
|
}
|
|
|
|
|
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-21 13:56:31 +00:00
|
|
|
func StringSliceIndex(h []string, n string) int {
|
|
|
|
for k, v := range h {
|
|
|
|
if v == n {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-12-20 21:57:10 +00:00
|
|
|
// IntSliceContains takes an int slice and an int and return true
|
|
|
|
// if the int is in the slice
|
|
|
|
func IntSliceContains(h []int, n int) bool {
|
|
|
|
for _, v := range h {
|
|
|
|
if v == n {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendString returns a slice of strings that are base+group[...]
|
|
|
|
func AppendStrings(base string, group []string) []string {
|
|
|
|
for k, v := range group {
|
|
|
|
group[k] = base + v
|
|
|
|
}
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
2020-12-21 13:56:31 +00:00
|
|
|
// UnionStringSlice returns a slice that combines list1 and list2
|
|
|
|
func UnionStringSlice(list1, list2 []string) []string {
|
|
|
|
for k := range list1 {
|
|
|
|
if !StringSliceContains(list2, list1[k]) {
|
|
|
|
list2 = append(list2, list1[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list2
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntersectStringSlice returns a slice that contains all elements that
|
|
|
|
// both list1 and list2 contain
|
|
|
|
func IntersectStringSlice(list1, list2 []string) []string {
|
|
|
|
var ret []string
|
|
|
|
for k := range list1 {
|
|
|
|
if StringSliceContains(list2, list1[k]) {
|
|
|
|
ret = append(ret, list1[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unique removes all duplicates
|
|
|
|
func UniqueStringSlice(list []string) []string {
|
|
|
|
var ret []string
|
|
|
|
for k := 0; k < len(list); k++ {
|
|
|
|
var dupe bool
|
|
|
|
for j := k; j < len(list); j++ {
|
|
|
|
if list[k] == list[j] {
|
|
|
|
dupe = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !dupe {
|
|
|
|
ret = append(ret, list[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2020-12-24 15:58:15 +00:00
|
|
|
|
2023-12-05 17:23:48 +00:00
|
|
|
func SliceMin(sl []int) int {
|
|
|
|
switch len(sl) {
|
|
|
|
case 0:
|
|
|
|
return MIN_INT
|
|
|
|
case 1:
|
|
|
|
return sl[0]
|
|
|
|
case 2:
|
|
|
|
return Min(sl[0], sl[1])
|
|
|
|
default:
|
|
|
|
return Min(sl[0], sl[1], sl[2:]...)
|
|
|
|
}
|
|
|
|
}
|
2024-12-07 14:09:00 +00:00
|
|
|
|
2021-12-17 19:06:03 +00:00
|
|
|
func Min(v1, v2 int, vrest ...int) int {
|
|
|
|
min := v2
|
2020-12-24 15:58:15 +00:00
|
|
|
if v1 < v2 {
|
2021-12-17 19:06:03 +00:00
|
|
|
min = v1
|
|
|
|
}
|
|
|
|
for i := range vrest {
|
|
|
|
if vrest[i] < min {
|
|
|
|
min = vrest[i]
|
|
|
|
}
|
2020-12-24 15:58:15 +00:00
|
|
|
}
|
2021-12-17 19:06:03 +00:00
|
|
|
return min
|
2020-12-24 15:58:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 19:06:03 +00:00
|
|
|
func Max(v1, v2 int, vrest ...int) int {
|
|
|
|
max := v2
|
2020-12-24 15:58:15 +00:00
|
|
|
if v1 > v2 {
|
2021-12-17 19:06:03 +00:00
|
|
|
max = v1
|
2020-12-24 15:58:15 +00:00
|
|
|
}
|
2021-12-17 19:06:03 +00:00
|
|
|
for i := range vrest {
|
|
|
|
if vrest[i] > max {
|
|
|
|
max = vrest[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sum adds up all of the numbers from l to h
|
|
|
|
func Sum(l, h int) int {
|
|
|
|
var ret int
|
|
|
|
for i := Min(l, h); i <= Max(l, h); i++ {
|
|
|
|
ret = ret + i
|
|
|
|
}
|
|
|
|
return ret
|
2020-12-24 15:58:15 +00:00
|
|
|
}
|
2022-12-15 16:57:33 +00:00
|
|
|
|
|
|
|
func ManhattanDistance(x1, y1, x2, y2 int) int {
|
|
|
|
return AbsInt(x1-x2) + AbsInt(y1-y2)
|
|
|
|
}
|
2024-12-07 14:09:00 +00:00
|
|
|
|
|
|
|
func PromptUser(text string, required bool) string {
|
|
|
|
var resp string
|
|
|
|
fmt.Print(text + ": ")
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
if scanner.Scan() {
|
|
|
|
resp = scanner.Text()
|
|
|
|
}
|
|
|
|
if resp == "" && required {
|
|
|
|
fmt.Println("Non-empty response is required")
|
|
|
|
return PromptUser(text, required)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(resp)
|
|
|
|
}
|