summaryrefslogtreecommitdiff
path: root/p61/p61.go
blob: e80349842e61e633c923d1d6df486a01f829d26f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main

import (
  "fmt"
)


type IndexedSet struct {
  v,i int
}

var it = 0

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, IndexedSet{v:n, i:it})
      }
    }
    n = form(i)
  }
}
func fillByNumber( dict map[int][]IndexedSet, num []IndexedSet) {
  for _,n := range num {
    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 := []IndexedSet{}
  sqrt := []IndexedSet{}
  pent := []IndexedSet{}
  hexa := []IndexedSet{}
  hept := []IndexedSet{}
  octa := []IndexedSet{}

  numMap := make(map[int][]IndexedSet)


  fillByFormule(&trin, func(x int) int { return (x*(x+1))>>1 })
  fillByFormule(&sqrt, func(x int) int { return x*x })
  fillByFormule(&pent, func(x int) int { return (x*(3*x-1))>>1 })
  fillByFormule(&hexa, func(x int) int { return x*(2*x-1) })
  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)
  fillByNumber(numMap, sqrt)
  fillByNumber(numMap, pent)
  fillByNumber(numMap, hexa)
  fillByNumber(numMap, hept)

  pile := []IndexedSet{}

  pile = append( pile, octa...)


  cycle := []IndexedSet{}

  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]
      }
    }


    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
    }

    n_2 := n.v % 100

    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)
          }
        }
      }
    }
  }
}