Browse Source

Day 4 R

master
boB Rudis 3 years ago
parent
commit
eecd812248
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 96
      R/.Rhistory
  2. 3
      R/.Rproj.user/shared/notebooks/paths
  3. 159
      R/04.R
  4. 1146
      input/04-01.txt

96
R/.Rhistory

@ -1,51 +1,3 @@
m_rows <- length(orig_map)
slope_x <- 3
slope_y <- 1
n_tiles <- slope_x * m_rows %/% m_cols
orig_map %>%
map(~rep(., n_tiles)) -> actual_map
start <- 1
xpos <- 1
ypos <- 1
n_trees <- 0
repeat {
xpos <- xpos + slope_x
row_trees <- sum(actual_map[[ypos]][start:(xpos-1)] == "#", na.rm = TRUE)
print(row_trees)
n_trees = n_trees + row_trees
start <- xpos
ypos <- ypos + 1
if (ypos > m_rows) break
}
n_trees
readLines("/tmp/test.txt") %>%
# readLines("../input/03-01.txt") %>%
stri_split_boundaries(
opts_brkiter = stri_opts_brkiter("character"),
simplify = FALSE
) -> orig_map
m_cols <- length(orig_map[[1]])
m_rows <- length(orig_map)
slope_x <- 3
slope_y <- 1
n_tiles <- slope_x * m_rows %/% m_cols
orig_map %>%
map(~rep(., n_tiles)) -> actual_map
start <- 1
xpos <- 1
ypos <- 1
n_trees <- 0
repeat {
xpos <- xpos + slope_x
row_trees <- sum(actual_map[[ypos]][(start+1):xpos] == "#", na.rm = TRUE)
print(row_trees)
n_trees = n_trees + row_trees
start <- xpos
ypos <- ypos + 1
if (ypos > m_rows) break
}
n_trees
actual_map
actual_map %>%
actual_map %>% map_chr(paste0, collapse="")
actual_map %>% map_chr(paste0, collapse="") %>% cat(sep="\n")
@ -510,3 +462,51 @@ map2_dbl(x, y, tree_count, orig_map = orig_map) %>%
reduce(`*`)
reticulate::repl_python()
x <- c(1, 3, 5, 7, 1)
library(stringi)
library(tidyverse)
tree_count <- function(orig_map, slope_x, slope_y) {
m_cols <- length(orig_map[[1]])
m_rows <- length(orig_map)
n_tiles <- (slope_x+1) * m_rows %/% (m_cols)
orig_map %>%
map(~rep(., n_tiles)) -> actual_map
xpos <- 1
ypos <- 1
n_trees <- 0
repeat {
xpos <- xpos + slope_x
ypos <- ypos + slope_y
if (ypos > m_rows) break
if (actual_map[[ypos]][xpos] == "#") n_trees <- n_trees + 1
}
n_trees
}
tree_count(orig_map, 3, 1)
readLines("../input/03-01.txt") %>%
stri_split_boundaries(
opts_brkiter = stri_opts_brkiter("character"),
simplify = FALSE
) -> orig_map
readLines("../input/03-01.txt") %>%
stri_split_boundaries(
opts_brkiter = stri_opts_brkiter("character"),
simplify = FALSE
) -> orig_map
tree_count <- function(orig_map, slope_x, slope_y) {
m_cols <- length(orig_map[[1]])
m_rows <- length(orig_map)
n_tiles <- (slope_x+1) * m_rows %/% (m_cols)
orig_map %>%
map(~rep(., n_tiles)) -> actual_map
xpos <- 1
ypos <- 1
n_trees <- 0
repeat {
xpos <- xpos + slope_x
ypos <- ypos + slope_y
if (ypos > m_rows) break
if (actual_map[[ypos]][xpos] == "#") n_trees <- n_trees + 1
}
n_trees
}
tree_count(orig_map, 3, 1)

3
R/.Rproj.user/shared/notebooks/paths

@ -4,10 +4,13 @@
/Users/hrbrmstr/Development/2020-code-advent/R/02.py="0900576A"
/Users/hrbrmstr/Development/2020-code-advent/R/03.R="B9D5FDD0"
/Users/hrbrmstr/Development/2020-code-advent/R/03.py="C2F47E74"
/Users/hrbrmstr/Development/2020-code-advent/R/04.R="E5E11F42"
/Users/hrbrmstr/Development/2020-code-advent/README.md="74DC8DCF"
/Users/hrbrmstr/Development/2020-code-advent/input/01-01.txt="53BE9636"
/Users/hrbrmstr/Development/2020-code-advent/input/02-01.txt="C32036DD"
/Users/hrbrmstr/Development/2020-code-advent/input/03-01.txt="3353A6D2"
/Users/hrbrmstr/Development/2020-code-advent/input/04-01="23400F29"
/Users/hrbrmstr/Development/2020-code-advent/input/04-01.txt="C5B6BCAC"
/Users/hrbrmstr/Development/2020-code-advent/js/01.js="77D3BDB6"
/Users/hrbrmstr/Development/2020-code-advent/js/02.js="BC8B36A1"
/Users/hrbrmstr/Development/2020-code-advent/js/03.js="E24752E6"

159
R/04.R

