-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -16,3 +16,4 @@ export(aoc_url_input) | |
export(extract_numbers) | ||
export(gcd) | ||
export(lcm) | ||
export(lines_to_matrix) |
This file contains 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 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 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,14 @@ | ||
#' Convert vectors to a matrix | ||
#' | ||
#' For the default split of `"`, assumes that each element of `lines` has the same number of characters. | ||
#' | ||
#' @param lines a vector | ||
#' @param split character. The string to split the input on. Default is `""`, i.e. one character per column | ||
#' | ||
#' @return A matrix where the number of rows is the length of `lines` and, for a split on `"`, the number of columns is the same as the number of characters in each element of `lines`. | ||
#' @export | ||
#' | ||
#' @examples lines_to_matrix(c("#.#.", "..#.", "##..")) | ||
lines_to_matrix <- function(lines, split = "") { | ||
strsplit(lines, split) |> do.call(rbind, args = _) | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,8 @@ | ||
# write test for lines_to_matrix | ||
test_that("lines_to_matrix works", { | ||
expect_equal(lines_to_matrix(c("#.#.", "..#.", "##..")), | ||
matrix(c("#", ".", "#", ".", | ||
".", ".", "#", ".", | ||
"#", "#", ".", "." | ||
), byrow = TRUE, nrow = 3)) | ||
}) |