re: AoC Day 5: Alchemical Reduction VIEW POST

VIEW FULL DISCUSSION
 

Late, but very happy with this one and had a lot of fun

Node.js

const isUpperCase = letter => letter === letter.toUpperCase();
const isLowerCase = letter => letter === letter.toLowerCase();
const lettersAreEqual = (a, b) => a.toUpperCase() === b.toUpperCase();
const last = array => array[array.length - 1];

const unique = array => [
  ...new Map(array.map(s => [s.toLowerCase(), s])).values()
];

const doesReact = (a, b) => {
  let reacts = false;
  if (
    (isLowerCase(a) && isUpperCase(b)) ||
    (isUpperCase(a) && isLowerCase(b))
  ) {
    if (lettersAreEqual(a, b)) {
      reacts = true;
    }
  }
  return reacts;
};

const removePolarity = polymer => {
  polymer = [...polymer];
  const output = [""];

  for (const char of polymer) {
    if (doesReact(char, last(output))) {
      output.pop();
    } else {
      output.push(char);
    }
  }

  // minus one for the emptry string at the start
  return output.length - 1;
};

const bestPolarity = polymer => {
  polymer = [...polymer];
  const uniqueLetters = unique(polymer);
  const results = uniqueLetters.map(letter => {
    const strippedPolymer = polymer.filter(c => !lettersAreEqual(c, letter));
    return removePolarity(strippedPolymer);
  });

  return Math.min.apply(null, results);
};
const fs = require("fs").promises;
const {removePolarity, bestPolarity} = require("./remove-polarity");

const main = async () => {
  try {
    const input = await fs.readFile(__dirname + "/input.txt", {
      encoding: "utf-8"
    });

    const part1 = removePolarity(input);
    console.log({part1});

    const part2 = bestPolarity(input);
    console.log({part2});
  } catch (e) {
    console.log(e.message);
    process.exit(-1);
  }
};

main();

Full code: github.com/MattMorgis/Advent-Of-Co...

code of conduct - report abuse