DEV Community

Cover image for I Was Told There Would Be No Math
Robert Mion
Robert Mion

Posted on

I Was Told There Would Be No Math

Advent of Code 2015 Day 2

Part 1

  1. Oh, there will be plenty of math.
  2. Using several familiar tools in my toolbox

Oh, there will be plenty of math.

Much to my delight, a majority of the math throughout Advent of Code was in celebration of patterns underlying various scientific, arithmetic or algorithmic formulas and proofs.

Using several familiar tools in my toolbox

  • reduce() to accumulate a total
  • split() to turn a string into an array
  • map() to coerce or manipulate data
  • array destructuring to concisely extract and store multiple values
  • sort() to, well, sort numbers
  • slice() to extract a subset of items from an array

My algorithm in JavaScript:

input.reduce(
  (sqft, box) => {
    let [l, w, h] = box.split('x').map(Number)
    let smallestArea = [l, w, h]
      .sort((a, b) => a - b)
      .slice(0, 2)
      .reduce((a, c) => a * c)
    return sqft += 2*l*w + 2*w*h + 2*h*l + smallestArea
  }
, 0)
Enter fullscreen mode Exit fullscreen mode

Part 2

A minor mathematical tweak

  • A shorter equation
  • And a relatively unchanged algorithm

My algorithm in JavaScript:

input.reduce(
  (sqft, box) => {
    let [l, w, h] = box.split('x').map(Number)
    let smallestPerimeter = [l, w, h]
      .sort((a, b) => a - b)
      .slice(0, 2)
      .map(el => el * 2)
      .reduce((a, c) => a + c)
    return sqft += l*w*h + smallestPerimeter
  }
, 0)
Enter fullscreen mode Exit fullscreen mode

Both parts, a year ago

This was my working algorithm in JavaScript when I first solved this puzzle:

function dayTwoPart1(input) {
  return input.map(box => wrappingPaperFor(box))
              .reduce((acc, curr) => { return acc + curr }, 0)
}

function dayTwoPart2(input) {
  return input.map(box => ribbonFor(box))
              .reduce((acc, curr) => { return acc + curr }, 0)
}

function wrappingPaperFor(box) {
  return surfaceAreaFor(box) + slackFor(box)
}

function surfaceAreaFor(box) {
  let [l, w, h] = box.split("x").map(i => +i)
  return (2 * l * w) + (2 * w * h) + (2 * h * l)
}

function slackFor(box) {
  let sortedBox = box.split("x").map(i => +i).sort((a,b) => a - b)
  return sortedBox[0] * sortedBox[1]
}
function ribbonFor(box) {
    let [s1, s2, s3] = box.split("x").map(i => +i).sort((a,b) => a - b)
    return (s1 + s1 + s2 + s2) + (s1 * s2 * s3)
}
Enter fullscreen mode Exit fullscreen mode
  • It's all very straightforward
  • Almost to its detriment
  • Then again, this puzzle did require a lot of straightforward math
  • Much like with Day 3, I'm very proud of the code I wrote my second time around

I did it!!

  • I solved both parts!
  • At first in a very separated, functional approach...then again in a more chained-together, eloquent approach!

While the math in this puzzle wasn't too intriguing, it served as a callback to all of the other puzzles that featured wonderfully-intriguing math.

One day left!

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay