Skip to content

Commit

Permalink
Solution for three challenges (noops-challenge#21)
Browse files Browse the repository at this point in the history
* feature: add solution for 10print

* feature: add solution to the hexcode challenge

* feature: add solution for conways game of life

* chore: add screenshot to conway solution
  • Loading branch information
benrgreene authored and doolittle committed Jun 29, 2019
1 parent 31270cb commit f412686
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions challenges/10print/entries/benrgreene/10print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
while(1){process.stdout.write(Math.random() > .5 ? '\\' : '/')}
13 changes: 13 additions & 0 deletions challenges/10print/entries/benrgreene/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 10Print JS

Solution to the 10Print challenge

## Run the program

This runs in terminal, and requires that NodeJS is installed

```
node 10print.js
```

**NOTE**: this runs infinitely, hit 'Control + C' to exit the program
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions challenges/conways-life/entries/benrgreene/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Conway's Game of Life

JS based implementation of Conway's game of life.

## Run

This requires NodeJS, and is run in the terminal:

```
node conway.js
```

## Explanation

Here's the unminified JS code to look over:

```
const nextStage = (array) => {
const newArray = [];
for (let i = 0; i < array.length; i++) {
newArray[i] = [];
for (let j = 0; j < array[i].length; j++) {
// count up all the different cells around the current cell.
let l = [[i, j-1],[i, j+1],[i-1, j-1],[i-1, j],[i-1, j+1],[i+1, j-1],[i+1, j],[i+1, j+1]].reduce((count, p) => {
let a = array[p[0]] || []
return count + (a[p[1]] || 0)
}, 0);
// check how many living cells are around, and based on that AND current status, set the new status of the cell
newArray[i][j] = array[i][j] == 1 ? (l < 2 ? 0 : (l < 4 ? 1 : (l > 3 ? 0 : 1))) : (l == 3 ? 1 : 0)
}
}
return newArray;
}
// build a new random board, first need a new array representing the different rows
let board = new Array(20).fill(0).map(() => {
// build an array of the columns in the rows.
return new Array(20).fill(0).map(() => {
// random number of whether the cell starts with life
return Math.floor(Math.random() * 2)
})
})
// infinitely rebuild the board
while(1) {
board = nextStage(board);
console.log(board);
}
```
1 change: 1 addition & 0 deletions challenges/conways-life/entries/benrgreene/conway.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions challenges/hexcode/entries/benrgreene/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Hexcode

This is a JS based solution to the hexcode challenge.

## Run

This requires NodeJS to run:

```
node hexcode.js
```
1 change: 1 addition & 0 deletions challenges/hexcode/entries/benrgreene/hexcode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let i=0;while(i<16777216){let a=i.toString(16);console.log(`#${'0'.repeat(6-a.length)}${a}`);i++}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f412686

Please sign in to comment.