diff --git a/day6/day 6b/go.mod b/day6/day 6b/go.mod new file mode 100644 index 0000000..e938e2e --- /dev/null +++ b/day6/day 6b/go.mod @@ -0,0 +1,3 @@ +module PWD + +go 1.17 diff --git a/day6/day 6b/main.go b/day6/day 6b/main.go new file mode 100644 index 0000000..c7b0dd4 --- /dev/null +++ b/day6/day 6b/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + + content := returnContent("../input") + //content := returnContent("testInput") + fmt.Println(content) + + days := 256 + + var waitTime [9]int + + for _, v := range *content { + waitTime[v]++ + } + + fmt.Println(waitTime) + + for gen := 0; gen < days; gen++ { + justBred := waitTime[0] + + for daysToWait := range waitTime[:len(waitTime)-1] { + //Move array down a position + waitTime[daysToWait] = waitTime[daysToWait+1] + } + + //Add new fish and fish that have just bred (fallen off end of array) + waitTime[6] += justBred //start waiting other 6 days + waitTime[8] = justBred //new fishes + } + + var numFishes int + + for _, fishes := range waitTime { + numFishes += fishes + } + + fmt.Println(numFishes) +} + +func returnContent(path string) *[]int { + //read file and return it as an array of integers + + file, err := os.Open(path) + var content []int + + if err != nil { + fmt.Println("Unlucky, the file didn't open") + return &content + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + text := strings.Split(scanner.Text(), ",") + for _, v := range text { + num, _ := strconv.Atoi(v) + content = append(content, num) + } + } + + return &content +} diff --git a/day6/day 6b/testInput b/day6/day 6b/testInput new file mode 100644 index 0000000..a7af2b1 --- /dev/null +++ b/day6/day 6b/testInput @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file