IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Advent of Code with data.table: Week One

    Kelly Bodwin发表于 2024-12-07 00:00:00
    love 0
    [This article was first published on Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

    Happy December, R friends!

    One of my favorite traditions in the R community is the Advent of Code, a series of puzzles released at midnight EST from December 1st through 25th, to be solved through programming in the language of your choosing. I usually do a few of them each year, and once tried to do every single one at the moment it released!

    This year, I know I won’t be able to do it daily, but I’m going to do as many as I can using just data.table solutions.

    I’ll allow myself to use other packages when there isn’t any data.table equivalent, but my solutions must be as data.table-y as possible.

    I’m going to abuse the blog post structure and update this file throughout the week.

    library(data.table)

    December 1st

    Part One

    d1 <- fread("day1_dat1.txt")
    d1[, V1 := sort(V1)]
    d1[, V2 := sort(V2)]
    d1[, diff := abs(V1-V2)]
    
    sum(d1$diff)
    [1] 2815556

    Part Two

    d1[, similarity := sum(V1 == d1$V2)*V1, by = V1]
    
    sum(d1$similarity)
    [1] 23927637

    December 2nd

    Part One

    d1 <- fread("day2_dat1.txt", fill = TRUE)
    check_report <- function(vec) {
      
      vec <- na.omit(vec)
      
      has_neg <- vec < 0
      has_pos <- vec > 0
      
      inc_dec <- sum(has_neg) == length(vec) | sum(has_pos) == length(vec)
    
      too_big <- max(abs(vec)) > 3
      
      return(inc_dec & !too_big)
    }
    d1t <- transpose(d1)
    deltas <- d1t[-nrow(d1t)] - d1t[2:nrow(d1t)]
    
    res <- apply(deltas, 2, "check_report")
    
    sum(res)
    [1] 479

    Part Two

    test_reports <- function(dat) {
    
      deltas <- dat[-nrow(dat)] - dat[2:nrow(dat)]
    
      res <- apply(deltas, 2, "check_report")
    
      res
    }
    res <- test_reports(d1t)
    
    for (i in 1:nrow(d1t)) {
      
      res <- res | test_reports(d1t[-i,])
      
      
    }
    
    sum(res)
    [1] 531

    Just for fun

    I found the use of apply deeply unsatisfying, even though it was fast, so just for fun:

    d1t <- transpose(d1)
    deltas <- d1t[-nrow(d1t)] - d1t[2:nrow(d1t)]
    
    is_not_pos <- deltas <= 0
    is_not_neg <- deltas >= 0
    is_big <- abs(deltas) > 3
    
    res_inc <- colSums(is_not_neg | is_big, na.rm = TRUE)
    
    res_dec <- colSums(is_not_pos | is_big, na.rm = TRUE)
    
    sum(res_inc == 0) + sum(res_dec == 0)
    [1] 479

    Yay. 🙂

    Comparing data.table reshape to duckdb and polars

    Oct 17, 2024
    Toby Dylan Hocking

    Visualizing performance regression of data.table with atime

    Oct 10, 2024
    Doris Afriyie Amoakohene

    Seal of Approval: mlr3

    Oct 1, 2024
    Maximilian Mücke

    Seal of Approval: collapse

    Sep 21, 2024
    Sebastian Krantz

    Newly awarded translation projects

    Aug 20, 2024
    Toby Dylan Hocking

    Seal of Approval: dtplyr

    Aug 1, 2024
    Kelly Bodwin

    Seal of Approval: nc

    Aug 1, 2024
    Toby Dylan Hocking

    Seal of Approval: tidyfast

    Aug 1, 2024
    Tyson S. Barrett

    Announcement: The ‘Seal of Approval’

    Jul 31, 2024
    Kelly Bodwin

    Announcement: Paola Corrales, data.table Ambassador

    Jun 12, 2024
    Community Team

    Two Roads Diverged

    Jun 4, 2024
    Kelly Bodwin

    Testing infrastructure for data.table

    Mar 10, 2024
    Toby Hocking

    Community interviews about data.table

    Mar 6, 2024
    Anirban Chetia

    Results of the 2023 survey

    Feb 25, 2024
    Aljaž Sluga

    Column assignment and reference semantics in data.table

    Feb 18, 2024
    Toby Hocking

    The Benefits of data.table Syntax

    Feb 5, 2024
    Tyson Barrett

    New governance, release with new features

    Jan 30, 2024
    Toby Dylan Hocking

    Piping data.tables

    Jan 28, 2024
    Elio Campitelli

    Announcement: Jan Gorecki, data.table Ambassador

    Jan 14, 2024
    Kelly Bodwin

    Summary of LatinR conference

    Nov 19, 2023
    Toby Dylan Hocking

    Announcement: The data.table Ambassadors Travel Grant

    Nov 1, 2023
    Kelly Bodwin

    Announcement: data.table translation projects

    Oct 17, 2023

    Welcome to the data.table ecosystem project!

    An NSF-POSE funded venture.
    Oct 15, 2023
    Toby Hocking
    No matching items
    To leave a comment for the author, please follow the link and comment on their blog: Blog.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
    Continue reading: Advent of Code with data.table: Week One


沪ICP备19023445号-2号
友情链接