R과 함께 코드 2020-04의 출현
                                            
                                                
                                                
                                                
                                                
                                                
                                                 16235 단어  adventofcoderstats
                    
[면책 조항] 분명히 이 게시물에는 Advent of Code에 대한 큰 스포일러가 포함되어 있습니다.
지침
1 단계
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
각 "여권"은 새 줄로 구분됩니다.
유효한 것으로 간주되려면 여권에
c("byr","iyr","eyr", "hgt", "hcl", "ecl", "pid") 가 모두 있어야 합니다.2 단계
유효한 것으로 간주되려면 여권에
c("byr","iyr","eyr", "hgt", "hcl", "ecl", "pid") 가 모두 있어야 하며, 각 항목은 일련의 규칙을 확인해야 합니다.전체 지침은 https://adventofcode.com/2020/day/4 에서 찾을 수 있습니다.
R 솔루션
1부
library(magrittr)
# Read the data
ipt <- readLines("2020-04-aoc.txt" ) %>% 
  # pasting everything into one big character string
  paste(collapse = "\n") %>% 
  # splitting where there are two \n\n (new lines)
  strsplit("\n\n") %>% 
  .[[1]] %>%
  # Removing the new lines
  gsub("\n", " ", .)
library(purrr, warn.conflicts = FALSE)
is_north_pole_valid <- function(
  x, 
  patt = c("byr","iyr","eyr", "hgt", "hcl", "ecl", "pid")
){
  map_lgl(
    patt, 
    ~ grepl(.x, x)
  ) %>% all()
}
map_dbl(
  ipt, ~{
    if (is_north_pole_valid(.x)) return(1)
    return(0)
  }
) %>% sum()
## [1] 204
두 번째 부분
ipt %>%
  # Remove the unvalid passport
  discard(~ !is_north_pole_valid(.x)) %>%
  # Split stuff
  strsplit(" ") %>%
  map_dbl(~{
    # Now, the heavy lifting
    # we have a string of key:value pairs, 
    # split it
    vals <- map_chr(.x, ~ gsub("([^:]*):(.*)", "\\2", .x)) 
    names(vals) <- map_chr(.x, ~ gsub("([^:]*):(.*)", "\\1", .x)) 
    # checking that values are inside range
    if (! dplyr::between(vals["byr"], 1920, 2002) ) return(0)
    if (! dplyr::between(vals["iyr"], 2010, 2020) ) return(0)
    if (! dplyr::between(vals["eyr"], 2020, 2030) ) return(0)
    # If height is in inch, check the range
    if ( grepl("in", vals["hgt"]) ) {
      if (! dplyr::between(gsub("in", "", vals["hgt"]), 59, 76)) return(0)
    } else if (grepl("cm", vals["hgt"])) {
      if (! dplyr::between(gsub("cm", "", vals["hgt"]), 150, 193)) return(0)
    } else {
      # No unit provided
      return(0)
    }
    if (! grepl("^#[a-f0-9]{6}$", vals["hcl"])) return(0)
    if (! vals["ecl"] %in% c("amb","blu", "brn", "gry", "grn","hzl", "oth")) return(0)
    if (! grepl("^[0-9]{9}$", vals["pid"])) return(0)
    return(1)
  }
) %>% sum()
## [1] 179
Reference
이 문제에 관하여(R과 함께 코드 2020-04의 출현), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/colinfay/advent-of-code-2020-04-with-r-3jl5텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)