
Researcher @deziminstitut. PhD candidate in sociology @unibielefeld. #rstats #rspatial #dataviz
- Tweets146
- Following204
- Followers305
- Likes501
![``` r is_right_order <- function(l, r) { equals <- which_equal(l, r) if (length(equals)) { l <- l[-equals] r <- r[-equals] } if (!length(r)) { FALSE } else if (!length(l)) { TRUE } else if (is.atomic(l) && is.atomic(r)) { l < r } else if (is.atomic(l) && is.recursive(r)) { is_right_order(list(l), r) } else if (is.recursive(l) && is.atomic(r)) { is_right_order(l, list(r)) } else { is_right_order(l[[1]], r[[1]]) } } which_equal <- function(l, r) { idx <- seq_len(min(length(l), length(r))) which(purrr::map2_lgl(l[idx], r[idx], \(x, y) identical(unlist(x), unlist(y)))) } ```](https://pbs.twimg.com/media/Fj443OaXgAAzHdg.jpg?name=small&format=webp)
ALT ``` r is_right_order <- function(l, r) { equals <- which_equal(l, r) if (length(equals)) { l <- l[-equals] r <- r[-equals] } if (!length(r)) { FALSE } else if (!length(l)) { TRUE } else if (is.atomic(l) && is.atomic(r)) { l < r } else if (is.atomic(l) && is.recursive(r)) { is_right_order(list(l), r) } else if (is.recursive(l) && is.atomic(r)) { is_right_order(l, list(r)) } else { is_right_order(l[[1]], r[[1]]) } } which_equal <- function(l, r) { idx <- seq_len(min(length(l), length(r))) which(purrr::map2_lgl(l[idx], r[idx], \(x, y) identical(unlist(x), unlist(y)))) } ```

ALT ``` r input <- readLines(here::here("2022/13-input")) pairs <- input |> split(cumsum(!nzchar(input))) |> purrr::map(purrr::keep, nzchar) |> purrr::map_depth(2, jsonlite::fromJSON, simplifyVector = FALSE) |> unname() # Part 1 ------------------------------------------------------------------ pairs |> purrr::map_lgl(purrr::lift_dl(is_right_order)) |> which() |> sum() # Part 2 ------------------------------------------------------------------ n_less_than <- function(xs, y) sum(purrr::map_lgl(xs, is_right_order, y)) div1 <- list(list(2L)) div2 <- list(list(6L)) packets <- purrr::flatten(pairs) (n_less_than(packets, div1) + 1) * (n_less_than(packets, div2) + 2) ```
![``` r input <- readLines(file) |> strsplit("", fixed = TRUE) |> purrr::map(as.integer) |> do.call(what = "rbind") tree_locs <- expand.grid( i = seq_len(nrow(input)), j = seq_len(ncol(input)) ) is_visible <- function(mat, i, j) { lines_to_edges(mat, i, j) |> purrr::map_lgl(\(x) n_smaller(x, mat[i, j]) == length(x)) |> any() } scenic_score <- function(mat, i, j) { lines_to_edges(mat, i, j) |> purrr::map_int(viewing_distance, mat[i, j]) |> prod() } viewing_distance <- function(xs, y) min(n_smaller(xs, y) + 1L, length(xs)) n_smaller <- function(xs, y) length(purrr::head_while(xs, \(x) x < y)) lines_to_edges <- function(mat, i, j) { list( w = rev(input[i, 1:j]), e = input[i, j:dim(input)[2]], n = rev(input[1:i, j]), s = input[i:dim(input)[1], j] ) |> purrr::map(utils::tail, -1) } # P1 tree_locs |> purrr::pmap_lgl(is_visible, mat = input) |> sum() # P2 tree_locs |> purrr::pmap_dbl(scenic_score, mat = input) |> max() ```](https://pbs.twimg.com/media/FjeBeHTXwAAAGyr.jpg?name=small&format=webp)
ALT ``` r input <- readLines(file) |> strsplit("", fixed = TRUE) |> purrr::map(as.integer) |> do.call(what = "rbind") tree_locs <- expand.grid( i = seq_len(nrow(input)), j = seq_len(ncol(input)) ) is_visible <- function(mat, i, j) { lines_to_edges(mat, i, j) |> purrr::map_lgl(\(x) n_smaller(x, mat[i, j]) == length(x)) |> any() } scenic_score <- function(mat, i, j) { lines_to_edges(mat, i, j) |> purrr::map_int(viewing_distance, mat[i, j]) |> prod() } viewing_distance <- function(xs, y) min(n_smaller(xs, y) + 1L, length(xs)) n_smaller <- function(xs, y) length(purrr::head_while(xs, \(x) x < y)) lines_to_edges <- function(mat, i, j) { list( w = rev(input[i, 1:j]), e = input[i, j:dim(input)[2]], n = rev(input[1:i, j]), s = input[i:dim(input)[1], j] ) |> purrr::map(utils::tail, -1) } # P1 tree_locs |> purrr::pmap_lgl(is_visible, mat = input) |> sum() # P2 tree_locs |> purrr::pmap_dbl(scenic_score, mat = input) |> max() ```























