Skip to content

Instantly share code, notes, and snippets.

@semibran
Last active May 23, 2024 07:55
Show Gist options
  • Save semibran/005a9defcec54ea4060cdadf3dc03d83 to your computer and use it in GitHub Desktop.
Save semibran/005a9defcec54ea4060cdadf3dc03d83 to your computer and use it in GitHub Desktop.
Run Length Encoding file format pattern parser for Conway's Game of Life
module.exports = function decode(string) {
var cells = []
var ignore = false
var step = 1
var x = 0
var y = 0
var match, number
for (var i = 0; i < string.length; i++) {
if (ignore) {
if (string[i] === "\n") {
ignore = false
}
continue
}
switch (string[i]) {
case "#":
case "x":
case "!":
ignore = true
continue
case "$":
x = 0
y += step
continue
case "b":
x += step
step = 1
continue
case "o":
for (var j = 0; j < step; j++) {
cells.push(x++, y)
}
step = 1
continue
}
match = string.slice(i).match(/[0-9]+/)
if (match && !match.index) {
number = match[0]
step = parseInt(number)
i += number.length - 1
}
}
return cells
}
@tyminko
Copy link

tyminko commented Feb 9, 2021

Thanks for the code. One thing.. on line 21

case "$": x = 0; y++

should be y += step to accommodate multiple empty lines

@semibran
Copy link
Author

late to the punch but you're absolutely right. Good catch

@geuis
Copy link

geuis commented Jun 10, 2022

Found this code because I was trying to write my own decoder. But I'm not sure what you're supposed to do with the outputs of this function.

When I try the following pattern with it, I just get a long array of array of numbers.

#C this is a fake comment
x = 187, y = 187, rule = B3/S23
179bobo$179b2o$180bo$$$o$b2o$2o14bo25bo$14bobo26b2o128bo$15b2o25b2o
128bo$172b3o$$10bo26bo$8bobo27b2o128bo$9b2o26b2o128bo10bo$167b3o7bo$
177b3o$15bo$13bobo28bo$14b2o29b2o$44b2o$$$129bo$129bobo$129b2o$$$130bo
$128b2o$129b2o$40bo$41b2o$40b2o$52bo$53bo$51b3o17bobo$72b2o98bobo$72bo
99b2o$54bo118bo$55bo69bo$53b3o68bo$124b3o50bobo$177b2o$166bobo9bo$166b
2o$122bobo42bo$122b2o$123bo$$$120bobo$71bobo46b2o$72b2o47bo$72bo58bo$
130bo$130b3o$$72bo63bo$66bo3bobo61b2o$61bo5b2o2b2o62b2o$62bo3b2o$60b3o
$$108bo$108bobo3bo$108b2o2b2o$113b2o$$107bobo$107b2o$108bo$$83bo$84bo
2bo$44bo2bobo32b3o3b2o34bobo$42bobo3b2o37b2o35b2o$43b2o3bo76bo$$66bo
26bo18bo$67bo25bobo15bo$65b3o25b2o16b3o$$$77bobo34bo$78b2o32b2o$78bo
34b2o$133bo$77bo55bobo$78bo2bo51b2o$76b3o3b2o$81b2o$$$99bo$98bo$98b3o$
$$$$$$69b2o$70b2o$69bo$$$120b3o$120bo$121bo16b2o$137b2o$73b2o64bo$72bo
bo$74bo$81b2o$33bo48b2o33bo10b2o$33b2o46bo34b2o9b2o$32bobo81bobo10bo$$
109b2o$52b3o54bobo$54bo54bo$53bo$$51bo106b2o$51b2o105bobo$50bobo105bo$
$$$$98bo$97b2o$97bobo26b2o$126bobo$126bo$75b2o$74bobo$76bo$19bo$19b2o$
8bo9bobo54b2o$8b2o66b2o$7bobo65bo$$$13bo$13b2o$12bobo$$$$$$$$$$$$$$$$$
141b2o$140b2o29b2o$142bo28bobo$171bo$7b3o$9bo7b3o$8bo10bo128b2o26b2o$
18bo128b2o27bobo$149bo26bo$$12b3o$14bo128b2o25b2o$13bo128b2o26bobo$
144bo25bo14b2o$184b2o$186bo$$$6bo$6b2o$5bobo!

This is the RLE for https://copy.sh/life/?pattern=112p51_synth.

The results I'm seeing are like this:
image

@semibran
Copy link
Author

hi Charles -- what you're looking at is a flattened list of (x, y) pairs of living cells in the resulting pattern. So if you're looking to convert that to a 2d array you'd do something like this (untested):

function convertLifeCellsToGrid(cells) {
  let width = 0
  let height = 0

  for (let i = 0; i < cells.length; i += 2) {
    let x = cells[i]
    let y = cells[i + 1]
    width = Math.max(x + 1, width)
    height = Math.max(y + 1, height)
  }

  const grid = new Array(height)
    .fill()
    .map(_ => new Uint8ClampedArray(width))

  for (let i = 0; i < cells.length; i += 2) {
    let x = cells[i]
    let y = cells[i + 1]
    grid[y][x] = 1
  }

  return grid
}

I have an example implementation of life up that uses the original cell list if you're interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment