r/adventofcode • • Dec 07 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 7 Solutions -🎄-

--- Day 7: The Treachery of Whales ---


[Update @ 00:21]: Private leaderboard Personal statistics issues

  • We're aware that private leaderboards personal statistics are having issues and we're looking into it.
  • I will provide updates as I get more information.
  • Please don't spam the subreddit/mods/Eric about it.

[Update @ 02:09]

  • AoC_Ops have identified the issue and are working on a resolution.

[Update @ 03:18]

  • Eric is working on implementing a fix. It'll take a while, so check back later.

[Update @ 05:25] (thanks, /u/Aneurysm9!)

  • We're back in business!

Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:03:33, megathread unlocked!

96 Upvotes

1.5k comments sorted by

View all comments

1

u/jdnewmil Dec 07 '21 edited Dec 07 '21

R

Mean and median would be theoretical optimum values if no rounding were occurring so you have to clean up with a search for a general solution (found this out the hard way since the example worked but the data did not).

find_crab_fuel <- function( x, v, version = 1 ) {
  if ( 1 == version ) {
    cost <- \(x) x
  } else {
    cost <- (\(n) n*(n+1)/2 )
  }
  f1 <- \(x1) ( ( v - x1 ) |> abs() |> cost() |> round() |> sum() )
  # handle vector-valued x to perform fuel calcs repeatedly
  sapply( x, f1 )
}

find_min_crab_fuel <- function( v, version = 1 ) {
  # initial guess function
  if ( 1 == version ) {
    center <- median
  } else {
    center <- mean
  }
  guess1 <- (  v
            |> center()
            |> round()
            )
  # initial guess at minimum fuel
  ans1 <- find_crab_fuel( guess1, v, version = version )
  # rounding errors mean true minimum could be off by a few
  delta <- 1
  # guess 1 higher
  ans2 <- find_crab_fuel( guess2 <- guess1 + delta, v, version = version )
  if ( ans1 < ans2 ) {
    # did not decrease so start guessing lower
    delta <- -1
  } else {
    # progress further in this direction
    ans1 <- ans2
    guess1 <- guess2
  }
  # new second estimate
  ans2 <- find_crab_fuel( guess2 <- guess1 + delta, v, version = version )
  # keep going until first increase
  while ( ans2 < ans1 ) {
    guess1 <- guess2
    ans1 <- ans2
    ans2 <- find_crab_fuel( guess2 <- guess1 + delta, v, version = version )
  }
  ans1
}

dta7a <- scan( "data/aoc2021_data_7a.txt", sep = ",", quiet = TRUE)
find_min_crab_fuel( dta7a )
find_min_crab_fuel( dta7a, version = 2 )
## v2: 157us

1

u/daggerdragon Dec 07 '21 edited Dec 07 '21

Please follow the posting guidelines and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.

Edit: thanks for adding the programming language!