grid_tidy: Parse a table with a string column that represents rows in a...

View source: R/grid.R

grid_tidyR Documentation

Parse a table with a string column that represents rows in a grid

Description

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.

Usage

grid_tidy(d, var, sep = "", parse = TRUE)

grid_matrix(d, var, sep = "", parse = TRUE)

grid_graph(d, ..., directed = FALSE, mutual = FALSE, circular = FALSE)

Arguments

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 create_lattice

mutual

Passed on to create_lattice

circular

Passed on to create_lattice

Examples


# 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 = " +")


dgrtwo/adventdrob documentation built on Jan. 3, 2023, 7:27 a.m.