2020 Day 13 Complete!
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -25,9 +26,11 @@ const (
|
||||
|
||||
MAX_INT = int(^uint(0) >> 1)
|
||||
MIN_INT = -MAX_INT - 1
|
||||
|
||||
SHRUG = "¯\\_(ツ)_/¯"
|
||||
)
|
||||
|
||||
// Find the greatest common denominator
|
||||
// Gcd Finds the greatest common denominator
|
||||
func Gcd(x, y int) int {
|
||||
for y != 0 {
|
||||
x, y = y, x%y
|
||||
@@ -35,7 +38,7 @@ func Gcd(x, y int) int {
|
||||
return x
|
||||
}
|
||||
|
||||
// Find the least common multiple, using gcd
|
||||
// Lcm finds 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++ {
|
||||
@@ -44,6 +47,7 @@ func Lcm(a, b int, integers ...int) int {
|
||||
return result
|
||||
}
|
||||
|
||||
// AbsInt returns the absolute value of i
|
||||
func AbsInt(i int) int {
|
||||
if i < 0 {
|
||||
return i * -1
|
||||
@@ -51,6 +55,7 @@ func AbsInt(i int) int {
|
||||
return i
|
||||
}
|
||||
|
||||
// ArgIsSet return true if an argument with the asked for key is present
|
||||
func ArgIsSet(a string) bool {
|
||||
for i := range os.Args {
|
||||
if os.Args[i] == a || strings.HasPrefix(os.Args[i], a+"=") {
|
||||
@@ -60,6 +65,7 @@ func ArgIsSet(a string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// GetArgValue returns the argument with the asked for key or ""
|
||||
func GetArgValue(a string) string {
|
||||
for i := range os.Args {
|
||||
if strings.HasPrefix(os.Args[i], a+"=") {
|
||||
@@ -69,6 +75,7 @@ func GetArgValue(a string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetArgNumber returns the asked for argument position or ""
|
||||
func GetArgNumber(i int) string {
|
||||
if len(os.Args) > i {
|
||||
return os.Args[i]
|
||||
@@ -76,6 +83,7 @@ func GetArgNumber(i int) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// OptArgNumber returns either the asked for argument position or the passed default
|
||||
func OptArgNumber(i int, def string) string {
|
||||
if len(os.Args) > i {
|
||||
return os.Args[i]
|
||||
@@ -83,6 +91,7 @@ func OptArgNumber(i int, def string) string {
|
||||
return def
|
||||
}
|
||||
|
||||
// StdinToIntSlice reads from stdin and returns it as an int slice
|
||||
func StdinToIntSlice() []int {
|
||||
var ret []int
|
||||
st := StdinToStringSlice()
|
||||
@@ -92,6 +101,7 @@ func StdinToIntSlice() []int {
|
||||
return ret
|
||||
}
|
||||
|
||||
// StdinToStringSlice reads from stdin and returns it as a string slice
|
||||
func StdinToStringSlice() []string {
|
||||
var input []string
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
@@ -101,10 +111,12 @@ func StdinToStringSlice() []string {
|
||||
return input
|
||||
}
|
||||
|
||||
// StdinToCoordMap reads stdin and returns it as a CoordByteMap
|
||||
func StdinToCoordMap() CoordByteMap {
|
||||
return StringSliceToCoordByteMap(StdinToStringSlice())
|
||||
}
|
||||
|
||||
// Atoi is basically redundant
|
||||
func Atoi(i string) int {
|
||||
var ret int
|
||||
var err error
|
||||
@@ -114,10 +126,12 @@ func Atoi(i string) int {
|
||||
return ret
|
||||
}
|
||||
|
||||
// Itoa is basically redundant
|
||||
func Itoa(i int) string {
|
||||
return strconv.Itoa(i)
|
||||
}
|
||||
|
||||
// StdinToString reads from stdin and returns a string
|
||||
func StdinToString() string {
|
||||
var input string
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
@@ -127,14 +141,17 @@ func StdinToString() string {
|
||||
return input
|
||||
}
|
||||
|
||||
// FileToStringSlice takes a file and returns it as a slice of strings
|
||||
func FileToStringSlice(fn string) []string {
|
||||
return strings.Split(string(FileToBytes(fn)), "\n")
|
||||
}
|
||||
|
||||
// FileToString reads a file and returns it as a string
|
||||
func FileToString(fn string) string {
|
||||
return string(FileToBytes(fn))
|
||||
}
|
||||
|
||||
// FileToBytes reads a file and returns it as a slice of bytes
|
||||
func FileToBytes(fn string) []byte {
|
||||
var c []byte
|
||||
var err error
|
||||
@@ -146,6 +163,7 @@ func FileToBytes(fn string) []byte {
|
||||
return c
|
||||
}
|
||||
|
||||
// PrintProgress is for outputting a progress bar
|
||||
func PrintProgress(curr, total int) {
|
||||
pct := int(float64(curr)/float64(total)) * 100
|
||||
for i := 0; i < 100; i += 10 {
|
||||
@@ -155,6 +173,7 @@ func PrintProgress(curr, total int) {
|
||||
}
|
||||
}
|
||||
|
||||
// StringPermutations takes a string and returns all permutations of it
|
||||
func StringPermutations(str string) []string {
|
||||
perms := stringPermHelper(str, 0)
|
||||
var wrk []string
|
||||
@@ -186,6 +205,7 @@ func stringPermHelper(str string, i int) []string {
|
||||
return ret
|
||||
}
|
||||
|
||||
// IntPermutations takes a slice of ints and returns all permutations of it
|
||||
func IntPermutations(inp []int) [][]int {
|
||||
perms := intPermHelper(inp, 0)
|
||||
var wrk [][]int
|
||||
@@ -219,6 +239,7 @@ func intPermHelper(inp []int, i int) [][]int {
|
||||
return ret
|
||||
}
|
||||
|
||||
// IntSlicesAreEqual takes two int slices and returns if they are equal
|
||||
func IntSlicesAreEqual(s1 []int, s2 []int) bool {
|
||||
if len(s1) != len(s2) {
|
||||
return false
|
||||
@@ -231,6 +252,8 @@ func IntSlicesAreEqual(s1 []int, s2 []int) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// StringSliceContains takes a string slice and a string and return true
|
||||
// if the string is in the slice
|
||||
func StringSliceContains(h []string, n string) bool {
|
||||
for _, v := range h {
|
||||
if v == n {
|
||||
@@ -239,3 +262,13 @@ func StringSliceContains(h []string, n string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user