advent-of-code/2025/cmd/root.go
C4 Patino 7062b47026
Some checks are pending
gofmt / format (push) Waiting to run
feat: completed day 1 for advent of code 2025
2025-12-03 21:54:59 -06:00

65 lines
1.2 KiB
Go

package cmd
import (
"fmt"
"log"
"os"
"strconv"
"cpatino.com/advent-of-code/2025/cmd/generate"
"github.com/spf13/cobra"
"cpatino.com/advent-of-code/2025/day01"
)
var days = map[int]func(string) (any, any){
1: day01.Run,
}
var rootCmd = &cobra.Command{
Use: "advent-of-code",
Short: "Advent of Code 2024",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
day, err := strconv.Atoi(args[0])
if err != nil {
log.Fatal(err)
}
file, _ := cmd.Flags().GetString("file")
if file == "" {
file = fmt.Sprintf("./day%02d/input.txt", day)
}
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Fatalf("file does not exist: %s", file)
}
log.Printf("Day: %d, Input File: %s\n", day, file)
part1, part2 := days[day](file)
log.Printf("Part 1: %v\n", part1)
log.Printf("Part 2: %v\n", part2)
},
}
func init() {
rootCmd.AddCommand(generate.GenerateCmd)
rootCmd.Flags().String("file", "", "Path to a file")
}
func Execute() error {
if len(os.Args) == 1 {
return rootCmd.Execute()
}
subCmd := os.Args[1]
for _, cmd := range rootCmd.Commands() {
if cmd.Use == subCmd {
return cmd.Execute()
}
}
return rootCmd.Execute()
}