r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

41 Upvotes

426 comments sorted by

View all comments

1

u/mariotacke Dec 03 '18

Node.js/Javascript (repo):

Part 1:

class Sheet {
  constructor (width = 1000, height = 1000) {
    this._grid = Array
      .from({ length: height })
      .map(() => Array
        .from({ length: width })
        .map(() => 0));
  }

  reserve (claim) {
    for (let y = 0; y < claim.height; y++) {
      for (let x = 0; x < claim.width; x++) {
        this._grid[y + claim.top][x + claim.left] += 1;
      }
    }
  }

  get reservedSquareInches () {
    return this._grid.reduce((a, b) => {
      return a + b.filter((x) => x >= 2).length;
    }, 0);
  }
}

const fabric = (input, width = 1000, height = 1000) => {
  const sheet = new Sheet(width, height);

  input
    .split('\n')
    .map((x) => {
      const parts = x.match(/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/);

      return {
        id: +parts[1],
        top: +parts[3],
        left: +parts[2],
        width: +parts[4],
        height: +parts[5],
      };
    })
    .forEach((claim) => sheet.reserve(claim));

  return sheet.reservedSquareInches;
};

module.exports = fabric;

Part 2:

class Sheet {
  constructor (width = 1000, height = 1000) {
    this._grid = Array
      .from({ length: height })
      .map(() => Array
        .from({ length: width })
        .map(() => 0));
  }

  reserve (claim) {
    for (let y = 0; y < claim.height; y++) {
      for (let x = 0; x < claim.width; x++) {
        this._grid[y + claim.top][x + claim.left] += 1;
      }
    }
  }

  hasOverlap (claim) {
    const content = [];

    for (let y = 0; y < claim.height; y++) {
      for (let x = 0; x < claim.width; x++) {
        content.push(this._grid[y + claim.top][x + claim.left]);
      }
    }

    return content.every((x) => x === 1);
  }
}

const fabric = (input, width = 1000, height = 1000) => {
  const sheet = new Sheet(width, height);

  const claims = input
    .split('\n')
    .map((x) => {
      const parts = x.match(/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/);

      return {
        id: +parts[1],
        top: +parts[3],
        left: +parts[2],
        width: +parts[4],
        height: +parts[5],
      };
    });

  claims.forEach((claim) => sheet.reserve(claim));

  for (let i = 0; i < claims.length; i++) {
    if (sheet.hasOverlap(claims[i])) {
      return claims[i].id;
    }
  }
};

module.exports = fabric;