R/day22.R

Defines functions example_data_22 f22a

Documented in example_data_22 f22a

#' Day 22: Reactor Reboot
#'
#' [Reactor Reboot](https://adventofcode.com/2021/day/22)
#'
#' @name day22
#' @rdname day22
#' @details
#'
#' **Part One**
#'
#' Operating at these extreme ocean depths has overloaded the submarine\'s
#' reactor; it needs to be rebooted.
#'
#' The reactor core is made up of a large 3-dimensional grid made up
#' entirely of cubes, one cube per integer 3-dimensional coordinate
#' (`x,y,z`). Each cube can be either *on* or *off*; at the start of the
#' reboot process, they are all *off*. (Could it be an old model of a
#' reactor you\'ve seen [before](/2020/day/17)?)
#'
#' To reboot the reactor, you just need to set all of the cubes to either
#' *on* or *off* by following a list of *reboot steps* (your puzzle input).
#' Each step specifies a [cuboid](https://en.wikipedia.org/wiki/Cuboid)
#' (the set of all cubes that have coordinates which fall within ranges for
#' `x`, `y`, and `z`) and whether to turn all of the cubes in that cuboid
#' *on* or *off*.
#'
#' For example, given these reboot steps:
#'
#'     on x=10..12,y=10..12,z=10..12
#'     on x=11..13,y=11..13,z=11..13
#'     off x=9..11,y=9..11,z=9..11
#'     on x=10..10,y=10..10,z=10..10
#'
#' The first step (`on x=10..12,y=10..12,z=10..12`) turns *on* a 3x3x3
#' cuboid consisting of 27 cubes:
#'
#' -   `10,10,10`
#' -   `10,10,11`
#' -   `10,10,12`
#' -   `10,11,10`
#' -   `10,11,11`
#' -   `10,11,12`
#' -   `10,12,10`
#' -   `10,12,11`
#' -   `10,12,12`
#' -   `11,10,10`
#' -   `11,10,11`
#' -   `11,10,12`
#' -   `11,11,10`
#' -   `11,11,11`
#' -   `11,11,12`
#' -   `11,12,10`
#' -   `11,12,11`
#' -   `11,12,12`
#' -   `12,10,10`
#' -   `12,10,11`
#' -   `12,10,12`
#' -   `12,11,10`
#' -   `12,11,11`
#' -   `12,11,12`
#' -   `12,12,10`
#' -   `12,12,11`
#' -   `12,12,12`
#'
#' The second step (`on x=11..13,y=11..13,z=11..13`) turns *on* a 3x3x3
#' cuboid that overlaps with the first. As a result, only 19 additional
#' cubes turn on; the rest are already on from the previous step:
#'
#' -   `11,11,13`
#' -   `11,12,13`
#' -   `11,13,11`
#' -   `11,13,12`
#' -   `11,13,13`
#' -   `12,11,13`
#' -   `12,12,13`
#' -   `12,13,11`
#' -   `12,13,12`
#' -   `12,13,13`
#' -   `13,11,11`
#' -   `13,11,12`
#' -   `13,11,13`
#' -   `13,12,11`
#' -   `13,12,12`
#' -   `13,12,13`
#' -   `13,13,11`
#' -   `13,13,12`
#' -   `13,13,13`
#'
#' The third step (`off x=9..11,y=9..11,z=9..11`) turns *off* a 3x3x3
#' cuboid that overlaps partially with some cubes that are on, ultimately
#' turning off 8 cubes:
#'
#' -   `10,10,10`
#' -   `10,10,11`
#' -   `10,11,10`
#' -   `10,11,11`
#' -   `11,10,10`
#' -   `11,10,11`
#' -   `11,11,10`
#' -   `11,11,11`
#'
#' The final step (`on x=10..10,y=10..10,z=10..10`) turns *on* a single
#' cube, `10,10,10`. After this last step, `39` cubes are *on*.
#'
#' The initialization procedure only uses cubes that have `x`, `y`, and `z`
#' positions of at least `-50` and at most `50`. For now, ignore cubes
#' outside this region.
#'
#' Here is a larger example:
#'
#'     on x=-20..26,y=-36..17,z=-47..7
#'     on x=-20..33,y=-21..23,z=-26..28
#'     on x=-22..28,y=-29..23,z=-38..16
#'     on x=-46..7,y=-6..46,z=-50..-1
#'     on x=-49..1,y=-3..46,z=-24..28
#'     on x=2..47,y=-22..22,z=-23..27
#'     on x=-27..23,y=-28..26,z=-21..29
#'     on x=-39..5,y=-6..47,z=-3..44
#'     on x=-30..21,y=-8..43,z=-13..34
#'     on x=-22..26,y=-27..20,z=-29..19
#'     off x=-48..-32,y=26..41,z=-47..-37
#'     on x=-12..35,y=6..50,z=-50..-2
#'     off x=-48..-32,y=-32..-16,z=-15..-5
#'     on x=-18..26,y=-33..15,z=-7..46
#'     off x=-40..-22,y=-38..-28,z=23..41
#'     on x=-16..35,y=-41..10,z=-47..6
#'     off x=-32..-23,y=11..30,z=-14..3
#'     on x=-49..-5,y=-3..45,z=-29..18
#'     off x=18..30,y=-20..-8,z=-3..13
#'     on x=-41..9,y=-7..43,z=-33..15
#'     on x=-54112..-39298,y=-85059..-49293,z=-27449..7877
#'     on x=967..23432,y=45373..81175,z=27513..53682
#'
#' The last two steps are fully outside the initialization procedure area;
#' all other steps are fully within it. After executing these steps in the
#' initialization procedure region, `590784` cubes are *on*.
#'
#' Execute the reboot steps. Afterward, considering only cubes in the
#' region `x=-50..50,y=-50..50,z=-50..50`, *how many cubes are on?*
#'
#' **Part Two**
#'
#' *(Use have to manually add this yourself.)*
#'
#' *(Try using `convert_clipboard_html_to_roxygen_md()`)*
#'
#' @param x list where each element is a list of x, y and z coordinates
#' @param on logical vector of length(x), specifying whether cuboid should be
#' turned on or off
#' @return For Part One, `f22a(x)` returns .... For Part Two,
#'   `f22b(x)` returns ....
#' @export
#' @examples
#' ex <- example_data_22(1)
#' f22a(ex$x, ex$on)
f22a <- function(x, on) {
  n <- length(x)
  y <- do.call(expand.grid,x[[1]])
  for (i in 2:n){
    z <- do.call(expand.grid,x[[i]])
    if (on[i]) {
      y <- dplyr::full_join(y, z, by = c("x", "y", "z"))
    } else {
      y <- dplyr::anti_join(y, z, by = c("x", "y", "z"))
    }
  }
  nrow(y)
}

#' @param example Which example data to use (by position or name). Defaults to
#'   1.
#' @rdname day22
#' @export
example_data_22 <- function(example = 1) {
  l <- list(
    a = list(x = list(list(x=10:12,y=10:12,z=10:12),
                      list(x=11:13,y=11:13,z=11:13),
                      list(x=9:11,y=9:11,z=9:11),
                      list(x=10:10,y=10:10,z=10:10)),

             on = c(TRUE, TRUE, FALSE, TRUE)),
    b = list(x = list(list(x=-20:26,y=-36:17,z=-47:7),
                      list(x=-20:33,y=-21:23,z=-26:28),
                      list(x=-22:28,y=-29:23,z=-38:16),
                      list(x=-46:7,y=-6:46,z=-50:-1),
                      list(x=-49:1,y=-3:46,z=-24:28),
                      list(x=2:47,y=-22:22,z=-23:27),
                      list(x=-27:23,y=-28:26,z=-21:29),
                      list(x=-39:5,y=-6:47,z=-3:44),
                      list(x=-30:21,y=-8:43,z=-13:34),
                      list(x=-22:26,y=-27:20,z=-29:19),
                      list(x=-48:-32,y=26:41,z=-47:-37),
                      list(x=-12:35,y=6:50,z=-50:-2),
                      list(x=-48:-32,y=-32:-16,z=-15:-5),
                      list(x=-18:26,y=-33:15,z=-7:46),
                      list(x=-40:-22,y=-38:-28,z=23:41),
                      list(x=-16:35,y=-41:10,z=-47:6),
                      list(x=-32:-23,y=11:30,z=-14:3),
                      list(x=-49:-5,y=-3:45,z=-29:18),
                      list(x=18:30,y=-20:-8,z=-3:13),
                      list(x=-41:9,y=-7:43,z=-33:15)),
             on = c(rep(TRUE, 10), rep(c(FALSE, TRUE),5)))
  )
  l[[example]]
}
hturner/adventofcode21 documentation built on Jan. 14, 2022, 7:03 a.m.