Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Sep 17, 2024
1 parent fd97677 commit 8ad8655
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 85 deletions.
14 changes: 10 additions & 4 deletions exercises/practice/game-of-life/.meta/example.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
tick <- function(cells) {
coords <- expand.grid(seq_len(nrow(cells)), seq_len(ncol(cells)))

matrix(apply(coords, 1, function(coord) {
cell <- cells[coord[1], coord[2]]
neighbor_coords <- intersect(expand.grid(coord[1] + -1:1, coord[2] + -1:1), coords)
live_neighbors <- sum(apply(neighbor_coords, 1, \(neighbor_coord) cells[neighbor_coord[1], neighbor_coord[2]])) - cell
neighbor_coords <- dplyr::inner_join(
expand.grid(coord[1] + -1:1, coord[2] + -1:1),
coords
)
live_neighbors <- sum(
apply(neighbor_coords, 1, function (neighbor_coord) {
cells[neighbor_coord[1], neighbor_coord[2]]
})) - cell
ifelse(live_neighbors == 3 ||
live_neighbors == 2 && cell == 1, 1, 0)
live_neighbors == 2 && cell == 1, 1, 0)
}), nrow(cells))
}
148 changes: 67 additions & 81 deletions exercises/practice/game-of-life/test_game-of-life.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,123 +2,109 @@ source("./game-of-life.R")
library(testthat)

test_that("live cells with zero live neighbors die", {
expect_true(
all.equal(
tick(
matrix(
c(0, 0, 0,
0, 1, 0,
0, 0, 0), 3, byrow = TRUE)),
expect_equal(
tick(
matrix(
c(0, 0, 0,
0, 0, 0,
0, 0, 0), 3, byrow = TRUE)
)
0, 1, 0,
0, 0, 0), 3, byrow = TRUE)),
matrix(
c(0, 0, 0,
0, 0, 0,
0, 0, 0), 3, byrow = TRUE)
)
})

test_that("live cells with only one live neighbor die", {
expect_true(
all.equal(
tick(
matrix(
c(0, 0, 0,
0, 1, 0,
0, 1, 0), 3, byrow = TRUE)),
expect_equal(
tick(
matrix(
c(0, 0, 0,
0, 0, 0,
0, 0, 0), 3, byrow = TRUE)
)
0, 1, 0,
0, 1, 0), 3, byrow = TRUE)),
matrix(
c(0, 0, 0,
0, 0, 0,
0, 0, 0), 3, byrow = TRUE)
)
})

test_that("live cells with two live neighbors stay alive", {
expect_true(
all.equal(
tick(
matrix(
c(1, 0, 1,
1, 0, 1,
1, 0, 1), 3, byrow = TRUE)),
expect_equal(
tick(
matrix(
c(0, 0, 0,
c(1, 0, 1,
1, 0, 1,
0, 0, 0), 3, byrow = TRUE)
1, 0, 1), 3, byrow = TRUE)),
matrix(
c(0, 0, 0,
1, 0, 1,
0, 0, 0), 3, byrow = TRUE)
)
)
})

test_that("live cells with three live neighbors stay alive", {
expect_true(
all.equal(
tick(
matrix(
c(0, 1, 0,
1, 0, 0,
1, 1, 0), 3, byrow = TRUE)),
expect_equal(
tick(
matrix(
c(0, 0, 0,
c(0, 1, 0,
1, 0, 0,
1, 1, 0), 3, byrow = TRUE)
)
1, 1, 0), 3, byrow = TRUE)),
matrix(
c(0, 0, 0,
1, 0, 0,
1, 1, 0), 3, byrow = TRUE)
)
})

test_that("dead cells with three live neighbors become alive", {
expect_true(
all.equal(
tick(
matrix(
c(1, 1, 0,
0, 0, 0,
1, 0, 0), 3, byrow = TRUE)),
expect_equal(
tick(
matrix(
c(0, 0, 0,
1, 1, 0,
0, 0, 0), 3, byrow = TRUE)
)
c(1, 1, 0,
0, 0, 0,
1, 0, 0), 3, byrow = TRUE)),
matrix(
c(0, 0, 0,
1, 1, 0,
0, 0, 0), 3, byrow = TRUE)
)
})

test_that("live cells with four or more neighbors die", {
expect_true(
all.equal(
tick(
matrix(
c(1, 1, 1,
1, 1, 1,
1, 1, 1), 3, byrow = TRUE)),
expect_equal(
tick(
matrix(
c(1, 0, 1,
0, 0, 0,
1, 0, 1), 3, byrow = TRUE)
)
c(1, 1, 1,
1, 1, 1,
1, 1, 1), 3, byrow = TRUE)),
matrix(
c(1, 0, 1,
0, 0, 0,
1, 0, 1), 3, byrow = TRUE)
)
})

test_that("bigger matrix", {
expect_true(
all.equal(
tick(
matrix(
c(1, 1, 0, 1, 1, 0, 0, 0,
1, 0, 1, 1, 0, 0, 0, 0,
1, 1, 1, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 1, 1, 0,
1, 0, 0, 0, 1, 1, 0, 0,
1, 1, 0, 0, 0, 1, 1, 1,
0, 0, 1, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 1, 1), 8)),
expect_equal(
tick(
matrix(
c(1, 1, 0, 1, 1, 0, 0, 0,
1, 0, 1, 1, 0, 0, 0, 0,
1, 1, 1, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 1, 1, 0,
1, 0, 1, 1, 1, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 1, 0, 0, 1,
1, 1, 0, 1, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1), 8)
)
1, 0, 0, 0, 1, 1, 0, 0,
1, 1, 0, 0, 0, 1, 1, 1,
0, 0, 1, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 1, 1), 8)),
matrix(
c(1, 1, 0, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0,
1, 0, 1, 1, 1, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 1, 0, 0, 1,
1, 1, 0, 1, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1), 8)
)
})

0 comments on commit 8ad8655

Please sign in to comment.