-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.R
46 lines (41 loc) · 1.02 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
read_as_grid <- function(input, col_sep = "", convert = TRUE) {
tibble(value = input) %>%
separate_rows(value, sep = "\n", convert = FALSE) %>%
mutate(y = 1:n()) %>%
separate_rows(value, sep = col_sep, convert = convert) %>%
filter(value != "") %>%
group_by(y) %>%
mutate(x = 1:n()) %>%
ungroup()
}
one_indexed_remainder <- function(value, mod) {
if_else(value %% mod == 0, mod, value %% mod)
}
directions <- tribble(
~direction, ~dx, ~dy,
"tl", -1, -1,
"t", 0, -1,
"tr", 1, -1,
"l", -1, 0,
"self", 0, 0,
"r", 1, 0,
"bl", -1, 1,
"b", 0, 1,
"br", 1, 1
) %>%
select(-direction)
binary_vec_to_decimal <- function(binary) {
sum(rev(binary) * 2 ^ (0:(length(binary) - 1)))
}
decimal_to_n_digit_binary <- function(num, n) {
result <- num %% 2
num <- (num - result) / 2
while (num > 0) {
result <- paste0(num %% 2, result)
num <- (num - num %% 2) / 2
}
str_pad(result, n, pad = "0")
}
sort_chars <- function(word) {
paste(sort(str_split(word, "")[[1]]), collapse = "")
}