library(mistlecode)
ticker <-
fread("ticker.txt", sep = ":") |>
`colnames<-`(c("item", "qty")) |>
deframe()
sue <-
readLines("input.txt") |>
map(\(x) {
x |>
str_split_1("[:, ]+") |>
t() |>
data.frame()
}) |>
bind_rows() |>
select(-X1) |>
`colnames<-`(
c("sue", "item_1", "qty_1", "item_2", "qty_2", "item_3", "qty_3")
) |>
mutate(across(c(sue, starts_with("qty_")), as.integer)) |>
pivot_longer(
cols = c(starts_with("item_"), starts_with("qty_")),
names_pattern = "(item|qty)_(.+)",
names_to = c(".value", "sue_item")
) |>
mutate(ticker_qty = ticker[item])
16: Aunt Sue
Hmmm. Just need to get the data in longer format then I can group by sue and filter as needed.
Part 1
Only tricky bit was remembering how to pivot longer with multiple columns.
Part 2
Not too bad. Just a gross filter and we’re good to go. Oh and don’t forget to put your !
in the right spot :).