save
authorsgf <sgf.dma@gmail.com>
Thu, 3 Feb 2022 21:00:36 +0000 (00:00 +0300)
committersgf <sgf.dma@gmail.com>
Thu, 3 Feb 2022 21:00:36 +0000 (00:00 +0300)
day6/main.go
main.go

index 557c8c9..627e2fe 100644 (file)
@@ -10,7 +10,7 @@ import (
     "bufio"
 )
 
-type Age int
+type BirthTimer int
 
 func SplitComma(data []byte, atEOF bool) (advance int, token []byte, err error) {
     token = make([]byte, 0)
@@ -30,20 +30,20 @@ func SplitComma(data []byte, atEOF bool) (advance int, token []byte, err error)
     return
 }
 
-func readInput(r io.Reader) (ages []Age, err error) {
+func readInput(r io.Reader) (ages []BirthTimer, err error) {
     s := bufio.NewScanner(r)
 
     s.Split(SplitComma)
-    ages = make([]Age, 0, 12)
+    ages = make([]BirthTimer, 0, 12)
     for s.Scan() {
         x, err := strconv.Atoi(s.Text())
         if err != nil { return nil, err }
-        ages = append(ages, Age(x))
+        ages = append(ages, BirthTimer(x))
     }
     return ages, err
 }
 
-func oneDay(ages []Age) ([]Age) {
+func oneDay(ages []BirthTimer) ([]BirthTimer) {
     n := 0
     for i := 0; i < len(ages); i++ {
         if ages[i] == 0 {
@@ -55,7 +55,7 @@ func oneDay(ages []Age) ([]Age) {
     }
 
 
-    childs := make([]Age, n)
+    childs := make([]BirthTimer, n)
     for j := 0; j < len(childs); j++ {
         childs[j] = 8
     }
@@ -82,21 +82,72 @@ func RunF1(input string) {
     fmt.Printf("Answer1: %v\n", len(ages))
 }
 
+type Fish struct {
+    age int
+    childs []Fish
+}
+
+func readInput2(r io.Reader) (fishes []Fish, err error) {
+    s := bufio.NewScanner(r)
+
+    s.Split(SplitComma)
+    fishes = make([]Fish, 0, 12)
+    for s.Scan() {
+        x, err := strconv.Atoi(s.Text())
+        if err != nil { return nil, err }
+        fishes = append(fishes, Fish{age: 8 - x})
+    }
+    return fishes, err
+}
+
+func live(fish *Fish) {
+    if fish.age < 8 {
+        return
+    }
+    fmt.Printf("   Fish: %v\n", fish)
+    fish.age -= 1
+    for fish.age >= 7 {
+        fish.age -= 7
+        fish.childs = append(fish.childs, Fish{age: fish.age})
+        fmt.Printf("   Fish child: %v\n", fish)
+    }
+}
+
+func liveDescendants(fish *Fish) {
+    live(fish)
+    fmt.Printf("Fish lived: %v\n", fish)
+    for i := 0; i < len(fish.childs); i++ {
+        fmt.Printf("Living descendant %d\n", i)
+        liveDescendants(&fish.childs[i])
+    }
+}
+
+func calculateAge(bt BirthTimer, days int) int {
+    return 8 - int(bt) + days
+}
+
 func RunF2(input string) {
     h0, err := os.Open(filepath.Join("day6", input))
     if err != nil { return }
     defer h0.Close()
 
-    ages, err := readInput(h0)
+    fishes, err := readInput2(h0)
+    fmt.Printf("Starting with fishes: %v\n", fishes)
+    for _, x := range fishes[:1] {
+        fmt.Printf("### Live top-level fish %v\n", x)
+        x.age = x.age + 9
+        liveDescendants(&x)
+    }
     /*
     for d := 0; d < 18; d++ {
         ages = oneDay(ages)
         fmt.Printf("Day %v: %v\n", d, ages)
     }*/
-    for d := 0; d < 256; d++ {
+    /*
+    for d := 0; d < 18; d++ {
         ages = oneDay(ages)
         //fmt.Printf("Day %v: %v\n", d, ages)
-    }
+    }*/
 
-    fmt.Printf("Answer2: %v\n", len(ages))
+    //fmt.Printf("Answer2: %v\n", len(ages))
 }
diff --git a/main.go b/main.go
index d69ad73..10ffe7f 100644 (file)
--- a/main.go
+++ b/main.go
@@ -14,6 +14,6 @@ func main() {
     //day1.RunF1("day1/input.txt")
     //day1.RunF2("day1/input.txt")
 
-    //day6.RunF1("input.txt")
+    day6.RunF1("in.txt")
     day6.RunF2("in.txt")
 }