package main import ( "bufio" "fmt" "os" "github.com/fatih/color" "github.com/thoas/go-funk" ) func readFileLines(filename string) ([]string, error) { var lines []string file, err := os.Open(filename) if err != nil { return lines, err } fileScanner := bufio.NewScanner(file) fileScanner.Split(bufio.ScanLines) for fileScanner.Scan() { lines = append(lines, fileScanner.Text()) } return lines, nil } func main() { redPrint := color.New(color.FgRed) yellowPrint := color.New(color.FgYellow) if len(os.Args) < 2 { redPrint.Fprintln(os.Stderr, "You must provide the input file") os.Exit(1) } lines, err := readFileLines(os.Args[1]) if err != nil { redPrint.Fprintln(os.Stderr, "Could not read input file") os.Exit(1) } var games []Game for _, line := range lines { games = append(games, Game{}) games[len(games)-1].parseGame(line) games[len(games)-1].resolveGame() } var part1Requirements = [3]CubeCount{{RED, 12}, {GREEN, 13}, {BLUE, 14}} var validIDsPart1 []uint64 for _, game := range games { if game.canBePlayed(part1Requirements) { validIDsPart1 = append(validIDsPart1, game.id) } } sumPart1 := funk.Reduce(validIDsPart1, func(acc, elem uint64) uint64 { return acc + elem }, uint64(0)) fmt.Print("Part 1: ") yellowPrint.Print(sumPart1) fmt.Println() var powersPart2 []uint64 for _, game := range games { powersPart2 = append(powersPart2, game.getGamePower()) } sumPart2 := funk.Reduce(powersPart2, func(acc, elem uint64) uint64 { return acc + elem }, uint64(0)) fmt.Print("Part 2: ") yellowPrint.Print(sumPart2) fmt.Println() }