Watch
1
0
Fork
You've already forked advent-of-code
0

2021 Day 5

This commit is contained in:
Jeremy Kaplan 2021-12-04 22:10:09 -08:00
commit 6e64e705b3
2 changed files with 171 additions and 0 deletions

View file

@ -2,6 +2,8 @@ package aoc
import (
"bufio"
"io"
"io/fs"
"os"
"strconv"
"strings"
@ -36,3 +38,49 @@ func MustInt(s string) int {
}
return i
}
type Inputs struct {
fs fs.FS
}
func (i Inputs) ReadLines(path string) (lines []string) {
f, err := i.fs.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}
func (i Inputs) ReadFile(path string) string {
f, err := i.fs.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
panic(err)
}
return strings.TrimSpace(string(b))
}
func Input() Inputs {
if len(os.Args) > 1 {
return Inputs{os.DirFS(os.Args[1])}
}
return Inputs{os.DirFS("./input")}
}
// Cut will be added to strings in Go 1.18, so hack it in for now!
func Cut(s, sep string) (before, after string) {
if i := strings.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):]
}
panic("Separator was not found in string")
}