In [3]:
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
In [4]:
rules <- read_tsv("input", col_names="input") |> filter(str_detect(input, "\\|")) |> separate(input, into=c("x","y"), sep="\\|", convert=T)
Rows: 1399 Columns: 1
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (1): input
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
In [5]:
lists <- read_tsv("input", col_names="input") |> filter(str_detect(input, ",")) |> mutate(listid=row_number()) |> separate_longer_delim(input, ",") |> nest_by(listid)
Rows: 1399 Columns: 1
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (1): input
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
In [6]:
failing_rules <- crossing(rules, lists) |>
    rowwise() |>
    filter(x %in% data$input, y %in% data$input) |>
    mutate(posx=match(x,data$input),posy=match(y,data$input)) |>
    filter(posx>posy)
failing_rules
Out[6]:
A rowwise_df: 7215 × 6
xylistiddataposxposy
<int><int><int><list<tibble[,1]>><int><int>
1112 4812, 47, 48, 38, 24, 34, 84, 37, 26, 93, 94, 11, 63, 74, 89, 68, 45, 35, 7512 1
1112 5826, 74, 68, 47, 93, 12, 76, 11, 94, 95, 57, 37, 55, 89, 27, 63, 24, 48, 35 8 6
1112 9538, 48, 94, 27, 68, 67, 95, 89, 12, 52, 84, 24, 26, 11, 75, 76, 69, 35, 1414 9
111212922, 47, 98, 12, 65, 52, 89, 11, 78, 94, 75, 45, 96, 38, 69, 68, 35, 48, 76, 13, 14 8 4
111214848, 76, 75, 93, 44, 38, 89, 35, 94, 24, 45, 13, 14, 78, 47, 68, 69, 96, 65, 12, 112120
111216673, 81, 42, 65, 53, 58, 94, 75, 96, 52, 98, 69, 24, 12, 89, 55, 44, 13, 11, 91, 87, 78, 141914
111221273, 89, 13, 44, 14, 47, 76, 75, 65, 45, 12, 52, 24, 69, 78, 35, 11, 94, 81, 96, 981711
111312298, 47, 45, 78, 68, 13, 76, 22, 11 9 6
111314848, 76, 75, 93, 44, 38, 89, 35, 94, 24, 45, 13, 14, 78, 47, 68, 69, 96, 65, 12, 112112
111315244, 89, 13, 65, 11, 94, 76, 69, 48, 14, 35, 22, 73, 75, 55, 38, 45, 78, 96, 98, 52 5 3
111316673, 81, 42, 65, 53, 58, 94, 75, 96, 52, 98, 69, 24, 12, 89, 55, 44, 13, 11, 91, 87, 78, 141918
111317655, 81, 13, 47, 98, 11, 12, 96, 65, 89, 73, 22, 69, 35, 76, 75, 52 6 3
111321273, 89, 13, 44, 14, 47, 76, 75, 65, 45, 12, 52, 24, 69, 78, 35, 11, 94, 81, 96, 9817 3
111321524, 69, 87, 78, 55, 13, 58, 22, 11, 96, 12, 65, 52, 89, 38 9 6
111322187, 52, 55, 14, 13, 47, 94, 24, 69, 65, 11, 78, 91, 45, 38, 89, 81, 98, 73, 75, 22, 12, 4411 5
1114 4747, 68, 24, 52, 65, 14, 98, 69, 94, 44, 75, 38, 22, 96, 45, 11, 1316 6
111410435, 89, 45, 78, 94, 24, 38, 22, 14, 96, 98, 55, 44, 75, 65, 11, 52, 12, 68, 48, 6916 9
111414848, 76, 75, 93, 44, 38, 89, 35, 94, 24, 45, 13, 14, 78, 47, 68, 69, 96, 65, 12, 112113
111421273, 89, 13, 44, 14, 47, 76, 75, 65, 45, 12, 52, 24, 69, 78, 35, 11, 94, 81, 96, 9817 5
111422187, 52, 55, 14, 13, 47, 94, 24, 69, 65, 11, 78, 91, 45, 38, 89, 81, 98, 73, 75, 22, 12, 4411 4
1122 4747, 68, 24, 52, 65, 14, 98, 69, 94, 44, 75, 38, 22, 96, 45, 11, 131613
112210276, 38, 89, 22, 11, 24, 47, 84, 13, 69, 75, 45, 78, 44, 94, 52, 14, 93, 65, 55, 48 5 4
112210435, 89, 45, 78, 94, 24, 38, 22, 14, 96, 98, 55, 44, 75, 65, 11, 52, 12, 68, 48, 6916 8
112212298, 47, 45, 78, 68, 13, 76, 22, 11 9 8
112212922, 47, 98, 12, 65, 52, 89, 11, 78, 94, 75, 45, 96, 38, 69, 68, 35, 48, 76, 13, 14 8 1
112221524, 69, 87, 78, 55, 13, 58, 22, 11, 96, 12, 65, 52, 89, 38 9 8
1124 1863, 37, 68, 89, 24, 38, 11, 84, 27, 94, 92, 95, 93, 34, 64, 45, 35, 67, 57, 74, 26, 76, 47 7 5
1124 4747, 68, 24, 52, 65, 14, 98, 69, 94, 44, 75, 38, 22, 96, 45, 11, 1316 3
1124 4812, 47, 48, 38, 24, 34, 84, 37, 26, 93, 94, 11, 63, 74, 89, 68, 45, 35, 7512 5
1124 9538, 48, 94, 27, 68, 67, 95, 89, 12, 52, 84, 24, 26, 11, 75, 76, 69, 35, 141412
⋮⋮⋮⋮⋮⋮
9984 6162, 27, 84, 68, 74, 99, 93, 47, 76, 64, 26 6 3
9984 6845, 93, 26, 27, 48, 54, 95, 84, 67, 35, 19, 34, 97, 62, 37, 68, 47, 99, 76, 57, 6318 8
9984 7674, 64, 84, 42, 37, 57, 99, 62, 58, 92, 27, 67, 97, 95, 53, 68, 91, 63, 26, 54, 48, 93, 34 7 3
998418692, 67, 84, 93, 37, 53, 27, 57, 95, 64, 63, 91, 19, 81, 62, 99, 74, 97, 2616 3
9992 474, 91, 42, 62, 87, 27, 53, 98, 81, 92, 97, 99, 54, 63, 57, 67, 96, 26, 341210
9992 1954, 26, 53, 19, 27, 91, 58, 87, 92, 57, 37, 74, 99, 95, 93, 62, 84, 68, 9713 9
9992 6063, 92, 74, 64, 68, 62, 93, 57, 27, 97, 84, 42, 35, 48, 95, 26, 34, 19, 53, 99, 37, 67, 5820 2
999211598, 74, 34, 92, 62, 53, 42, 26, 19, 58, 64, 73, 37, 87, 97, 27, 81, 96, 63, 99, 57, 67, 5420 4
999213181, 67, 78, 96, 92, 58, 97, 62, 99, 98, 87, 64, 44, 34, 73, 63, 53 9 5
999213591, 14, 78, 62, 42, 74, 58, 52, 22, 81, 97, 92, 53, 44, 98, 99, 54, 64, 73, 19, 961612
999216167, 35, 92, 93, 76, 99, 54, 58, 48 6 3
999218692, 67, 84, 93, 37, 53, 27, 57, 95, 64, 63, 91, 19, 81, 62, 99, 74, 97, 2616 1
9993 6063, 92, 74, 64, 68, 62, 93, 57, 27, 97, 84, 42, 35, 48, 95, 26, 34, 19, 53, 99, 37, 67, 5820 7
9993 6774, 35, 93, 57, 99, 19, 54, 97, 37, 95, 58 5 3
9993 6845, 93, 26, 27, 48, 54, 95, 84, 67, 35, 19, 34, 97, 62, 37, 68, 47, 99, 76, 57, 6318 2
999316167, 35, 92, 93, 76, 99, 54, 58, 48 6 4
999318692, 67, 84, 93, 37, 53, 27, 57, 95, 64, 63, 91, 19, 81, 62, 99, 74, 97, 2616 4
9995 6063, 92, 74, 64, 68, 62, 93, 57, 27, 97, 84, 42, 35, 48, 95, 26, 34, 19, 53, 99, 37, 67, 582015
9995 6845, 93, 26, 27, 48, 54, 95, 84, 67, 35, 19, 34, 97, 62, 37, 68, 47, 99, 76, 57, 6318 7
999518692, 67, 84, 93, 37, 53, 27, 57, 95, 64, 63, 91, 19, 81, 62, 99, 74, 97, 2616 9
9997 474, 91, 42, 62, 87, 27, 53, 98, 81, 92, 97, 99, 54, 63, 57, 67, 96, 26, 341211
9997 3648, 68, 37, 58, 76, 34, 19, 57, 35, 63, 74, 84, 97, 53, 99, 27, 541513
9997 6063, 92, 74, 64, 68, 62, 93, 57, 27, 97, 84, 42, 35, 48, 95, 26, 34, 19, 53, 99, 37, 67, 582010
9997 6845, 93, 26, 27, 48, 54, 95, 84, 67, 35, 19, 34, 97, 62, 37, 68, 47, 99, 76, 57, 631813
9997 7997, 99, 62, 87, 13, 44, 42, 19, 22, 78, 52, 37, 96, 64, 92, 73, 98 2 1
999711598, 74, 34, 92, 62, 53, 42, 26, 19, 58, 64, 73, 37, 87, 97, 27, 81, 96, 63, 99, 57, 67, 542015
999713181, 67, 78, 96, 92, 58, 97, 62, 99, 98, 87, 64, 44, 34, 73, 63, 53 9 7
999713591, 14, 78, 62, 42, 74, 58, 52, 22, 81, 97, 92, 53, 44, 98, 99, 54, 64, 73, 19, 961611
999720953, 54, 52, 97, 22, 99, 65, 14, 69, 12, 19, 91, 58, 98, 42, 44, 87, 62, 96, 78, 13 6 4
999721497, 54, 87, 19, 91, 75, 69, 99, 52, 96, 12, 13, 73, 62, 65 8 1
In [7]:
lists |>-
    filter(!listid %in% failing_rules$listid) |>
    rowwise() |>
    mutate(middle = data$input[(length(data$input)+1)/2]) |>
    mutate(middle = as.numeric(middle)) |>
    ungroup() |>
    summarize(sum(middle))
Out[7]:
A tibble: 1 × 1
sum(middle)
<dbl>
6505