tests/testthat/test-day12.R

context("test-day12")

test_that("simple padding to match test output", {
  t0 <- "...#..#.#..##......###...###..........."

  "#..#.#..##......###...###" %>%
    str_set_plant_substr() %>%
    str_pad_plant() %>%
    str_unset_plant() %>%
    expect_equal(t0)
})

test_that("applying plant rules many times", {
  rules <- read_text_lines("
  ...## => #
  ..#.. => #
  .#... => #
  .#.#. => #
  .#.## => #
  .##.. => #
  .#### => #
  #.#.# => #
  #.### => #
  ##.#. => #
  ##.## => #
  ###.. => #
  ###.# => #
  ####. => #")

  pots <- "#..#.#..##......###...###" %>%
    str_set_plant_substr()
  lookup <- parse_plant_rules(rules)

  expected <- c(
    "...#...#....#.....#..#..#..#...........",
    "...##..##...##....#..#..#..##..........",
    "..#.#...#..#.#....#..#..#...#..........",
    "...#.#..#...#.#...#..#..##..##.........",
    "....#...##...#.#..#..#...#...#.........",
    "....##.#.#....#...#..##..##..##........",
    "...#..###.#...##..#...#...#...#........",
    "...#....##.#.#.#..##..##..##..##.......",
    "...##..#..#####....#...#...#...#.......",
    "..#.#..#...#.##....##..##..##..##......",
    "...#...##...#.#...#.#...#...#...#......",
    "...##.#.#....#.#...#.#..##..##..##.....",
    "..#..###.#....#.#...#....#...#...#.....",
    "..#....##.#....#.#..##...##..##..##....",
    "..##..#..#.#....#....#..#.#...#...#....",
    ".#.#..#...#.#...##...#...#.#..##..##...",
    "..#...##...#.#.#.#...##...#....#...#...",
    "..##.#.#....#####.#.#.#...##...##..##..",
    ".#..###.#..#.#.#######.#.#.#..#.#...#..",
    ".#....##....#####...#######....#.#..##.")

  for (i in 1:20) {
    pots <- pots %>%
      apply_plant_rules(lookup)

    pots %>%
      str_pad_plant() %>%
      str_unset_plant() %>%
      expect_equal(expected[i])
  }
})

test_that("solving main puzzle", {
  rules <- read_text_lines("
  ...## => #
  ..#.. => #
  .#... => #
  .#.#. => #
  .#.## => #
  .##.. => #
  .#### => #
  #.#.# => #
  #.### => #
  ##.#. => #
  ##.## => #
  ###.. => #
  ###.# => #
  ####. => #")

  "#..#.#..##......###...###" %>%
    run_plant_rules(rules, 20) %>%
    which_pots_have_plants() %>%
    sum() %>%
    expect_equal(325)
})
tjmahr/adventofcode18 documentation built on May 24, 2019, 4:10 p.m.