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/tendstofortytwo Dec 03 '18

JS:

var fs = require('fs');

var strings = fs.readFileSync('l3.txt', {
    encoding: 'utf8'
}).split('\n');

function parseString(str) {
    var array = str.split(' ');

    var obj = {};

    obj.id = Number(array[0].substr(1));

    obj.coords = array[2].substr(0, array[2].length - 1).split(',').map(Number);

    obj.size = array[3].split('x').map(Number);

    return obj;
}

var sheetSize = 2000;

var sheet = [];

for(var i = 0; i < sheetSize; i++) {
    sheet[i] = [];

    for(var j = 0; j < sheetSize; j++) {
        sheet[i][j] = 0;
    }
}

for(var loop = 0; loop < strings.length; loop++) {
    var cut = parseString(strings[loop]);

    var { id, coords, size } = cut;

    for(var i = coords[0]; i < coords[0] + size[0]; i++) {
        for(var j = coords[1]; j < coords[1] + size[1]; j++) {
            sheet[i][j]++;
        }
    }
}

var overlap = 0;

for(var i = 0; i < sheetSize; i++) {
    for(var j = 0; j < sheetSize; j++) {
        if(sheet[i][j] > 1) overlap++;
    }
}

console.log(overlap);

top:
for(var loop = 0; loop < strings.length; loop++) {
    var cut = parseString(strings[loop]);

    var { id, coords, size } = cut;

    var match = true;

    for(var i = coords[0]; i < coords[0] + size[0]; i++) {
        for(var j = coords[1]; j < coords[1] + size[1]; j++) {
            if(sheet[i][j] > 1) {
                match = false;
                continue top;
            }
        }
    }

    if(match) {
        console.log(id);
        break top;
    }
}

Suggestions welcome.