From d09df2aeb5e5841ddabc513b5067b752a2d2eda4 Mon Sep 17 00:00:00 2001 From: DutchEllie Date: Wed, 8 Dec 2021 10:53:46 +0100 Subject: [PATCH] Completed 6-2 --- 6-2/go.mod | 3 ++ 6-2/input | 1 + 6-2/main.go | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 6-2/go.mod create mode 100644 6-2/input create mode 100644 6-2/main.go diff --git a/6-2/go.mod b/6-2/go.mod new file mode 100644 index 0000000..a8a4a84 --- /dev/null +++ b/6-2/go.mod @@ -0,0 +1,3 @@ +module git.home.dutchellie.nl/DutchEllie/adventofcode2021/6-2 + +go 1.17 diff --git a/6-2/input b/6-2/input new file mode 100644 index 0000000..57e0ce5 --- /dev/null +++ b/6-2/input @@ -0,0 +1 @@ +4,1,3,2,4,3,1,4,4,1,1,1,5,2,4,4,2,1,2,3,4,1,2,4,3,4,5,1,1,3,1,2,1,4,1,1,3,4,1,2,5,1,4,2,2,1,1,1,3,1,5,3,1,2,1,1,1,1,4,1,1,1,2,2,1,3,1,3,1,3,4,5,1,2,2,1,1,1,4,1,5,1,3,1,3,4,1,3,2,3,4,4,4,3,4,5,1,3,1,3,5,1,1,1,1,1,2,4,1,2,1,1,1,5,1,1,2,1,3,1,4,2,3,4,4,3,1,1,3,5,3,1,1,5,2,4,1,1,3,5,1,4,3,1,1,4,2,1,1,1,1,1,1,3,1,1,1,1,1,4,5,1,2,5,3,1,1,3,1,1,1,1,5,1,2,5,1,1,1,1,1,1,3,5,1,3,2,1,1,1,1,1,1,1,4,5,1,1,3,1,5,1,1,1,1,3,3,1,1,1,4,4,1,1,4,1,2,1,4,4,1,1,3,4,3,5,4,1,1,4,1,3,1,1,5,5,1,2,1,2,1,2,3,1,1,3,1,1,2,1,1,3,4,3,1,1,3,3,5,1,2,1,4,1,1,2,1,3,1,1,1,1,1,1,1,4,5,5,1,1,1,4,1,1,1,2,1,2,1,3,1,3,1,1,1,1,1,1,1,5 \ No newline at end of file diff --git a/6-2/main.go b/6-2/main.go new file mode 100644 index 0000000..478aa46 --- /dev/null +++ b/6-2/main.go @@ -0,0 +1,107 @@ +package main + +import ( + "bufio" + "bytes" + "fmt" + "os" + "strconv" +) + +func main() { + fishies, err := readInput("input") + if err != nil { + fmt.Printf("Error reading or something\n%s", err) + return + } + + fmt.Printf("Initializing fishies array...\n") + fish, _ := splitFishies(fishies) + fmt.Printf("Done!\n") + + fishiesptr := &fish + // Run for i days + for i := 0; i < 256; i++ { + newfishies, _ := nextDay(*fishiesptr) + fishiesptr = &newfishies + amount := countFishies(*fishiesptr) + fmt.Printf("On day %d: there are %d fishies\n", i+1, amount) + } +} + +func countFishies(fishies []int) int { + counter := 0 + for i := 0; i < len(fishies); i++ { + counter += fishies[i] + } + return counter +} + +// Read the fishies slice and returns the slice of new fishies +func nextDay(fishies []int) ([]int, error) { + // Move fishies at 0 into buffer, move all fishies down the stack + // Add the 0 day fishies (from buffer) to the fishies at day 6 + // Add the same amount of fishies from buffer to the 8 day counter + // Return the new splits + + buffer := fishies[0] + for i := 0; i < len(fishies)-1; i++ { + fishies[i] = fishies[i+1] + } + fishies[6] += buffer + fishies[8] = buffer + return fishies, nil +} + +// Returns a slice of the amount of fishies per day, ordered from 0 days to 8 +func splitFishies(fishies []int) ([]int, error) { + fishiesButSplit := make([]int, 9) + for i := 0; i < len(fishies); i++ { + fishiesButSplit[fishies[i]]++ + } + + return fishiesButSplit, nil +} + +func readInput(inputfile string) ([]int, error) { + file, err := os.OpenFile(inputfile, 0, 0) + if err != nil { + return nil, err + } + defer file.Close() + + splitfunc := func(data []byte, atEOF bool) (advance int, token []byte, err error) { + nextIndex := bytes.IndexByte(data, ',') + if nextIndex > 0 { + // Returning the next position aka taking only the stuff up to nextIndex and slicing off everything else + buffer := data[:nextIndex] + // nextIndex plus 1 because index is at the comma, we want the next number + return nextIndex + 1, bytes.TrimSpace(buffer), nil + } + + // If we are at the end of the buffer, then return the entire buffer, but only if there is data + if atEOF { + if len(data) > 0 { + return len(data), bytes.TrimSpace(data), nil + } + } + + // https://github.com/kgrz/reading-files-in-go/blob/master/comma-separated-string.go + // For more info + return 0, nil, nil + } + + scanner := bufio.NewScanner(file) + scanner.Split(splitfunc) + + fishies := make([]int, 0) + for scanner.Scan() { + fish, err := strconv.Atoi(scanner.Text()) + if err != nil { + fmt.Printf("Error converting string to integer\n") + return nil, err + } + fishies = append(fishies, fish) + } + return fishies, nil +}