04: Scratchcards
Part 1
This took me longer than it should’ve, mostly because of the multiplication factor. I’m happy with my code, but disappointed in how long it took.
Part 2
Part 2 follows the same theme as part 1. I’m ultimately really pleased with my code, but it took way too long and in this case could be faster too. The almost recursion but not quite was quite confusing to figure out and I’m sure my code makes even less sense than the problem does.
dt2 <- list()
dt |>
purrr::iwalk(\(x, idx) {
x1 <- break_line(x[1]); x2 <- break_line(x[2]);
i <- if (length(dt2) == 0) 1 else (unname(table(unlist(dt2))[idx]) + 1)
i <- if (is.na(i)) 1 else i
dt2[[idx]] <<- c(idx, rep((idx:(idx + sum(x1 %in% x2)))[-1], i))
})
dt2 |>
unlist() |>
length()
[1] 5329815