Skip to content

Commit 69ddf1b

Browse files
committed
day03
1 parent 4989cae commit 69ddf1b

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

2024/day03.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
r <- paste0(readLines(read.input(3)),collapse="")
2+
s <- strsplit(gsub("mul\\(|\\)","",regmatches(r,gregexpr("mul\\([0-9]+,[0-9]+\\)",r))[[1]]),",")
3+
m <- apply(do.call(rbind,s),2,as.integer)
4+
sum(apply(m,1,prod))
5+
#173419328
6+
7+
inst <- regmatches(r,gregexpr("(mul\\([0-9]+,[0-9]+\\))|(do\\(\\))|(don\\'t\\(\\))",r))[[1]]
8+
9+
do <- TRUE
10+
a <- 0
11+
for(i in seq_along(inst)){
12+
if(inst[i]=="do()") do <- TRUE
13+
if(inst[i]=="don't()") do <- FALSE
14+
if(grepl("mul",inst[i])&do) a <- a + prod(as.integer(strsplit(gsub("(mul\\()|\\)","",inst[i]),",")[[1]]))
15+
}
16+
a
17+
#90669332

2024/parse.group.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
parse.group <- function(regex,input) { #old faithful (from regexpr help file)
2+
result <- regexpr(regex, input, perl=TRUE)
3+
m <- do.call(rbind, lapply(seq_along(input), function(i) {
4+
if(result[i] == -1) return("")
5+
st <- attr(result, "capture.start")[i, ]
6+
substring(input[i], st, st + attr(result, "capture.length")[i, ] - 1)
7+
}))
8+
colnames(m) <- attr(result, "capture.names")
9+
as.data.frame(m)
10+
}

2024/screenshot.png

5.26 KB
Loading

0 commit comments

Comments
 (0)