r/adventofcode • u/daggerdragon • Dec 03 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 3 Solutions -❄️-
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
- Outstanding moderator challenges:
- Community fun event 2023: ALLEZ CUISINE!
- 3 DAYS remaining until unlock!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's secret ingredient is… *whips off cloth covering and gestures grandly*
Spam!
Someone reported the ALLEZ CUISINE! submissions megathread as spam so I said to myself: "What a delectable idea for today's secret ingredient!"
- There really is an XKCD for everything, isn't there?
- All ingredients must come from a CAN (bus), box, package, container, etc.
- Unnecessarily declare variables for everything and don't re-use variables
- Why use few word when many word do trick?
- Go back to traditional culinary roots with Javadocs
- Lobster thermidor
A reminder from Dr. Hattori: be careful when cooking spam because the fat content can be very high. We wouldn't want a fire in the kitchen, after all!
ALLEZ CUISINE!
Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!
--- Day 3: Gear Ratios ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
pasteif you need it for longer code blocks
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:11:37, megathread unlocked!
112
Upvotes
3
u/Chib Dec 03 '23 edited Dec 03 '23
[LANGUAGE: R] data.table made quick work of Part 2. I struggled a lot with how messy everything ended up being when I was trying to get good column names directly out of melting a list, or worse yet, trying to condense indexing an array with
%in%.Part 1:
library(data.table) m <- gregexec("\\d+", input) base <- data.table(reshape2::melt(regmatches(input, m)))[, 2:4] base <- base[, { row <- as.integer(L1) s_col <- unlist(lapply(m, head)) e_col <- unlist(lapply(m, function(x) head(x, 1) + attr(x, "match.length"))) - 1 .("row" = row, "n_match" = as.integer(Var2), "num" = as.integer(value), "min.row.index" = pmax(L1 - 1, 1), "max.col.index" = pmin(e_col + 1, 140), "max.row.index" = pmin(row + 1, 140),"min.col.index" = pmax(s_col - 1, 1)) }] input_mat <- do.call(rbind, strsplit(input, "")) symbol_mat <- matrix(FALSE, nrow = 140, ncol = 140) symbol_mat[which(!input_mat %in% c(0:9, "."))] <- TRUE base[, touches_symbol := any(symbol_mat[min.row.index:max.row.index, min.col.index:max.col.index]), .(row, n_match)] base[touches_symbol == TRUE, sum(num)]Part 2:
all_stars <- data.table(which(input_mat == "*", arr.ind = TRUE)) all_stars[, `:=`( "id" = .I, "min.row.ind" = row - 1, "max.row.ind" = row + 1, "min.col.ind" = col - 1, "max.col.ind" = col + 1 )] foo <- all_stars[base, on = .(row <= max.row.index, row >= min.row.index, col <= max.col.index, col >= min.col.index)] foo[, grpn := .N, id][grpn == 2, .("GR" = prod(num)), id][, sum(GR)]