DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 1: The Tyranny of the Rocket Equation

Collapse
 
lindakatcodes profile image
Linda Thompson

I would love to be part of some DEV leaderboards! I'm not showing myself as part of any right now, and am more than willing to use my own private one as a DEV-centric one if we need it. :) Do we have a way to share those so we can start joining?

I'll be doing my solutions in JavaScript - they're usually fairly verbose, since it helps me think better that way. lol :) So likely pretty beginner-friendly!

Here's day 1's solutions:

const fs = require('fs');
const data = fs.readFileSync('../2019 Solutions/inputs/day01input.txt').toString();
const input = data.split('\r\n').map(Number);
const testInput = [
  14,
  1969,
  100756
];

// mass / 3, round down, -2
function formula(mass) {
  return Math.floor(mass / 3) - 2;
}

// Part 1
const fuelOfMass = input.map((curr) => {
  return formula(curr);
});

const totalFuel = fuelOfMass.reduce((acc, curr) => {
  return acc + curr;
}, 0);

console.log(`Part 1: ${totalFuel}`);

// Part 2

const totalFuelOfMass = input.map((curr) => {
  let value = curr;
  let accumulator = [];

  do {
    value = formula(value);
    if (value > 0) {
      accumulator.push(value);
    }
  } while (value > 0);

  return accumulator.reduce((acc, curr) => {
    return acc + curr;
  }, 0);
});

const newTotalFuel = totalFuelOfMass.reduce((acc, curr) => {
  return acc + curr;
}, 0);

console.log(`Part 2: ${newTotalFuel}`);
Collapse
 
jbristow profile image
Jon Bristow

Post it here and I'll add it to the post and on further days posts! Go to you private leaderboard page and copy the code into a reply.

Collapse
 
lindakatcodes profile image
Linda Thompson

Alright, here's the code - 120635-5c140b9a.

We can make it for basically anyone on DEV, or we can focus it towards more beginner-friendly if we'd like - whatever works best for everyone!

Thread Thread
 
jbristow profile image
Jon Bristow

Updated the post to include it! Thank you!

Thread Thread
 
ballpointcarrot profile image
Christopher Kruse

Awesome! Thanks for creating a leaderboard for DEV.

Thread Thread
 
moopet profile image
Ben Sinclair

I'm in.

Collapse
 
coolshaurya profile image
Shaurya
Collapse
 
jnschrag profile image
Jacque Schrag

Ooh this is a great use of do...while. I went with the less elegant nested if for mine 😅

Part 1 (JavaScript)

const fs = require("fs");
const path = require("path");
const text = fs.readFileSync(path.join(__dirname) + "/input.txt", "utf8");
let output = text
  .split("\n")
  .map(d => calcFuelPerModule(d))
  .reduce((acc, curr) => acc + curr, 0);
console.log(output);

function calcFuelPerModule(mass) {
  return Math.floor(+mass / 3) - 2;
}

Part 2 (JavaScript)

const fs = require("fs");
const path = require("path");
const text = fs.readFileSync(path.join(__dirname) + "/input.txt", "utf8");
let output = text
  .split("\n")
  .map(d => calcFuel(d))
  .reduce((acc, curr) => acc + curr, 0);
console.log(output);

function calcFuel(mass) {
  let fuel = calcFuelPerModule(mass);
  if (fuel > 0) {
    let fuelFuel = calcFuel(fuel);
    if (fuelFuel > 0) {
      fuel += calcFuel(fuel);
    }
  }
  return fuel;
}

function calcFuelPerModule(mass) {
  return Math.floor(+mass / 3) - 2;
}
Collapse
 
lindakatcodes profile image
Linda Thompson

Still works well! As long as it gets you the answer, it's good! :)

I saw someone use a ternary operator, and was super impressed. Might refactor mine later today to test that. :) Didn't even think of it!

Collapse
 
brandonschreck profile image
Brandon Schreck • Edited

Very nice, I need to step my JS game up!

const data = [148216, 142030, 129401, 74642, 108051, 54128, 145495, 67818, 120225, 67113, 
107672, 101032, 147714, 55788, 87732, 73681, 114646, 76586, 116436, 139788, 125150, 136675, 
90527, 74674, 105505, 146059, 52735, 101389, 108121, 62897, 132337, 51963, 129188, 122308, 
84677, 66433, 118374, 66822, 94714, 101162, 54030, 136580, 55677, 114051, 133898, 95026, 
112964, 68662, 85139, 53559, 84703, 92053, 132197, 60130, 63184, 86182, 113038, 52659, 
140463, 123234, 97887, 70216, 131832, 108162, 116759, 111828, 132815, 113476, 127734, 
134545, 99643, 141911, 74705, 65720, 95640, 51581, 66787, 147590, 72937, 148774, 
119881, 139875, 131976, 68238, 100342, 134691, 112320, 86107, 100045, 120458, 
54459, 52047, 108226, 102138, 141233, 54452, 67859, 105132, 81903, 104282];

function calculateFuel(mass) {
  return Math.floor(mass / 3) - 2;
}

function getTotalFuel(mass) {
  let fuel = calculateFuel(mass);
  return fuel > 0 ? fuel += getTotalFuel(fuel) : 0;
}

function outputTestResults(index, testCase, result) {
  console.log(`Test ${ index + 1 }\n` +
    `Input: ${ testCase.input }\n` +
    `Expected Result: ${ testCase.output }\n` +
    `Result: ${ result }\n` +
    `Passes Test: ${ testCase.output === result }\n\n`);
}

// Part One
console.log(`Begin Part One`);
let partOneResult = 0;
const partOneTestCases = [{
    input: 12,
    output: 2
  },
  {
    input: 14,
    output: 2
  },
  {
    input: 1969,
    output: 654
  },
  {
    input: 100756,
    output: 33583
  },
];

// part one test cases
partOneTestCases.forEach(function(testCase, index) {
  outputTestResults(index, testCase, calculateFuel(testCase.input));
});

// part one do work
data.forEach(function(mass) {
  partOneResult += calculateFuel(mass);
});
console.log(`Part One Answer: ${partOneResult}`);

// Part Two
console.log(`Begin Part Two`);
let partTwoResult = 0;
const partTwoTestCases = [{
    input: 14,
    output: 2
  },
  {
    input: 1969,
    output: 966
  },
  {
    input: 100756,
    output: 50346
  }
];

// part two test cases
partTwoTestCases.forEach(function(testCase, index) {
  outputTestResults(index, testCase, getTotalFuel(testCase.input));
});

// do work
data.forEach(function(mass) {
  partTwoResult += getTotalFuel(mass);
});
console.log(`Part Two Answer: ${partTwoResult}`);
Collapse
 
lindakatcodes profile image
Linda Thompson

Also, I couldn't resist the idea of the poem challenge they're doing on the subreddit, so I wrote an acrostic. :) Sharing here as well!

Adventure awaits!
Discover the cosmos
Venture into the unknown
Earn fifty stars to save Christmas!
No one goes alone, however
There's friendly folks to help

Overly dramatic situations await
Find Santa and bring him home!

Come code with us!
Outer space is calling
Don't be afraid
Elves will guide the way!