Skip to content

Instantly share code, notes, and snippets.

@selfup
Created August 20, 2019 12:34
Show Gist options
  • Save selfup/79dd8b2b4591f3356eed4a7eae43bc78 to your computer and use it in GitHub Desktop.
Save selfup/79dd8b2b4591f3356eed4a7eae43bc78 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"sync"
)
type Electron struct {
}
type Proton struct {
}
type Neutron struct {
}
type Nucleus struct {
sync.Mutex
Protons []Proton
Neutrons []Neutron
}
type Atom struct {
sync.Mutex
Nucleus *Nucleus
Electrons []Electron
}
func CreateAtom() *Atom {
var electrons []Electron
var protons []Proton
var neutrons []Neutron
esize := rand.Intn(118)
psize := rand.Intn(118)
nsize := rand.Intn(118)
for i := 0; i < esize; i++ {
electrons = append(electrons, Electron{})
}
for i := 0; i < psize; i++ {
protons = append(protons, Proton{})
}
for i := 0; i < nsize; i++ {
neutrons = append(neutrons, Neutron{})
}
atom := new(Atom)
atom.Electrons = electrons
atom.Nucleus = new(Nucleus)
atom.Nucleus.Protons = protons
atom.Nucleus.Neutrons = neutrons
return atom
}
type UniverseBlock struct {
X int
Y int
Z int
charge int
Atom *Atom
}
type Universe struct {
sync.Mutex
UniverseBlocks []UniverseBlock
}
func (u *Universe) Create3DSpace(size int) {
byteRange := make([]byte, size)
var wg sync.WaitGroup
wg.Add(len(byteRange))
for x := range byteRange {
go func(xx int) {
for y := range byteRange {
for z := range byteRange {
atom := CreateAtom()
block := UniverseBlock{xx, y, z, 0, atom}
u.Lock()
u.UniverseBlocks = append(u.UniverseBlocks, block)
u.Unlock()
}
}
wg.Done()
}(x)
}
wg.Wait()
}
func main() {
size, err := strconv.Atoi(os.Args[1])
if err != nil {
panic(err)
}
universe := new(Universe)
universe.Create3DSpace(size)
fmt.Println(len(universe.UniverseBlocks))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment