tests/testthat/test-day22.R

context("test-day22.R")

test_that("cursor can move on the grid", {
  start <- "..#
  #..
  ..."

  grid <- start %>% read_text_lines() %>% virus_grid()

  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 102, y = 102))

  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 102, y = 101))

  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 102, y = 100))

  grid$turn_left()
  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 101, y = 100))

  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 100, y = 100))

  grid$turn_left()
  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 100, y = 101))

  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 100, y = 102))

  grid$turn_left()
  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 101, y = 102))

  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 102, y = 102))

  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 103, y = 102))

  grid$turn_right()
  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 103, y = 103))

  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 103, y = 104))

  grid$turn_right()
  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 102, y = 104))

  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 101, y = 104))

  grid$turn_right()
  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 101, y = 103))

  grid$move()
  grid$get_cursor()[c("x", "y")] %>%
    expect_equal(list(x = 101, y = 102))
})

test_that("can recreate virus grid demo", {
  start <- "..#
  #..
  ..."

  m1 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   "#",   ".",   ".",   ".",
          ".",   ".",   ".",   "#",   "[.]", ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g1 <- matrix(m1, nrow = 8, ncol = 9, byrow = TRUE)

  m2 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   "#",   ".",   ".",   ".",
          ".",   ".",   ".",   "[#]", "#",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g2 <- matrix(m2, nrow = 8, ncol = 9, byrow = TRUE)

  m3 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   "[.]", ".",   "#",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   "#",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g3 <- matrix(m3, nrow = 8, ncol = 9, byrow = TRUE)

  m4 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   "#",   "[#]", ".",   "#",   ".",   ".",   ".",
          ".",   ".",   "#",   "#",   "#",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g4 <- matrix(m4, nrow = 8, ncol = 9, byrow = TRUE)

  m5 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   "#",   ".",   "[.]", "#",   ".",   ".",   ".",
          ".",   ".",   "#",   "#",   "#",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g5 <- matrix(m5, nrow = 8, ncol = 9, byrow = TRUE)

  m6 <- c(".",   ".",   ".",   ".",   ".",   "#",   "#",   ".",   ".",
          ".",   ".",   ".",   ".",   "#",   ".",   ".",   "#",   ".",
          ".",   ".",   ".",   "#",   ".",   ".",   ".",   ".",   "#",
          ".",   ".",   "#",   ".",   "#",   "[.]", ".",   ".",   "#",
          ".",   ".",   "#",   ".",   "#",   ".",   ".",   "#",   ".",
          ".",   ".",   ".",   ".",   ".",   "#",   "#",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g6 <- matrix(m6, nrow = 8, ncol = 9, byrow = TRUE)

  grid <- start %>% read_text_lines() %>% virus_grid()
  expect_equal(grid$view_grid(4, 4, 3, 4), g1)

  grid$step(1)
  expect_equal(grid$view_grid(4, 5, 3, 3), g2)

  grid$step(1)
  expect_equal(grid$view_grid(3, 5, 4, 3), g3)

  grid$step(4)
  expect_equal(grid$view_grid(3, 5, 4, 3), g4)

  grid$step(1)
  expect_equal(grid$view_grid(3, 4, 4, 4), g5)
  expect_equal(grid$get_infect_count(), 5)

  grid$step(63)
  expect_equal(grid$view_grid(3, 3, 4, 5), g6)
  expect_equal(grid$get_infect_count(), 41)

  grid$step(10000 - 70)
  expect_equal(grid$get_infect_count(), 5587)
})



test_that("can recreate evolved virus grid demo", {
  start <- "..#
  #..
  ..."

  m1 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   "#",   ".",   ".",   ".",
          ".",   ".",   ".",   "#",   "[.]", ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g1 <- matrix(m1, nrow = 8, ncol = 9, byrow = TRUE)

  m2 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   "#",   ".",   ".",   ".",
          ".",   ".",   ".",   "[#]", "W",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g2 <- matrix(m2, nrow = 8, ncol = 9, byrow = TRUE)

  m3 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   "[.]", ".",   "#",   ".",   ".",   ".",
          ".",   ".",   ".",   "F",   "W",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g3 <- matrix(m3, nrow = 8, ncol = 9, byrow = TRUE)

  m4 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   "W",   "W",   ".",   "#",   ".",   ".",   ".",
          ".",   ".",   "W",   "[F]", "W",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g4 <- matrix(m4, nrow = 8, ncol = 9, byrow = TRUE)

  m5 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   "W",   "W",   ".",   "#",   ".",   ".",   ".",
          ".",   ".",   "[W]",  ".",  "W",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g5 <- matrix(m5, nrow = 8, ncol = 9, byrow = TRUE)

  m6 <- c(".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   "W",   "W",   ".",   "#",   ".",   ".",   ".",
          ".",   "[.]", "#",   ".",   "W",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",
          ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".",   ".")
  g6 <- matrix(m6, nrow = 8, ncol = 9, byrow = TRUE)

  grid <- start %>% read_text_lines() %>% evolved_virus_grid()
  expect_equal(grid$view_grid(4, 4, 3, 4), g1)

  grid$step(1)
  expect_equal(grid$view_grid(4, 5, 3, 3), g2)

  grid$step(1)
  expect_equal(grid$view_grid(3, 5, 4, 3), g3)

  grid$step(3)
  expect_equal(grid$view_grid(4, 5, 3, 3), g4)

  grid$step(1)
  expect_equal(grid$view_grid(4, 6, 3, 2), g5)

  grid$step(1)
  expect_equal(grid$view_grid(4, 7, 3, 1), g6)

  grid$step(100 - 7)
  expect_equal(grid$get_infect_count(), 26)
})
tjmahr/adventofcode17 documentation built on May 30, 2019, 2:29 p.m.