@ -0,0 +1,159 @@
# --- Day 4: Passport Processing ---
#
# You arrive at the airport only to realize that you grabbed your North Pole Credentials instead of your passport. While these documents are extremely similar, North Pole Credentials aren't issued by a country and therefore aren't actually valid documentation for travel in most of the world.
#
# It seems like you're not the only one having problems, though; a very long line has formed for the automatic passport scanners, and the delay could upset your travel itinerary.
#
# Due to some questionable network security, you realize you might be able to solve both of these problems at the same time.
#
# The automatic passport scanners are slow because they're having trouble detecting which passports have all required fields. The expected fields are as follows:
#
# byr (Birth Year)
# iyr (Issue Year)
# eyr (Expiration Year)
# hgt (Height)
# hcl (Hair Color)
# ecl (Eye Color)
# pid (Passport ID)
# cid (Country ID)
#
# Passport data is validated in batch files (your puzzle input). Each passport
# is represented as a sequence of key:value pairs separated by spaces or newlines.
# Passports are separated by blank lines.
#
# Here is an example batch file containing four passports:
#
# ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
# byr:1937 iyr:2017 cid:147 hgt:183cm
#
# iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
# hcl:#cfa07d byr:1929
#
# hcl:#ae17e1 iyr:2013
# eyr:2024
# ecl:brn pid:760753108 byr:1931
# hgt:179cm
#
# hcl:#cfa07d eyr:2025 pid:166559648
# iyr:2011 ecl:brn hgt:59in
#
# The first passport is valid - all eight fields are present. The second
# passport is invalid - it is missing hgt (the Height field).
#
# The third passport is interesting; the only missing field is cid, so it
# looks like data from North Pole Credentials, not a passport at all! Surely,
# nobody would mind if you made the system temporarily ignore missing cid
# fields. Treat this "passport" as valid.
#
# The fourth passport is missing two fields, cid and byr. Missing cid is
# fine, but missing any other field is not, so this passport is invalid.
#
# According to the above rules, your improved system would report 2 valid
# passports.
#
# Count the number of valid passports - those that have all required fields.
# Treat cid as optional. In your batch file, how many passports are valid?
library(stringi)
library(tidyverse)
req_keys <- c("byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid")
input <- readLines("../input/04-01.txt")
# 04-01 -------------------------------------------------------------------
split(input, findInterval(1:length(input), which(input == ""))) %>%
map_lgl(~{
paste0(.x, collapse = " ") %>%
stri_match_all_regex("([[:alpha:]]{3}):([^ $]+)") %>%
pluck(1) %>%
.[,2] %>%
discard(`==`, 'cid') -> found
all(found %in% req_keys) & (length(found) == length(req_keys))
}) %>%
sum()
# 04-02 -------------------------------------------------------------------
# --- Part Two ---
#
# The line is moving more quickly now, but you overhear airport security
# talking about how passports with invalid data are getting through.
# Better add some data validation, quick!
#
# You can continue to ignore the cid field, but each other field has strict
# rules about what values are valid for automatic validation:
#
# byr (Birth Year) - four digits; at least 1920 and at most 2002.
# iyr (Issue Year) - four digits; at least 2010 and at most 2020.
# eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
# hgt (Height) - a number followed by either cm or in:
# If cm, the number must be at least 150 and at most 193.
# If in, the number must be at least 59 and at most 76.
# hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
# ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.
# pid (Passport ID) - a nine-digit number, including leading zeroes.
# cid (Country ID) - ignored, missing or not.
#
# Your job is to count the passports where all required fields are both present and valid according to the above rules. Here are some example values:
validate_height <- function(value) {
matches <- unlist(stri_match_first_regex(value, "^([[:digit:]]{2,3})(cm|in)$"))
return(
suppressWarnings(
switch(
matches[3],
"cm" = between(as.integer(matches[2]), 150, 193),
"in" = between(as.integer(matches[2]), 59, 76),
FALSE
)
)
)
}
validate <- function(field, value) {
return(
suppressWarnings(
switch(
field,
"byr" = stri_detect_regex(value, "^[[:digit:]]{4}$") & between(as.integer(value), 1920, 2002),
"iyr" = stri_detect_regex(value, "^[[:digit:]]{4}$") & between(as.integer(value), 2010, 2020),
"eyr" = stri_detect_regex(value, "^[[:digit:]]{4}$") & between(as.integer(value), 2020, 2030),
"hgt" = stri_detect_regex(value, "^[[:digit:]]{2,3}(cm|in)$") & validate_height(value),
"hcl" = stri_detect_regex(value, "^#[a-f[:digit:]]{6}$"),
"ecl" = value %in% c("amb", "blu", "brn", "gry", "grn", "hzl", "oth"),
"pid" = stri_detect_regex(value, "^[[:digit:]]{9}$"),
"cid" = TRUE,
TRUE
)
)
)
}
split(input, findInterval(1:length(input), which(input == ""))) %>%
map_lgl(~{
paste0(.x, collapse = " ") %>%
stri_match_all_regex("([[:alpha:]]{3}):([^ $]+)") %>%
pluck(1) %>%
.[,2:3] %>%
as.data.frame() %>%
set_names(c("field", "value")) %>%
filter(field != "cid") -> found
if (!(all(found$field %in% req_keys) & (length(found$field) == length(req_keys)))) return(FALSE)
all(map2_lgl(found$field, found$value, validate))
}) %>%
sum()

1146
input/04-01.txt

File diff suppressed because it is too large
Loading…
Cancel
Save