Skip to content

Commit

Permalink
advent of code example
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Dylan Hocking committed Dec 20, 2024
1 parent 781137c commit 01b2235
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
31 changes: 31 additions & 0 deletions R/capture_first_vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,36 @@ capture_first_vec <- structure(function # Capture first match in each character
chr.pos.vec)
nc::capture_first_vec(na.vec, range.pattern, nomatch.error=FALSE)

## another subject from https://adventofcode.com/2024/day/14
pvxy.subject <- c("p=0,4 v=3,-3","p=6,3 v=-1,-3")
nc::capture_first_vec(
pvxy.subject,
"p=",
px="[0-9]",
",",
py="[0-9]",
" v=",
vx="[-0-9]+",
",",
vy="[-0-9]+",
type.convert=TRUE)

## to do the same as above but with less repetition:
g <- function(prefix,suffix)nc::group(
name=paste0(prefix,suffix),
"[-0-9]+")
xy <- function(prefix)list(
prefix,
"=",
g(prefix,"x"),
",",
g(prefix,"y"))
nc::capture_first_vec(
pvxy.subject,
xy("p"),
" ",
xy("v"),
type.convert=TRUE)

})

31 changes: 31 additions & 0 deletions man/capture_first_vec.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,35 @@ na.vec <- c(
chr.pos.vec)
nc::capture_first_vec(na.vec, range.pattern, nomatch.error=FALSE)

## another subject from https://adventofcode.com/2024/day/14
pvxy.subject <- c("p=0,4 v=3,-3","p=6,3 v=-1,-3")
nc::capture_first_vec(
pvxy.subject,
"p=",
px="[0-9]",
",",
py="[0-9]",
" v=",
vx="[-0-9]+",
",",
vy="[-0-9]+",
type.convert=TRUE)

## to do the same as above but with less repetition:
g <- function(prefix,suffix)nc::group(
name=paste0(prefix,suffix),
"[-0-9]+")
xy <- function(prefix)list(
prefix,
"=",
g(prefix,"x"),
",",
g(prefix,"y"))
nc::capture_first_vec(
pvxy.subject,
xy("p"),
" ",
xy("v"),
type.convert=TRUE)

}

0 comments on commit 01b2235

Please sign in to comment.