codebuddies / DailyAlgorithms

Do a problem. Create (or find) your problem in the issues. Paste a link to your solution. See others' solutions of the same problem.
12 stars 1 forks source link

[Advent of Code] Day 2: Inventory Management System #3

Open billglover opened 5 years ago

billglover commented 5 years ago

Day 2: Inventory Management System

Source: adventofcode.com/2018/day/2

Part 1

You stop falling through time, catch your breath, and check the screen on the device. "Destination reached. Current Year: 1518. Current Location: North Pole Utility Closet 83N10." You made it! Now, to find those anomalies.

Outside the utility closet, you hear footsteps and a voice. "...I'm not sure either. But now that so many people have chimneys, maybe he could sneak in that way?" Another voice responds, "Actually, we've been working on a new kind of suit that would let him fit through tight spaces like that. But, I heard that a few days ago, they lost the prototype fabric, the design plans, everything! Nobody on the team can even seem to remember important details of the project!"

"Wouldn't they have had enough fabric to fill several boxes in the warehouse? They'd be stored together, so the box IDs should be similar. Too bad it would take forever to search the warehouse for two similar box IDs..." They walk too far away to hear any more.

Late at night, you sneak to the warehouse - who knows what kinds of paradoxes you could cause if you were discovered - and use your fancy wrist device to quickly scan every box and produce a list of the likely candidates (your puzzle input).

To make sure you didn't miss any, you scan the likely candidate boxes again, counting the number that have an ID containing exactly two of any letter and then separately counting those with exactly three of any letter. You can multiply those two counts together to get a rudimentary checksum and compare it to what your device predicts.

For example, if you see the following box IDs:

What is the checksum for your list of box IDs?

Part Two

Confident that your list of box IDs is complete, you're ready to find the boxes full of prototype fabric.

The boxes will have IDs which differ by exactly one character at the same position in both strings. For example, given the following box IDs:

What letters are common between the two correct box IDs? (In the example above, this is found by removing the differing character from either ID, producing fgij.)

billglover commented 5 years ago

My solution: github.com/billglover/aoc/tree/master/2018/02

For day 2, I abandoned the use of tests and went for a single main package. This saves a bit of time but doesn't feel like the right approach. I wonder if test stubs could be written such that they could be used as a starting template for each new puzzle.

ISPOL commented 5 years ago

https://repl.it/@ISPOL/AdventOfCode2018 Python solution (fpath is filepath)

lpatmo commented 5 years ago

Slightly different Python solution: https://repl.it/@lpatmo/AdvancedOptimisticFile

gauravchl commented 5 years ago

JS solution:

#!/usr/bin/env node

const fs = require("fs");
const input = fs.readFileSync("./day2.input", { encoding: "utf8" });

function getChecksum(input) {
  const ids = input.trim().split("\n");
  let appearanceOfTwo = 0;
  let appearanceOfThree = 0;

  ids.forEach(id => {
    const countTable = {};
    id.trim()
      .split("")
      .forEach(char => {
        countTable[char] = countTable[char] ? ++countTable[char] : 1;
      });
    if (Object.values(countTable).indexOf(2) > -1) appearanceOfTwo++;
    if (Object.values(countTable).indexOf(3) > -1) appearanceOfThree++;
  });
  return appearanceOfTwo * appearanceOfThree;
}

function getDiffer(input) {
  const ids = input.trim().split("\n");
  let result = '';

  for (let i = 0; i < ids.length - 1; i++) {
    for (let j = i + 1; j < ids.length; j++) {
      let differCount = 0;
      let diffIndex = -1;
      for (let k = 0; k < ids[i].length; k++) {
        if (ids[i][k] != ids[j][k]) {
          differCount ++;
          diffIndex = k;
        }
        if (differCount > 1) {
          diffIndex = -1;
          break;
        }
      }

      if (differCount === 1) {
        let result = ids[i].split('')
        result.splice(diffIndex, 1);
        return result.join('')
      }
    }
  }
  return result;
}

console.log("Checksum:", getChecksum(input), 'Differ', getDiffer(input));

https://github.com/gauravchl/sourcecode/blob/master/adventofcode/day2.js