solutions for the Advent of Code 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.1 KiB

package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
)
type puzzle interface {
solve(r io.Reader) string
}
var (
// pick which day to run with a flag
day = flag.String("d", "", "which day to solve")
// slice of all puzzles
puzzles = []puzzle{
&day01{},
}
)
func init() {
flag.Parse()
}
func inputs(day int64) []io.Reader {
inputs := make([]io.Reader, 0, 2)
path := fmt.Sprintf("input/%d-[1-2]", day)
matches, err := filepath.Glob(path)
if err != nil {
log.Fatalf("matching puzzle input files: %s", err.Error())
}
fmt.Println(matches)
for _, match := range matches {
f, err := os.Open(match)
if err == nil {
inputs = append(inputs, f)
}
}
return inputs
}
func main() {
puzzleIndex, err := strconv.ParseInt(*day, 10, 32)
if err != nil {
log.Fatalf("invalid puzzle day: %s", *day)
}
inputs := inputs(puzzleIndex)
puzzle := puzzles[puzzleIndex-1]
fmt.Fprintf(os.Stdout, "Day %d:\n", puzzleIndex)
for i := range inputs {
ans := puzzle.solve(inputs[i])
fmt.Fprintf(os.Stdout, "Solution %d:\n%s", i+1, ans)
}
fmt.Fprintln(os.Stdout)
}