Guest User

Untitled

a guest
Mar 31st, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "bytes"
  5.     "fmt"
  6.     "strconv"
  7.  
  8.     "github.com/dolthub/swiss"
  9. )
  10.  
  11. const MAX_DATA = 12000000
  12.  
  13. var i2ch []byte
  14.  
  15. func init() {
  16.     i2ch = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'B', 'c', 'D', 'e', 'F'}
  17. }
  18. func get_first_digit(d float64) int {
  19.     for d > 10 {
  20.         d /= 10
  21.     }
  22.     return int(d)
  23. }
  24. func to_rhex(v int) string {
  25.     hex := bytes.Buffer{}
  26.     for v > 0 {
  27.         hex.WriteByte(i2ch[v%16])
  28.         v /= 16
  29.     }
  30.     return hex.String()
  31. }
  32. func set_or_inc(m *swiss.Map[string, float64], key string, set, inc int, ctr *int) {
  33.     if old, ok := m.Get(key); !ok {
  34.         m.Put(key, float64(set))
  35.     } else {
  36.         m.Put(key, old+float64(inc))
  37.         *ctr += 1
  38.     }
  39. }
  40. func main() {
  41.     m := swiss.NewMap[string, float64](MAX_DATA)
  42.     dup1, dup2, dup3 := 0, 0, 0
  43.     for z := MAX_DATA; z > 0; z-- {
  44.         val2 := MAX_DATA - z
  45.         val3 := MAX_DATA*2 - z
  46.         key1 := strconv.Itoa(z)
  47.         key2 := strconv.Itoa(val2)
  48.         key3 := to_rhex(val3)
  49.         set_or_inc(m, key1, z, val2, &dup1)
  50.         set_or_inc(m, key2, val2, val3, &dup2)
  51.         set_or_inc(m, key3, val3, z, &dup3)
  52.     }
  53.     fmt.Println(dup1, dup2, dup3)
  54.     total, verify, count := 0, 0, 0
  55.     m.Iter(func(k string, v float64) bool {
  56.         total += get_first_digit(v)
  57.         verify += len(k)
  58.         count += 1
  59.         return true
  60.     })
  61.     fmt.Println(total, verify, count)
  62. }
  63.  
Add Comment
Please, Sign In to add comment