grid_tidy | R Documentation |
Many Advent of Code challenges involve parsing a grid where each row of the input represents one row of the grid. These turn such inputs into a tidy table, a matrix, or a tidygraph object.
grid_tidy(d, var, sep = "", parse = TRUE) grid_matrix(d, var, sep = "", parse = TRUE) grid_graph(d, ..., directed = FALSE, mutual = FALSE, circular = FALSE)
d |
A table |
var |
The name of a string column |
sep |
A string separator to use for splitting columns |
parse |
Whether to parse the input by guessing its format |
... |
Any of the above arguments |
directed |
Passed on to |
mutual |
Passed on to |
circular |
Passed on to |
# 2021 Day 3 Part 1 grid_day3 <- advent_input(3) %>% grid_tidy(x) grid_day3 %>% group_by(col) %>% summarize(gamma = round(mean(value)), epsilon = 1 - gamma) %>% mutate(power = 2 ^ (rev(col) - 1)) %>% summarize(gamma = sum(gamma * power), epsilon = sum(epsilon * power), answer = gamma * epsilon) # 2021 Day 4 Part 1 (includes a third dimension of "board") input4 <- advent_input(4) grid_day4 <- advent_input(4) %>% slice(-1) %>% mutate(board = cumsum(x == "")) %>% filter(x != "") %>% grid_tidy(x, sep = " +")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.