~loges/aoc-go-utils

6b0f169c019eaf98f9587d802c13240a165c93c4 — Logan Connolly 7 months ago 746c819 master v0.2.0
feat: do not hardcode the input file path
1 files changed, 17 insertions(+), 4 deletions(-)

M io.go
M io.go => io.go +17 -4
@@ 3,13 3,16 @@ package aoc_go_utils
import (
	"log"
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

// ReadInputAsString is a helper function for reading in the
// problem input and converting it to string.
func ReadInputAsString() string {
	content, err := os.ReadFile("./input.txt")
func ReadInputAsString(fname string) string {
	fpath := getFilePath(fname)
	content, err := os.ReadFile(fpath)
	if err != nil {
		log.Fatal(err)
	}


@@ 18,7 21,17 @@ func ReadInputAsString() string {

// ReadInputAsStringLines is a helper function for reading in the
// problem input and converting it to a slice of strings.
func ReadInputAsStringLines(delim string) []string {
	s := ReadInputAsString()
func ReadInputAsStringLines(fname string, delim string) []string {
	s := ReadInputAsString(fname)
	return strings.Split(s, delim)
}

// getFilePath fetches the full path relative directory of the caller.
func getFilePath(fname string) string {
	_, filename, _, ok := runtime.Caller(0)
	if !ok {
		log.Fatalf("Could not find file: %s", fname)
	}
	dirname := filepath.Dir(filename)
	return filepath.Join(dirname, fname)
}