emidio

Untitled

Apr 16th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. // Notice the changing of some imports for brevity
  4.  
  5. import f "fmt"
  6. import "net/http"
  7. import "io/ioutil"
  8. import str "strings"
  9. import "encoding/hex"
  10. import "sort"
  11.  
  12. // Just a couple of globals
  13.  
  14. var (   numbers []byte
  15.         bet []int
  16.         stars []int
  17.     )
  18.  
  19. // Quick and dirty way of handling a fatal error. Throws a panic upstream and the app quits.
  20.  
  21. func handleError(err error) {
  22.     if err != nil {
  23.         f.Printf("%v", err)
  24.         panic(nil)
  25.     }
  26. }
  27.  
  28. // Simple web page fetch and content extraction. No Parsing since only ONE <td> is used.
  29. // It can/WILL rot with time but keeps things simple for now.
  30. // At the and, numbers will contain a 1024 array of quantum random bytes
  31.  
  32. func load() {
  33.     resp, err := http.Get("http://150.203.48.55/RawHex.php");
  34.     handleError(err)
  35.    
  36.     defer resp.Body.Close()
  37.    
  38.     body, err := ioutil.ReadAll(resp.Body);
  39.     handleError(err)
  40.        
  41.     s   := string(body);
  42.     x1  := str.Index(s, "<td>")
  43.     x2  := str.Index(s, "</td>")
  44.    
  45.     numbers,
  46.         err = hex.DecodeString(s[x1 + 5:x2])
  47.     handleError(err)
  48. }
  49.  
  50. // Simple aid function to check the existence of a value inside an array.
  51.  
  52. func exists(a []int, b int) (bool) {
  53.     for _, v := range a {
  54.         if v == b {
  55.             return true
  56.         }
  57.     }
  58.     return false
  59. }
  60.  
  61. // Simple aid function to intersect two arrays - returns the number of equal elements
  62.  
  63. func overlap(a []int, b []int) (int) {
  64.     r := 0
  65.     for _, v := range b {
  66.         if exists(a, v) {
  67.             r++
  68.         }
  69.     }
  70.     return r
  71. }
  72.  
  73. // Iterate the numbers array and build the bets and stars array without duplicates.
  74.  
  75. func build() {
  76.     var n, s int // Notice that both n and s are initialized to 0 (natural null)
  77.    
  78.     bet = []int{}
  79.     stars = []int{}
  80.    
  81.     for i := 0; i < 1024; i++ {
  82.         switch {
  83.         case n < 5:
  84.             j := int(numbers[i] % 49)
  85.             if j == 0 {
  86.                 j++
  87.             }
  88.             if !exists(bet, j) {
  89.                 bet = append(bet, j)
  90.                 n++
  91.             }
  92.         case s < 2: 
  93.             j := int(numbers[i] % 11)
  94.             if j == 0 {
  95.                 j++
  96.             }
  97.             if !exists(bet, j) {
  98.                 stars = append(stars, j)
  99.                 s++
  100.             }
  101.         }
  102.     }
  103.    
  104.     sort.Ints(bet)
  105.     sort.Ints(bet)
  106. }
  107.  
  108. // Validates the bets array with a fixed set of rules
  109. // Clearly a bit more work since we don't enjoy list comprehensions as in other languages.
  110.  
  111. func validate() (bool) {
  112.     evens := []int {}
  113.     primes := []int {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
  114.     fibo := []int {1, 2, 3, 5, 8, 13, 21, 34}
  115.    
  116.     for i := 2; i < 49; i += 2 {
  117.         evens = append(evens, i)
  118.     }
  119.    
  120.     e := overlap(bet, evens)
  121.     p := overlap(bet, primes)
  122.     f := overlap(bet, fibo)
  123.    
  124.     return e < 3 && e > 0 && p == 2 && f == 0
  125. }
  126.  
  127.  
  128. func main() {
  129.     for {
  130.         load()
  131.         build()
  132.         if validate() {
  133.             break
  134.         }
  135.     }
  136.     f.Printf("Euromillions bet for today:\n%v \nStars: %v\nGood luck!", bet, stars)
  137. }
Advertisement
Add Comment
Please, Sign In to add comment