From 01699081b420b51cae36ead58e5177d982e736aa Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Sun, 5 Dec 2021 21:36:24 -0800 Subject: [PATCH] 2021 Day 6 --- 2021/day6/main.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 2021/day6/main.go diff --git a/2021/day6/main.go b/2021/day6/main.go new file mode 100644 index 0000000..0e35d9c --- /dev/null +++ b/2021/day6/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/jdkaplan/advent-of-code/aoc" +) + +func main() { + text := aoc.Input().ReadFile("day6.txt") + fmt.Println(part1(text)) + fmt.Println(part2(text)) +} + +type Fish struct { + ticks int +} + +func NewFish() *Fish { + return &Fish{8} +} + +func (f *Fish) Tick() bool { + if f.ticks == 0 { + f.ticks = 6 + return true + } + f.ticks-- + return false +} + +func part1(text string) int { + var fish []*Fish + for _, n := range strings.Split(text, ",") { + fish = append(fish, &Fish{aoc.MustInt(n)}) + } + for i := 0; i < 80; i++ { + for _, f := range fish { + if f.Tick() { + fish = append(fish, NewFish()) + } + } + } + return len(fish) +} + +func part2(text string) int { + delays := make(map[int]int) + for _, n := range strings.Split(text, ",") { + delays[aoc.MustInt(n)]++ + } + for i := 0; i < 256; i++ { + newDelays := make(map[int]int) + + // Reproduce + newDelays[8] = delays[0] + + // Tick + for d := 1; d <= 8; d++ { + newDelays[d-1] = delays[d] + } + newDelays[6] += delays[0] + + delays = newDelays + } + + var total int + for _, n := range delays { + total += n + } + return total +}