Browse Source

add day 2 to main.go and simplify input reading

master
Alexander Avery 12 months ago
parent
commit
3eb7e88d8b
  1. 31
      main.go

31
main.go

@ -21,35 +21,28 @@ var (
// slice of all puzzles
puzzles = []puzzle{
&day01{},
&day02{},
}
)
func inputs(day int) [2]io.Reader {
inputs := [2]io.Reader{}
path1 := fmt.Sprintf("input/%d-1", day)
path2 := fmt.Sprintf("input/%d-2", day)
f1, err := os.Open(path1)
if err == nil {
inputs[0] = f1
}
f2, err := os.Open(path2)
if err == nil {
inputs[1] = f2
}
return inputs
}
func main() {
flag.Parse()
puzzleIndex, err := strconv.Atoi(*day)
if err != nil {
log.Fatalf("invalid puzzle day: %s", *day)
}
inputs := inputs(puzzleIndex)
puzzle := puzzles[puzzleIndex-1]
fmt.Fprintf(os.Stdout, "Solution 1:\n%s\n", puzzle.solve1(inputs[0]))
fmt.Fprintf(os.Stdout, "Solution 2:\n%s\n", puzzle.solve2(inputs[1]))
path1 := fmt.Sprintf("input/%d-1", puzzleIndex)
path2 := fmt.Sprintf("input/%d-2", puzzleIndex)
f1, err := os.Open(path1)
if err == nil {
fmt.Fprintf(os.Stdout, "Solution 1:\n%s\n", puzzle.solve1(f1))
}
f2, err := os.Open(path2)
if err == nil {
fmt.Fprintf(os.Stdout, "Solution 2:\n%s\n", puzzle.solve2(f2))
}
}

Loading…
Cancel
Save