rInputValidation1 = await webR.evalRNumber(`
r"(${sample1.innerText})" |>
stri_split_lines1() -> grid
grid |>
imap_dfr(\\(.line, .idx) {
as.data.frame(
re_exec_all(.line, "([[:digit:]]+)") |>
pull(\`.match\`)
) |>
rename( # makes it easier to do the comparison below
nmatch = match,
nstart = start,
nend = end
) |>
mutate(
ny = .idx
)
}) -> number_matches
grid |>
imap_dfr(\\(.line, .idx) {
as.data.frame(
re_exec_all(.line, "([^\\\\.[:digit:]]+)") |> # having to double escape these is a annoying
pull(\`.match\`)
) |>
mutate(
y = .idx
)
}) -> symbol_matches
symbol_matches |>
rowwise() |>
mutate(
found_numbers = list(
number_matches |>
filter(
start >= nstart - 1 &
end <= nend + 1 &
y >= ny - 1 &
y <= ny + 1
)
)
) |>
unnest(found_numbers) |>
distinct(nmatch, nstart, nend, ny) |>
pull(nmatch) |>
as.integer() |>
sum()
`)