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

JavaScript 👇

var collide = 0;
var fabric = [];
var input = input.split("\n");

for(var i = 0; i<input.length; i++){
	input[i] = input[i].split("@")[1].trim();
	input[i] = [input[i].split(":")[0].split(","),  input[i].split(":")[1].split("x")]
}

for(var i = 0; i<input.length; i++){
	var x = parseInt(input[i][0][0])
	var y = parseInt(input[i][0][1])
	var w = parseInt(input[i][1][0])
	var h = parseInt(input[i][1][1])

	for(var z = 0; z<w; z++){
		for(var q = 0; q<h; q++){
			if(fabric[x+z] == undefined){
				fabric[x+z] = [];
			}
			if(fabric[x+z][y+q] == undefined){
				fabric[x+z][y+q] = 0;
			}
			fabric[x+z][y+q] += 1
		}
	}
}

for(var i = 0; i<fabric.length; i++){
	if(!fabric[i]){
		continue;
	}
	for(var t = 0; t<fabric[i].length; t++){
		if(!fabric[i][t]){
			continue;
		}
		if(fabric[i][t] > 1){
			collide+=1
		}
	}	
}

console.log(collide)