2021 Day 6 Complete!

This commit is contained in:
Brian Buller 2021-12-06 16:09:34 -06:00
parent 8545249be7
commit dba8cc0e0f
3 changed files with 86 additions and 0 deletions

1
2021/day06/input Normal file
View File

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

84
2021/day06/main.go Normal file
View File

@ -0,0 +1,84 @@
package main
import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()[0]
fishStr := strings.Split(inp, ",")
part1(fishStr)
part2(fishStr)
}
func part1(fishStr []string) {
var fish []int
for i := range fishStr {
fish = append(fish, h.Atoi(fishStr[i]))
}
for i := 0; i < 80; i++ {
var newFish int
for l := range fish {
if fish[l] == 0 {
fish[l] = 6
newFish++
} else {
fish[l]--
}
}
for ; newFish > 0; newFish-- {
fish = append(fish, 8)
}
}
fmt.Println()
fmt.Println("# Part 1")
fmt.Println(len(fish), "fish")
fmt.Println()
}
// For 256 days there are too many to do it the same way. Instead we track the number of fish per cycle
func part2(fishStr []string) {
var fish []int
for i := range fishStr {
fish = append(fish, h.Atoi(fishStr[i]))
}
fishMap := make(map[int]int)
for i := 0; i < 9; i++ {
fishMap[i] = 0
}
for i := range fish {
fishMap[fish[i]]++
}
for i := 0; i < 256; i++ {
zeros := fishMap[0]
fishMap[0] = fishMap[1]
fishMap[1] = fishMap[2]
fishMap[2] = fishMap[3]
fishMap[3] = fishMap[4]
fishMap[4] = fishMap[5]
fishMap[5] = fishMap[6]
fishMap[6] = fishMap[7] + zeros
fishMap[7] = fishMap[8]
fishMap[8] = zeros
}
fmt.Println()
fmt.Println("# Part2")
total := 0
for k := range fishMap {
total += fishMap[k]
}
fmt.Println(total, "fish")
}
func printStatus(fish map[int]int) {
var total int
for i := 0; i < 8; i++ {
fmt.Printf("[%d: %d] ", i, fish[i])
total += fish[i]
}
fmt.Println("Total:", total)
}

1
2021/day06/testinput Normal file
View File

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