boB Rudis
3 years ago
6 changed files with 1689 additions and 145 deletions
@ -1,149 +1,512 @@ |
|||
library(rvest) |
|||
library(tidyverse) |
|||
library(httr) |
|||
httr::GET("https://adventofcode.com/2020/day/1") |
|||
httr::GET("https://adventofcode.com/2020/day/1") %>% |
|||
httr::content() |
|||
httr::GET("https://adventofcode.com/2020/day/1") %>% |
|||
httr::content() %>% |
|||
as.character() |
|||
scan(here::here("../input/01-01.txt"), what = "integer") |
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = "integer" |
|||
) |
|||
?scan |
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = integer() |
|||
) |
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = integer() |
|||
) -> input |
|||
combn(input) |
|||
combn(input, 2) |
|||
x <- combn(input, 2) |
|||
str(x, 1) |
|||
?combn |
|||
which(combn(input, 2, sum) == 2020) |
|||
sums <- combn(input, 2, sum) |
|||
sums |
|||
pairs <- combn(input, 2) |
|||
dim(pairs) |
|||
pairs[,which(sums == 2020)] |
|||
`*`(pairs[,which(sums == 2020)]) |
|||
magrittr::multiply_by(pairs[,which(sums == 2020)]) |
|||
reduce(pairs[,which(sums == 2020)], `*`) |
|||
Reduct(pairs[,which(sums == 2020)], `*`) |
|||
Reduce(pairs[,which(sums == 2020)], `*`) |
|||
Reduce(`*`, pairs[,which(sums == 2020)]) |
|||
tri <- combn(input, 3) |
|||
str(tri) |
|||
sums <- combn(input, 3, sum) |
|||
Reduce(`*`, tri[,which(sums == 2020)]) |
|||
combn |
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = integer() |
|||
) -> input |
|||
try(combn(input, 2, function(x) { |
|||
stop() |
|||
})) |
|||
try(combn(input, 2, function(x) { |
|||
if (sum(x) == 2020) { |
|||
res <<- Reduce(`*`, x) |
|||
stop() |
|||
} |
|||
})) |
|||
try(combn(head(input, 6), 2, function(x) { |
|||
message(str(x)) |
|||
x |
|||
})) |
|||
try(combn(head(input, 6), 2, function(x) { |
|||
message(sum(x)) |
|||
x |
|||
})) |
|||
res <- NULL |
|||
try(combn(head(input, 6), 2, function(x) { |
|||
if (sum(x) == 2020) { |
|||
res <<- Reduce(`*`, x) |
|||
} |
|||
x |
|||
})) |
|||
res |
|||
try(combn(head(input, 6), 2, function(x) { |
|||
if (sum(x) == 2020) { |
|||
message("HERE") |
|||
res <<- Reduce(`*`, x) |
|||
message(res) |
|||
} |
|||
x |
|||
})) |
|||
try(combn(head(input, 8), 2, function(x) { |
|||
if (sum(x) == 2020) { |
|||
message("HERE") |
|||
res <<- Reduce(`*`, x) |
|||
message(res) |
|||
} |
|||
x |
|||
})) |
|||
pairs <- combn(input, 2) |
|||
sums <- combn(input, 2, sum) |
|||
which(sums == 2020) |
|||
try(combn(input, 2, function(x) { |
|||
if (sum(x) == 2020) { |
|||
message("HERE") |
|||
res <<- Reduce(`*`, x) |
|||
message(res) |
|||
} |
|||
x |
|||
})) |
|||
try(tmp <- combn(input, 2, function(x) { |
|||
if (sum(x) == 2020) { |
|||
message("HERE") |
|||
res <<- Reduce(`*`, x) |
|||
message(res) |
|||
} |
|||
x |
|||
})) |
|||
res |
|||
try(combn(input, 2, function(x) { |
|||
if (sum(x) == 2020) { |
|||
res <<- Reduce(`*`, x) |
|||
stop() |
|||
} |
|||
x |
|||
})) |
|||
try(combn(input, 2, function(x) { |
|||
if (sum(x) == 2020) { |
|||
res <<- Reduce(`*`, x) |
|||
stop() |
|||
} |
|||
x |
|||
}), silent = TRUE) |
|||
res |
|||
} |
|||
microbenchmark::microbenchmark( |
|||
try = try(combn(input, 2, function(x) { |
|||
if (sum(x) == 2020) { |
|||
res <<- Reduce(`*`, x) |
|||
stop() |
|||
} |
|||
x |
|||
}), silent = TRUE), |
|||
plain = { |
|||
pairs <- combn(input, 2) |
|||
sums <- combn(input, 2, sum) |
|||
Reduce(`*`, pairs[,which(sums == 2020)]) |
|||
}) |
|||
reticulate::repl_python() |
|||
with open('../input/01-01.txt') as f: |
|||
), |
|||
ct = nchar(password), |
|||
good = ((ct >= min) & (ct <= max)) |
|||
) %>% |
|||
count(good) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
password = stri_replace_all_regex( |
|||
password, |
|||
sprintf("[^%s]", letter), |
|||
"" |
|||
) |
|||
input <- read_lines("/tmp/test.txt") |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
password = stri_replace_all_regex( |
|||
password, |
|||
sprintf("[^%s]", letter), |
|||
"" |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
password = stri_replace_all_regex( |
|||
password, |
|||
sprintf("[^%s]", letter), |
|||
"" |
|||
), |
|||
ct = nchar(password) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
password = stri_replace_all_regex( |
|||
password, |
|||
sprintf("[^%s]", letter), |
|||
"" |
|||
), |
|||
ct = nchar(password), |
|||
good = ((ct >= min) & (ct <= max)) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
password = stri_replace_all_regex( |
|||
password, |
|||
sprintf("[^%s]", letter), |
|||
"" |
|||
), |
|||
ct = nchar(password), |
|||
good = ((ct >= min) & (ct <= max)) |
|||
) %>% |
|||
count(good) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
as_tibble() |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
min = as.integer(min), |
|||
max = as.integer(max), |
|||
password = stri_replace_all_regex( |
|||
password, |
|||
sprintf("[^%s]", letter), |
|||
"" |
|||
), |
|||
ct = nchar(password), |
|||
good = ((ct >= min) & (ct <= max)) |
|||
) %>% |
|||
count(good) |
|||
input <- read_lines("../input/02-01.txt") |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
min = as.integer(min), |
|||
max = as.integer(max), |
|||
password = stri_replace_all_regex( |
|||
password, |
|||
sprintf("[^%s]", letter), |
|||
"" |
|||
), |
|||
ct = nchar(password), |
|||
good = ((ct >= min) & (ct <= max)) |
|||
) %>% |
|||
count(good) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(min), |
|||
pos2 = as.integer(max) |
|||
) |
|||
input <- read_lines("/tmp/test.txt") |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(min), |
|||
pos2 = as.integer(max), |
|||
) |
|||
?substr |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos1", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
good = substr(password, pos1, pos1) != substr(password, pos2, pos2) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
good = substr(password, pos1, pos1) != substr(password, pos2, pos2) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
good = (substr(password, pos1, pos1) == letter) & (substr(password, pos2, pos2) == letter) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
password = map(password, strsplit) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
password = map(password, strsplit, "") |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
password = map(password, strsplit, "") |
|||
) %>% |
|||
as_tibble() |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
password = map(password, strsplit, "") %>% map2(pos1, pos2, ~.[c(.x, .y)]) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
password = map(password, strsplit, "") %>% map2(pos1, pos2, ~.[c(.x, .y)]) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
password = map(password, strsplit, "") |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
password = map(password, strsplit, "")[,c(pos1, pos2)] |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
password = map(password, strsplit, "") |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
chr1 = substr(password, pos1, pos1), |
|||
chr2 = substr(password, pos1, pos2) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
chr1 = substr(password, pos1, pos1), |
|||
chr2 = substr(password, pos2, pos2) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
chr1 = substr(password, pos1, pos1), |
|||
chr2 = substr(password, pos2, pos2), |
|||
has_chr = letter %in% c(chr1, chr2) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
chr1 = substr(password, pos1, pos1), |
|||
chr2 = substr(password, pos2, pos2) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
chr1 = substr(password, pos1, pos1), |
|||
chr2 = substr(password, pos2, pos2), |
|||
has_chr = (chr1 == letter) | (chr2 == letter) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
chr1 = substr(password, pos1, pos1), |
|||
chr2 = substr(password, pos2, pos2), |
|||
good = ((chr1 == letter) | (chr2 == letter)) & (!(chr1 == chr2)) |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
chr1 = substr(password, pos1, pos1), |
|||
chr2 = substr(password, pos2, pos2), |
|||
good = ((chr1 == letter) | (chr2 == letter)) & (!(chr1 == chr2)) |
|||
) %>% |
|||
count(good) |
|||
input <- read_lines("../input/02-01.txt") |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
chr1 = substr(password, pos1, pos1), |
|||
chr2 = substr(password, pos2, pos2), |
|||
good = ((chr1 == letter) | (chr2 == letter)) & (!(chr1 == chr2)) |
|||
) %>% |
|||
count(good) |
|||
reticulate::repl_python() |
|||
with open("../input/01-01.txt"") as f: |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$" |
|||
) %>% |
|||
head() |
|||
input %>% |
|||
stri_extract_first_regex( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$" |
|||
) %>% |
|||
head() |
|||
input %>% |
|||
stri_extract_all_regex( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$" |
|||
) %>% |
|||
head() |
|||
input %>% |
|||
stri_match_all_regex( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$" |
|||
) %>% |
|||
head() |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$" |
|||
) %>% |
|||
head() |
|||
install.packages("stringb") |
|||
library(stringb) |
|||
?stringb |
|||
input %>% |
|||
stringb::text_extract_group_all( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$", |
|||
) %>% |
|||
head() |
|||
input %>% |
|||
stringb::text_extract_group_all( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$" |
|||
) %>% |
|||
head() |
|||
?stringb::text_extract_group_all |
|||
input %>% |
|||
?stringb::text_extract_group_all( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$", |
|||
perl = TRUE |
|||
) %>% |
|||
head() |
|||
input %>% |
|||
stringb::text_extract_group_all( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$", |
|||
perl = TRUE |
|||
) %>% |
|||
head() |
|||
input %>% |
|||
stringb::text_extract_group_all( |
|||
"(?<pos1>[^-]+)", |
|||
perl = TRUE |
|||
) %>% |
|||
head() |
|||
regexpr( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$", |
|||
input, |
|||
perl = TRUE |
|||
) %>% |
|||
head() |
|||
regexpr( |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$", |
|||
input, |
|||
perl = TRUE |
|||
) |
|||
?stringb::as.data.frame |
|||
text_extract_group_all( |
|||
input, |
|||
"(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$", |
|||
perl = TRUE |
|||
) |
|||
text_extract_group_all |
|||
stringb:::text_extract_group_all.default |
|||
reticulate::repl_python() |
|||
w, h = [int(x) for x in next(f).split()] |
|||
input <- read_lines("../input/02-01.txt") |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
chr1 = substr(password, pos1, pos1), |
|||
chr2 = substr(password, pos2, pos2), |
|||
good = ((chr1 == letter) | (chr2 == letter)) & (!(chr1 == chr2)) |
|||
) %>% |
|||
count(good) |
|||
reticulate::repl_python() |
|||
array = [[int(x) for x in line.split()] for line in f] |
|||
readRenviron("~/.Renviron") |
|||
input <- read_lines("../input/02-01.txt") |
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
min = as.integer(min), |
|||
max = as.integer(max), |
|||
password = stri_replace_all_regex(password, sprintf("[^%s]", letter), ""), |
|||
ct = nchar(password), |
|||
good = ((ct >= min) & (ct <= max)) |
|||
) %>% |
|||
count(good) |
|||
reticulate::repl_python() |
|||
|
@ -1,5 +1,10 @@ |
|||
/Users/hrbrmstr/Development/2020-code-advent/R/01.R="AE02BC3A" |
|||
/Users/hrbrmstr/Development/2020-code-advent/R/01.py="1AD9A8A4" |
|||
/Users/hrbrmstr/Development/2020-code-advent/R/02.R="AF113B0D" |
|||
/Users/hrbrmstr/Development/2020-code-advent/R/02.py="0900576A" |
|||
/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/js/01.js="77D3BDB6" |
|||
/Users/hrbrmstr/Development/2020-code-advent/js/02.js="BC8B36A1" |
|||
/private/tmp/test.txt="DFC19AA4" |
|||
|
@ -0,0 +1,78 @@ |
|||
# --- Day 2: Password Philosophy --- |
|||
# |
|||
# Your flight departs in a few days from the coastal airport; the easiest way down to the coast from here is via toboggan. |
|||
# |
|||
# The shopkeeper at the North Pole Toboggan Rental Shop is having a bad day. "Something's wrong with our computers; we can't log in!" You ask if you can take a look. |
|||
# |
|||
# Their password database seems to be a little corrupted: some of the passwords wouldn't have been allowed by the Official Toboggan Corporate Policy that was in effect when they were chosen. |
|||
# |
|||
# To try to debug the problem, they have created a list (your puzzle input) of passwords (according to the corrupted database) and the corporate policy when that password was set. |
|||
# |
|||
# For example, suppose you have the following list: |
|||
# |
|||
# 1-3 a: abcde |
|||
# 1-3 b: cdefg |
|||
# 2-9 c: ccccccccc |
|||
# |
|||
# Each line gives the password policy and then the password. The password policy indicates the lowest and highest number of times a given letter must appear for the password to be valid. For example, 1-3 a means that the password must contain a at least 1 time and at most 3 times. |
|||
# |
|||
# In the above example, 2 passwords are valid. The middle password, cdefg, is not; it contains no instances of b, but needs at least 1. The first and third passwords are valid: they contain one a or nine c, both within the limits of their respective policies. |
|||
# |
|||
# How many passwords are valid according to their policies? |
|||
library(stringi) |
|||
library(tidyverse) |
|||
|
|||
# 02-01 ------------------------------------------------------------------- |
|||
|
|||
input <- read_lines("../input/02-01.txt") |
|||
|
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("min", "max", "letter", "password")) %>% |
|||
mutate( |
|||
min = as.integer(min), |
|||
max = as.integer(max), |
|||
password = stri_replace_all_regex(password, sprintf("[^%s]", letter), ""), |
|||
ct = nchar(password), |
|||
good = ((ct >= min) & (ct <= max)) |
|||
) %>% |
|||
count(good) |
|||
|
|||
# 02-02 ------------------------------------------------------------------- |
|||
|
|||
# --- Part Two --- |
|||
# |
|||
# While it appears you validated the passwords correctly, they don't seem to be what the Official Toboggan Corporate Authentication System # is expecting. |
|||
# |
|||
# The shopkeeper suddenly realizes that he just accidentally explained the password policy rules from his old job at the sled rental place # down the street! The Official Toboggan Corporate Policy actually works a little differently. |
|||
# |
|||
# Each policy actually describes two positions in the password, where 1 means the first character, 2 means the second character, and so # on. (Be careful; Toboggan Corporate Policies have no concept of "index zero"!) Exactly one of these positions must contain the given # letter. Other occurrences of the letter are irrelevant for the purposes of policy enforcement. |
|||
# |
|||
# Given the same example list from above: |
|||
# |
|||
# 1-3 a: abcde is valid: position 1 contains a and position 3 does not. |
|||
# 1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b. |
|||
# 2-9 c: ccccccccc is invalid: both position 2 and position 9 contain c. |
|||
# How many passwords are valid according to the new interpretation of the policies? |
|||
|
|||
input <- read_lines("../input/02-01.txt") |
|||
|
|||
input %>% |
|||
stri_match_first_regex( |
|||
"([^-]+)-([^ ]+) ([^:]+): (.*)$" |
|||
) %>% |
|||
.[,2:5] %>% |
|||
as.data.frame() %>% |
|||
set_names(c("pos1", "pos2", "letter", "password")) %>% |
|||
mutate( |
|||
pos1 = as.integer(pos1), |
|||
pos2 = as.integer(pos2), |
|||
chr1 = substr(password, pos1, pos1), |
|||
chr2 = substr(password, pos2, pos2), |
|||
good = ((chr1 == letter) | (chr2 == letter)) & (!(chr1 == chr2)) |
|||
) %>% |
|||
count(good) |
@ -0,0 +1,49 @@ |
|||
# See R code for puzzle info |
|||
|
|||
import re |
|||
|
|||
with open("../input/02-01.txt") as f: |
|||
lines = f.readlines() |
|||
|
|||
# 02-01 ------------------------------------------------------------------- |
|||
|
|||
def check1(line): |
|||
|
|||
match = re.search( |
|||
"(?P<min>[^-]+)-(?P<max>[^ ]+) (?P<letter>[^:]+): (?P<password>.*)$", |
|||
line |
|||
) |
|||
|
|||
min = int(match.group("min")) |
|||
max = int(match.group("max")) |
|||
letter = match.group("letter") |
|||
password = ''.join(chr for chr in match.group("password") if chr == letter) |
|||
ct = len(password) |
|||
|
|||
return( |
|||
(ct >= min) & (ct <= max) |
|||
) |
|||
|
|||
sum([ check1(line) for line in lines ]) |
|||
|
|||
# 02-02 ------------------------------------------------------------------- |
|||
|
|||
def check2(line): |
|||
|
|||
match = re.search( |
|||
"(?P<pos1>[^-]+)-(?P<pos2>[^ ]+) (?P<letter>[^:]+): (?P<password>.*)$", |
|||
line |
|||
) |
|||
|
|||
pos1 = int(match.group("pos1")) |
|||
pos2 = int(match.group("pos2")) |
|||
letter = match.group("letter") |
|||
password = match.group("password") |
|||
|
|||
return( |
|||
((password[pos1-1] == letter) | |
|||
(password[pos2-1] == letter)) & |
|||
(password[pos1-1] != password[pos2-1]) |
|||
) |
|||
|
|||
sum([ check2(line) for line in lines ]) |
File diff suppressed because it is too large
@ -0,0 +1,49 @@ |
|||
// See R code for the problem texts
|
|||
|
|||
// 02-01
|
|||
|
|||
var fs = require("fs") |
|||
|
|||
input = fs.readFileSync("../input/02-01.txt", "utf-8") |
|||
.split("\n") |
|||
|
|||
|
|||
const re1 = /(?<min>[^-]+)-(?<max>[^ ]+) (?<letter>[^:]+): (?<password>.*)$/; |
|||
|
|||
res = input.map((line) => { |
|||
|
|||
g = re1.exec(line).groups |
|||
|
|||
g.min = parseInt(g.min) |
|||
g.max = parseInt(g.max) |
|||
|
|||
ltr_re = new RegExp("[^" + g.letter + "]", "g") |
|||
g.password = g.password.replace(ltr_re, "") |
|||
|
|||
return( |
|||
(g.password.length >= g.min) & |
|||
(g.password.length <= g.max) |
|||
) |
|||
|
|||
}).reduce((a, b) => a + b) |
|||
|
|||
console.log(res) |
|||
|
|||
// 02-02
|
|||
|
|||
const re2 = /(?<pos1>[^-]+)-(?<pos2>[^ ]+) (?<letter>[^:]+): (?<password>.*)$/; |
|||
|
|||
res = input.map((line) => { |
|||
|
|||
g = re2.exec(line).groups |
|||
|
|||
g.pos1 = parseInt(g.pos1) |
|||
g.pos2 = parseInt(g.pos2) |
|||
|
|||
return(((g.password[g.pos1-1] == g.letter) | |
|||
(g.password[g.pos2-1] == g.letter)) & |
|||
(g.password[g.pos1-1] != g.password[g.pos2-1])) |
|||
|
|||
}).reduce((a, b) => a + b) |
|||
|
|||
console.log(res) |
Loading…
Reference in new issue