From 7518efc103548d5976c9de4570f7c91916d593bc Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Mon, 6 Dec 2021 07:41:51 +0100 Subject: [PATCH] 06: part1+2 --- 6/input | 1 + 6/part1.go | 33 +++++++++++++++++++++++++++++++++ 6/part2.go | 39 +++++++++++++++++++++++++++++++++++++++ 6/testinput | 1 + 4 files changed, 74 insertions(+) create mode 100644 6/input create mode 100644 6/part1.go create mode 100644 6/part2.go create mode 100644 6/testinput diff --git a/6/input b/6/input new file mode 100644 index 0000000..3d35529 --- /dev/null +++ b/6/input @@ -0,0 +1 @@ +3,1,5,4,4,4,5,3,4,4,1,4,2,3,1,3,3,2,3,2,5,1,1,4,4,3,2,4,2,4,1,5,3,3,2,2,2,5,5,1,3,4,5,1,5,5,1,1,1,4,3,2,3,3,3,4,4,4,5,5,1,3,3,5,4,5,5,5,1,1,2,4,3,4,5,4,5,2,2,3,5,2,1,2,4,3,5,1,3,1,4,4,1,3,2,3,2,4,5,2,4,1,4,3,1,3,1,5,1,3,5,4,3,1,5,3,3,5,4,2,3,4,1,2,1,1,4,4,4,3,1,1,1,1,1,4,2,5,1,1,2,1,5,3,4,1,5,4,1,3,3,1,4,4,5,3,1,1,3,3,3,1,1,5,4,2,5,1,1,5,5,1,4,2,2,5,3,1,1,3,3,5,3,3,2,4,3,2,5,2,5,4,5,4,3,2,4,3,5,1,2,2,4,3,1,5,5,1,3,1,3,2,2,4,5,4,2,3,2,3,4,1,3,4,2,5,4,4,2,2,1,4,1,5,1,5,4,3,3,3,3,3,5,2,1,5,5,3,5,2,1,1,4,2,2,5,1,4,3,3,4,4,2,3,2,1,3,1,5,2,1,5,1,3,1,4,2,4,5,1,4,5,5,3,5,1,5,4,1,3,4,1,1,4,5,5,2,1,3,3 diff --git a/6/part1.go b/6/part1.go new file mode 100644 index 0000000..de354c5 --- /dev/null +++ b/6/part1.go @@ -0,0 +1,33 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + var fishes []int + + s := bufio.NewScanner(os.Stdin) + s.Scan() + for _, fishStr := range strings.Split(s.Text(), ",") { + fish, _ := strconv.Atoi(fishStr) + fishes = append(fishes, fish) + } + + for x := 0; x < 80; x++ { + for i := range fishes { + if fishes[i] == 0 { + fishes[i] = 6 + fishes = append(fishes, 8) + } else { + fishes[i]-- + } + } + } + + fmt.Println(len(fishes)) +} diff --git a/6/part2.go b/6/part2.go new file mode 100644 index 0000000..d91576f --- /dev/null +++ b/6/part2.go @@ -0,0 +1,39 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + // https://en.wiktionary.org/wiki/fishes#English + fishes := map[int]int{} + + s := bufio.NewScanner(os.Stdin) + s.Scan() + for _, fishStr := range strings.Split(s.Text(), ",") { + fish, _ := strconv.Atoi(fishStr) + fishes[fish]++ + } + + for x := 0; x < 256; x++ { + newFishes := map[int]int{} + // handle usual decrements + for i := 8; i > 0; i-- { + newFishes[i-1] = fishes[i] + } + // handle the new fishes + newFishes[6] += fishes[0] + newFishes[8] += fishes[0] + fishes = newFishes + } + + sum := 0 + for _, count := range fishes { + sum += count + } + fmt.Println(sum) +} diff --git a/6/testinput b/6/testinput new file mode 100644 index 0000000..a7af2b1 --- /dev/null +++ b/6/testinput @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file