npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-gol-hashlife

v0.1.1

Published

Hashlife algorithm for Conway's Game of Life.

Downloads

8

Readme

npm version Build Status Built with Grunt

Use case

Hashlife is an algorithm used to evolve Conway's Game of Life simulation. If you are not familiar with this cellular automata model or other Life-algorithms, you might want to take a look at the following guidelines for using hashlife:

You should use hashlife for:

  • Patterns containing a lot of redundancy (sub-patterns that show up often).
  • Far-future evolution of the simulation.

Conversely, you shouldn't use hashlife for:

  • Highly chaotic patterns.
  • Evolving the simulation one generation at a time.
  • Memory light execution.

Installation

Install via npm: npm install node-gol-hashlife

The dist/ directory contains both a normal (gol.js) as well as a minified version of the library (gol.min.js). Import either into Node.js using require("gol") or directly include in the browser using <script src="gol.min.js"></script>

Usage

Lets take a look at a basic example. Say we wish to view the evolution of the glider pattern.

Create a universe

Universe size is important. The amount of generations we can simulate in the future depends on it. A universe's size is always a power of two, and a universe with size 2n can simulate 2n - 2 generations ahead.

Suppose we are interested in the glider's evolution two generations into the future. For that, we will need a universe of size 8x8, since 8 = 2^3 which means we can now simulate the pattern 2(3 - 1) generations ahead.

var gol = require('gol');
var sim = new gol.Simulation(3);  // creates a (2^3)x(2^3) = 8x8 universe

Set initial pattern

Now that we have an empty universe, lets populate it with the initial glider pattern:

/*
    The glider pattern looks like this:
    t = 0      t = 1      t = 2
    -----      -----      -----
    0 1 0 0    0 0 0 0    0 0 0 0
    0 0 1 0    1 0 1 0    0 0 1 0
    1 1 1 0    0 1 1 0    1 0 1 0
    0 0 0 0    0 1 0 0    0 1 1 0
*/

// These are the living cell coordinates of the glider at t = 0
// (origin is at the center of the universe)
var gen0 = [
    {x: -2, y: -1},
    {x: -1, y: -1},
    {x: -1, y:  1},
    {x:  0, y: -1},
    {x:  0, y:  0}
];

// Populate the universe with our glider
for (i in gen0) {
    p = gen0[i]
    sim.set(p.x, p.y)  // Sets the cell to 'alive'
}

Simulate and inspect

Now that we have the glider in the universe, lets inspect its evolution. The simulation object allows us to view generations that are a power of two ahead in time:

// Get all living cells 2 generations into the future:
population = sim.get(1)  // 1 as in 2^1

/* 
	'population' now contains the following positions:
    population == [
        {x: -2, y: -1},
        {x: -1, y: -2},
        {x:  0, y: -2},
        {x:  0, y: -1},
        {x:  0, y:  0}
    ]
 */

Note that the hashlife algorithm evaluates this future state for the central quadrant of the universe. In our case we evaluated cells from (-2, -2) up to (2, 2) two generations into the future. Had we wanted to evaluate a larger neighbourhood, we'd need a larger universe.

License

This software is licensed under the MIT License. See the LICENSE file for more information.