package main import ( "bufio" "container/list" "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 getTotalAmountOfCards(cards []Card) uint64 { var result uint64 = 0 cardStack := list.New() cardStack.Init() for _, card := range cards { cardStack.PushBack(card.id) } for cardStack.Len() > 0 { current := cardStack.Front() id := current.Value.(uint64) index := id - 1 if cards[index].correctGuesses > 0 { for i := id; i < id+uint64(cards[index].correctGuesses); i++ { cardStack.PushBack(cards[i].id) } } result++ cardStack.Remove(current) } return result } 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 cards []Card var points []uint64 for _, line := range lines { cards = append(cards, Card{}) err := cards[len(cards)-1].parseCard(line) if err != nil { redPrint.Fprintf(os.Stderr, "Could not parse line '%s'\n", line) } points = append(points, cards[len(cards)-1].getPoints()) } sumPart1 := funk.Reduce(points, func(acc, elem uint64) uint64 { return acc + elem }, uint64(0)) fmt.Print("Part 1: ") yellowPrint.Print(sumPart1) fmt.Println() part2 := getTotalAmountOfCards(cards) fmt.Print("Part 1: ") yellowPrint.Print(part2) fmt.Println() }