-
-
Notifications
You must be signed in to change notification settings - Fork 97
Add game-of-life exercice
#780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
resu-xuniL
wants to merge
4
commits into
exercism:main
Choose a base branch
from
resu-xuniL:game-of-life
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Instructions | ||
|
|
||
| After each generation, the cells interact with their eight neighbors, which are cells adjacent horizontally, vertically, or diagonally. | ||
|
|
||
| The following rules are applied to each cell: | ||
|
|
||
| - Any live cell with two or three live neighbors lives on. | ||
| - Any dead cell with exactly three live neighbors becomes a live cell. | ||
| - All other cells die or stay dead. | ||
|
|
||
| Given a matrix of 1s and 0s (corresponding to live and dead cells), apply the rules to each cell, and return the next generation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Introduction | ||
|
|
||
| [Conway's Game of Life][game-of-life] is a fascinating cellular automaton created by the British mathematician John Horton Conway in 1970. | ||
|
|
||
| The game consists of a two-dimensional grid of cells that can either be "alive" or "dead." | ||
|
|
||
| After each generation, the cells interact with their eight neighbors via a set of rules, which define the new generation. | ||
|
|
||
| [game-of-life]: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "authors": [ | ||
| "resu-xuniL" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "game_of_life.sh" | ||
| ], | ||
| "test": [ | ||
| "game_of_life.bats" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.sh" | ||
| ] | ||
| }, | ||
| "blurb": "Implement Conway's Game of Life.", | ||
| "source": "Wikipedia", | ||
| "source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| declare -A matrix | ||
| declare -i height width | ||
|
|
||
| create_matrix() { | ||
| local -i col row=0 | ||
|
|
||
| while read -r line; do | ||
| for (( col = 0; col < ${#line}; col++ )); do | ||
| matrix[${col},${row}]=${line:col:1} | ||
| done | ||
|
|
||
| height=${#line} | ||
| (( row++ )) | ||
| done < <(printf "%s\n" "${@}") | ||
|
|
||
| width=$row | ||
| } | ||
|
|
||
| tick() { | ||
| local -A next_matrix | ||
| local -i row col countLiveCell | ||
|
|
||
| for (( row = 0; row < height; row++ )); do | ||
| for (( col = 0; col < width; col++ )); do | ||
| countLiveCell=$(check_neighborhood "$col" "$row") | ||
|
|
||
| if (( countLiveCell == 3 || matrix[${col},${row}] == 1 && countLiveCell == 2 )); then | ||
| next_matrix[${col},${row}]=1 | ||
| else | ||
| next_matrix[${col},${row}]=0 | ||
| fi | ||
| done | ||
| done | ||
|
|
||
| print_next_matrix "next_matrix" | ||
| } | ||
|
|
||
| check_neighborhood() { | ||
| local cell_col=$1 cell_row=$2 | ||
| local -i neighb_row neighb_col | ||
| local -i count=0 | ||
|
|
||
| for check_row in -1 0 1; do | ||
| neighb_row=$(( check_row + cell_row )) | ||
| (( neighb_row < 0 || neighb_row >= height )) && continue | ||
| for check_col in -1 0 1; do | ||
| neighb_col=$(( check_col + cell_col )) | ||
| (( neighb_col < 0 || neighb_col >= width )) && continue | ||
| (( check_row == 0 && check_col == 0 )) && continue | ||
|
|
||
| (( count+=${matrix[${neighb_col},${neighb_row}]} )) | ||
| done | ||
| done | ||
|
|
||
| echo "$count" | ||
| } | ||
|
|
||
| print_next_matrix() { | ||
| local -n array=$1 | ||
| local -i row col | ||
| local output | ||
|
|
||
| for (( row = 0; row < height; row++ )); do | ||
| output="" | ||
| for (( col = 0; col < width; col++ )); do | ||
| output+="${array[${col},${row}]}" | ||
| done | ||
| echo "$output" | ||
| done | ||
| } | ||
|
|
||
| create_matrix "$@" | ||
| tick | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # This is an auto-generated file. | ||
| # | ||
| # Regenerating this file via `configlet sync` will: | ||
| # - Recreate every `description` key/value pair | ||
| # - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
| # - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
| # - Preserve any other key/value pair | ||
| # | ||
| # As user-added comments (using the # character) will be removed when this file | ||
| # is regenerated, comments can be added via a `comment` key. | ||
|
|
||
| [ae86ea7d-bd07-4357-90b3-ac7d256bd5c5] | ||
| description = "empty matrix" | ||
|
|
||
| [4ea5ccb7-7b73-4281-954a-bed1b0f139a5] | ||
| description = "live cells with zero live neighbors die" | ||
|
|
||
| [df245adc-14ff-4f9c-b2ae-f465ef5321b2] | ||
| description = "live cells with only one live neighbor die" | ||
|
|
||
| [2a713b56-283c-48c8-adae-1d21306c80ae] | ||
| description = "live cells with two live neighbors stay alive" | ||
|
|
||
| [86d5c5a5-ab7b-41a1-8907-c9b3fc5e9dae] | ||
| description = "live cells with three live neighbors stay alive" | ||
|
|
||
| [015f60ac-39d8-4c6c-8328-57f334fc9f89] | ||
| description = "dead cells with three live neighbors become alive" | ||
|
|
||
| [2ee69c00-9d41-4b8b-89da-5832e735ccf1] | ||
| description = "live cells with four or more neighbors die" | ||
|
|
||
| [a79b42be-ed6c-4e27-9206-43da08697ef6] | ||
| description = "bigger matrix" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.