diff --git a/exercises/practice/game-of-life/.meta/example.R b/exercises/practice/game-of-life/.meta/example.R index 63596724..6a29e9e5 100644 --- a/exercises/practice/game-of-life/.meta/example.R +++ b/exercises/practice/game-of-life/.meta/example.R @@ -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)) } diff --git a/exercises/practice/game-of-life/test_game-of-life.R b/exercises/practice/game-of-life/test_game-of-life.R index b2dae0cd..ec215a89 100644 --- a/exercises/practice/game-of-life/test_game-of-life.R +++ b/exercises/practice/game-of-life/test_game-of-life.R @@ -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) ) })