Commits

Shlomi Fish committed faf261d

Go program yields correct results.

Seems kinda slow though. Perhaps parsing and converting back to a string
is time consuming.

  • Participants
  • Parent commits 564b26f

Comments (0)

Files changed (1)

project-euler/189/euler_189.go

 
 import (
     "fmt"
+    "strconv"
+    "strings"
 )
 
 type ColorInt int32
     colors[2] ColorInt
 }
 
+func GetLastDigit(s string) ColorInt {
+    var ret, _ = strconv.ParseInt(s[(len(s)-1):len(s)], 10, 32)
+    return ColorInt(ret)
+}
 func CreateColorArray() ColorArray {
     return ColorArray{n:0, colors:[2]ColorInt{0,0}}
 }
         data.derived[""] = deriv
     }
 
-    return 0
+    for h := 1 ; h <= wanted_h-1; h++ {
+        var this_seqs map[string]int = data.seq
+        var prev_deriveds map[string]map[string]int = data.derived
+
+        var next_data = RowData{seq:make(map[string]int), derived:make(map[string]map[string]int), count:0}
+
+        for seq, seq_count := range this_seqs {
+            for left, left_count := range (prev_deriveds[seq[0:len(seq)-1]]) {
+                var delta int = seq_count * left_count
+                var lefter_a ColorArray = colors[GetLastDigit(seq)][GetLastDigit(left)]
+                for ler_idx := ColorInt(0) ; ler_idx < lefter_a.n ; ler_idx++ {
+                    var lefter_tri_color = lefter_a.colors[ler_idx]
+                    var leftest_a = l_colors[lefter_tri_color]
+
+                    for lest_idx := ColorInt(0) ; lest_idx < leftest_a.n ; lest_idx++ {
+                        var leftest_color = leftest_a.colors[lest_idx]
+                        var str = strings.Join([]string{left, strconv.FormatInt(int64(leftest_color), 10)}, "")
+
+                        next_data.count += delta
+                        next_data.seq[str] += delta
+                        _, ok := next_data.derived[seq]
+                        if (! ok) {
+                            next_data.derived[seq] = make(map[string]int)
+                        }
+                        next_data.derived[seq][str] += left_count
+                    }
+                }
+            }
+        }
+
+        data = next_data
+        fmt.Printf("Count[%d] = %d\n", h+1, data.count)
+    }
+
+    return data.count
 }
 
 func main() {
-    /*
-    var this_range[2] SquareRange
-    this_range[0] = SquareRange {CreateSquare(1), CreateSquare(3) }
-    this_range[1] = SquareRange {CreateSquare(2), CreateSquare(4) }
-    */
 
     for i := ColorInt(0); i < num_colors; i++ {
         for j := ColorInt(0); j < num_colors; j++ {
         }
         l_colors[i] = arr
     }
+    {
+        fmt.Printf("%d should be 3*2*2*2 == 24\n", my_find(2))
+    }
+    fmt.Printf("Result[8] = %d\n", my_find(8))
 
-    /*
-    for ((next_range.Diff() >= 0) && (next_range.SqDiff() > max)) {
-
-    var count int64 = 0
-    // var occurences map[int64]int8
-    occurences := make(map[int64]int8)
-    var mod = 0
-    var max int64 = 1000000
-    for ((this_range[mod].Diff() > 0) && (this_range[mod].SqDiff() <= max)) {
-        // count += (this_range[mod].Diff())
-        {
-            var iter_range SquareRange = this_range[mod]
-            for (iter_range.Diff() > 0) {
-                var n = iter_range.SqDiff()
-                c, ok := occurences[n]
-                if (ok) {
-                    if (c != 11) {
-                        if (c == 10) {
-                            count--
-                        }
-                        occurences[n] = c+1
-                    }
-                } else {
-                    count++;
-                    occurences[n] = 1
-                }
-                iter_range.bottom.Inc2()
-            }
-        }
-
-        var next_range SquareRange = this_range[mod]
-        next_range.top.Inc2()
-
-        // var prev_bottom Square = next_range.bottom
-
-        // for (next_range.top.value - next_range.bottom.value > 1000000) {
-        for ((next_range.Diff() >= 0) && (next_range.SqDiff() > max)) {
-            next_range.bottom.Inc2()
-        }
-
-        this_range[mod] = next_range
-        mod = 1 - mod
-    }
-
-    fmt.Println("count = ", count)
-    */
 }