day 6b complete

This commit is contained in:
Luke Else 2021-12-06 21:29:26 +00:00
parent 77fd77b4cb
commit 057aca2a3f
3 changed files with 75 additions and 0 deletions

3
day6/day 6b/go.mod Normal file
View File

@ -0,0 +1,3 @@
module PWD
go 1.17

71
day6/day 6b/main.go Normal file
View File

@ -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
}

1
day6/day 6b/testInput Normal file
View File

@ -0,0 +1 @@
3,4,3,1,2