# [adventofcode-day3] my solutions require always too many resources, advices?

I need help…
It works in small, but if I use the input of the actual challenge I get an error `TypeError: Cannot read property '99' of undefined at countOverlap`, which I imagine means there is an error occurring somwhere,
So, how can I make this thing more efficient?
next parts are blurred in case one wants to do the challenge later on, but the previous question is still valid, how to make things more efficient?
my interpretation of the challenge:

the example input is like this (already manipulated by me): “#1 @ 1,3: 4x4”, “#3 @ 3,1: 4x4”, “#3 @ 5,5: 2x2”
each string is indicating a rectangular part of a piece of cloth that in this case is 8x8
considering the first one: #1 is an ID; 1,3 are the distance from the left edge of the cloth and the left edge of the rectangle, and 3 is the top-top distance, and 4x4 is the dimension of the rectangle.
the challenge asks for the area in which different rectangles overlap, and in the case of the example input the result is 4

my approach to the challenge:

in my limited javascript knowledge, and after some failed attempts and inspiration from a gif of the challenge, I decided to create a matrix (arry of arrays) and +1 an item of the matrix when it was included in the rectangle being currently evaluated and at the end count the number of items in the matrix that are >1 , it works on small scale, only the input is 1353 items long and the matrix is 1000x1000

my code

``````var input = ["#1 @ 1,3: 4x4", "#2 @ 3,1: 4x4", "#3 @ 5,5: 2x2"];

function toArray(a) { //keeping only the numbers from the string
return a.split(/[^0-9]/)
.filter( e => {
if (e == '') {return false;}
return true;
}).map(e => {
return parseInt(e, 10);
});
}

function countOverlap(param, num) {
var map = []; //creating the map
for (let x = 0; x < num; x++) {
map.push([]);
for (let y = 0; y < num; y++) {
map[x].push(0);
}
}

for (var i = 0; i < param.length; i++) { //evaluating each string one by one
var a = toArray(param[i]);
var c1 = a[1] + 1;
var c2 = a[1] + a[3];
var d1 = a[2] + 1;
var d2 = a[2] + a[4];
for (var c = c1; c <= c2; c++) { //+1 to all items in the rectangle
for (var d = d1; d <= d2; d++) {
let beNice = map[c][d];
map[c][d] = beNice + 1;
}
}
}
var overlapCount = 0; // counting items of the matrix that are >1
for (var m = 0; m < map.length; m++) {
for (var n = 0; n < map[m].length; n++) {
if (map[m][n] > 1) {
overlapCount++;
}
}
}
console.log(map);
console.log(overlapCount);
return overlapCount;
}

countOverlap(input, 8);
``````

I only started AoC late yesterday night, so only got first one done; I should be on that one later tonight and probably give better help at that point, but can you give a short explanation of the task + one of the small test cases with the expected output? (Put it in spoilers if you want just in case people doing it aren’t on that one yet)

blurred everything out and added more details, thank you

1 Like

I think I’m missing something obvious here, but what do the IDs represent? Just that there are two with the same ID

that was my doing a mess copying and pasting… they are consecutive IDs