From 726f4a24a2c503aaf4bb0038c36d427a25f9a409 Mon Sep 17 00:00:00 2001 From: ache Date: Wed, 12 Sep 2018 03:32:42 +0200 Subject: Problem 61 solution --- p61/p61 | Bin 0 -> 1911589 bytes p61/p61.go | 121 ++++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 67 insertions(+), 54 deletions(-) create mode 100755 p61/p61 diff --git a/p61/p61 b/p61/p61 new file mode 100755 index 0000000..2f874a4 Binary files /dev/null and b/p61/p61 differ diff --git a/p61/p61.go b/p61/p61.go index ed7d47f..e803498 100644 --- a/p61/p61.go +++ b/p61/p61.go @@ -5,44 +5,55 @@ import ( ) -type num struct { +type IndexedSet struct { v,i int } var it = 0 -func fillByFormule( slc *[]int, form func (int) int) { +func fillByFormule( slc *[]IndexedSet, form func (int) int) { it++ for n, i := 0, 0 ; n < 10000 ; i++ { if n > 999 && n < 10000 { if n % 100 > 9 { - *slc = append(*slc, num{v=n, i=it}) + *slc = append(*slc, IndexedSet{v:n, i:it}) } } n = form(i) } } -func fillByNumber( dict map[int][]int, num []int) { +func fillByNumber( dict map[int][]IndexedSet, num []IndexedSet) { for _,n := range num { - dict[n/100] = append(dict[n/100], n%100) + dict[n.v/100] = append(dict[n.v/100], n) } } +func containsIndex( ref []IndexedSet, index int) bool { + for _,n := range ref { + if n.i == index { + return true + } + } + return false +} +func containsValue( ref []IndexedSet, value int) bool { + for _,n := range ref { + if n.v == value { + return true + } + } + return false +} func main () { - trin := []int{} - sqrt := []int{} - pent := []int{} - hexa := []int{} - hept := []int{} - octa := []int{} + trin := []IndexedSet{} + sqrt := []IndexedSet{} + pent := []IndexedSet{} + hexa := []IndexedSet{} + hept := []IndexedSet{} + octa := []IndexedSet{} - numMap := make(map[int][]num) - /* - hexaMap := make(map[int][]int) - heptMap := make(map[int][]int) - octaMap := make(map[int][]int) - */ + numMap := make(map[int][]IndexedSet) fillByFormule(&trin, func(x int) int { return (x*(x+1))>>1 }) @@ -52,57 +63,59 @@ func main () { fillByFormule(&hept, func(x int) int { return (x*(5*x-3))>>1 }) fillByFormule(&octa, func(x int) int { return x*(3*x-2) }) - fillByNumber(numMap, trin, 1) - fillByNumber(numMap, sqrt, 2) - /* - fillByNumber(sqrtMap, sqrt) - fillByNumber(pentMap, pent) - */ - - fmt.Println(trin) - fmt.Println(numMap) + fillByNumber(numMap, trin) + fillByNumber(numMap, sqrt) + fillByNumber(numMap, pent) + fillByNumber(numMap, hexa) + fillByNumber(numMap, hept) - pile := []int{} + pile := []IndexedSet{} - empiler := func(i []int) { - pile = append(pile, i...) - } + pile = append( pile, octa...) - empiler(pent) + cycle := []IndexedSet{} - for len(pile) != 0{ - n_1 := n / 100 - n_2 := n % 100 + for len(pile) > 0 { + if len(cycle) >= 6 { + if (cycle[0].v/100) == (cycle[5].v%100) { + fmt.Println(cycle) + sum := 0 + for _,v := range cycle { + sum += v.v + } + fmt.Println(sum) + } + for pile[len(pile)-1].i != -1 { + pile = pile[:len(pile)-1] + } + } - cycle := []int{n8} - numberType := []int{0} - for len(cycle) != 3 { - + n := pile[len(pile)-1] + pile = pile[:len(pile)-1] + if n.i == -1 { + if len(cycle) > 0 { + cycle = cycle[:len(cycle)-1] + } else { + fmt.Println("Houston, we got a problem") + } + continue } - if _,ok := numMap[n_2] ; ok { - for + n_2 := n.v % 100 - for _, n1_2 := range numMap[n_2] { - if _,ok := trinMap[n1_2] ; ok { - for _, n2 := range trinMap[n1_2] { - n2_2 := n2 % 100 - if n2_2 == n8_1 { - fmt.Println(n1_2, n2_2, n8_2) - } + if !containsIndex(cycle, n.i) && !containsValue( cycle, n.v) { + cycle = append(cycle, n) + pile = append(pile, IndexedSet{ v:-1, i: -1} ) + if vList,ok := numMap[n_2] ; ok && vList != nil { + for _,v := range vList { + if !containsIndex( cycle, v.i ) { + pile = append(pile, v) } } } } } - - fmt.Println(len(trin)) - fmt.Println(len(sqrt)) - fmt.Println(len(pent)) - fmt.Println(len(hexa)) - fmt.Println(len(hept)) - fmt.Println(len(octa)) } -- cgit v1.2.3