Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- // Notice the changing of some imports for brevity
- import f "fmt"
- import "net/http"
- import "io/ioutil"
- import str "strings"
- import "encoding/hex"
- import "sort"
- // Just a couple of globals
- var ( numbers []byte
- bet []int
- stars []int
- )
- // Quick and dirty way of handling a fatal error. Throws a panic upstream and the app quits.
- func handleError(err error) {
- if err != nil {
- f.Printf("%v", err)
- panic(nil)
- }
- }
- // Simple web page fetch and content extraction. No Parsing since only ONE <td> is used.
- // It can/WILL rot with time but keeps things simple for now.
- // At the and, numbers will contain a 1024 array of quantum random bytes
- func load() {
- resp, err := http.Get("http://150.203.48.55/RawHex.php");
- handleError(err)
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body);
- handleError(err)
- s := string(body);
- x1 := str.Index(s, "<td>")
- x2 := str.Index(s, "</td>")
- numbers,
- err = hex.DecodeString(s[x1 + 5:x2])
- handleError(err)
- }
- // Simple aid function to check the existence of a value inside an array.
- func exists(a []int, b int) (bool) {
- for _, v := range a {
- if v == b {
- return true
- }
- }
- return false
- }
- // Simple aid function to intersect two arrays - returns the number of equal elements
- func overlap(a []int, b []int) (int) {
- r := 0
- for _, v := range b {
- if exists(a, v) {
- r++
- }
- }
- return r
- }
- // Iterate the numbers array and build the bets and stars array without duplicates.
- func build() {
- var n, s int // Notice that both n and s are initialized to 0 (natural null)
- bet = []int{}
- stars = []int{}
- for i := 0; i < 1024; i++ {
- switch {
- case n < 5:
- j := int(numbers[i] % 49)
- if j == 0 {
- j++
- }
- if !exists(bet, j) {
- bet = append(bet, j)
- n++
- }
- case s < 2:
- j := int(numbers[i] % 11)
- if j == 0 {
- j++
- }
- if !exists(bet, j) {
- stars = append(stars, j)
- s++
- }
- }
- }
- sort.Ints(bet)
- sort.Ints(bet)
- }
- // Validates the bets array with a fixed set of rules
- // Clearly a bit more work since we don't enjoy list comprehensions as in other languages.
- func validate() (bool) {
- evens := []int {}
- primes := []int {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
- fibo := []int {1, 2, 3, 5, 8, 13, 21, 34}
- for i := 2; i < 49; i += 2 {
- evens = append(evens, i)
- }
- e := overlap(bet, evens)
- p := overlap(bet, primes)
- f := overlap(bet, fibo)
- return e < 3 && e > 0 && p == 2 && f == 0
- }
- func main() {
- for {
- load()
- build()
- if validate() {
- break
- }
- }
- f.Printf("Euromillions bet for today:\n%v \nStars: %v\nGood luck!", bet, stars)
- }
Advertisement
Add Comment
Please, Sign In to add comment