r/adventofcode • • Dec 06 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 6 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's theme ingredient is… *whips off cloth covering and gestures grandly*

Obsolete Technology

Sometimes a chef must return to their culinary roots in order to appreciate how far they have come!

  • Solve today's puzzles using an abacus, paper + pen, or other such non-digital methods and show us a picture or video of the results
  • Use the oldest computer/electronic device you have in the house to solve the puzzle
  • Use an OG programming language such as FORTRAN, COBOL, APL, or even punchcards

    • We recommend only the oldest vintages of codebases such as those developed before 1970
  • Use a very old version of your programming language/standard library/etc.

    • Upping the Ante challenge: use deprecated features whenever possible

Endeavor to wow us with a blast from the past!

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 6: Wait For It ---


Post your code solution in this megathread.

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:05:02, megathread unlocked!

46 Upvotes

1.2k comments sorted by

View all comments

2

u/babeheim Dec 06 '23 edited Dec 06 '23

[language: R]

Quadratic formula FTW!

calc_win_ways <- function(path, part1 = TRUE) {
  x <- readLines(path)
  times <- gsub("Time:\\s+", "", x[1])
  records <- gsub("Distance:\\s+", "", x[2])
  if (part1) {
    times <- as.numeric(unlist(strsplit(times, split = "\\s+")))
    records <- as.numeric(unlist(strsplit(records, split = "\\s+")))
  } else {
    times <- as.numeric(gsub("\\s+", "", times))
    records <- as.numeric(gsub("\\s+", "", records))
  }
  n_ways <- rep(NA, length(times))
  for (i in 1:length(n_ways)) {
    s1 <- (times[i] + sqrt(times[i]^2 - 4 * records[i])) / 2
    s2 <- (times[i] - sqrt(times[i]^2 - 4 * records[i])) / 2
    n_ways[i] <- floor(s1 - 1e-10) - ceiling(s2 + 1e-10) + 1
  }
  out <- prod(n_ways)
  return(out)
}

1

u/daggerdragon Dec 06 '23 edited Dec 06 '23

Inlined code is intended for short snippets of code only. On old.reddit, your one-liners get cut off when it reaches the edge of the window.

Please edit your post to put the one-liner in a four-spaces code block so it will be horizontally scrollable. edit: 👍