8
Haxk20
54d

# I probably chose the worst language to do AoC in. C jeez. Day 3 is a pain cause i gave myself to only use stdio and math .h and day 1,2 were fine. Day 3 i had to create well better show you. int map[40000][40000]; Horrible i know but i didnt fine better solution to it. Oh if you have better solution dont forget to paste the github link here.

Comments
• 18
You will be in a heap of trouble if you try that on the stack...
• 4
@Demolishun
Are you proud of that one?
• 5
That will not work with static allocation, you need to go via calloc because that's 6 GB.

The easiest way is to just allocate a plain 1D array and handle the indices via manual multiplication.

So instead of map[a][b] you'd write map[a*N + b]. Performance bonus if you can make N a power of two. Another and much larger performance bonus if your inner array traversal loops work on b, not on a.
• 1
@Fast-Nop wat even ? The first part OK. But once you said "So instead of map..." i was wat ?
• 1
Oh and the solution doesnt even fucking work WOW. fucking hell
• 1
@Haxk20 the "map" was the integer 2D array in your rant, and instead of generating a 2D array with indices a and b, you'd use the manual addressing scheme. That's what the compiler generates anyway.
• 1
@highlight
#define N 40000ULL
size_t i, j;
int32_t * map;
if ((map = calloc(N * N, sizeof(int32_t))) == NULL) {
/*error handling here*/
}

for (i = 200; i < 3000; i++)
for (j = 4000; j < 10000; j++)
map[i*N + j] = foo;
• 1
• 2
@Fast-Nop I was just about to write my solution must be totally flawed and that i must be dumb.

I was writing what my code does and then i spotted it.

Then i realized my HUGE mistake. i - 20000 from x and y. I had to do 20000 - x and same for y. Got answer and boom it worked just amazingly.

And i guess if i just run trough my code with my duck i would have spotted it.

But the solution is still horrible as code and just bleh. I need to learn to do C better. 9 or so years in it and i still have no clue how to use it properly jeeez.
• 1
Day 3 done.

Hopefully day 4 will be better and not something like this. Path tracing is piece of shit.
• 2
I did it in Rust by testing intersection of every segment.
I optimized it quite well for the first part, didn't have the courage to finish the second one ðŸ˜­

DAMN YOU, MICRO OPTIMIZATION
• 2
I just abandoned after I saw the monstrosity I wrote
• 1
@MagicSowap dont worry. Mine looked even worse. But the solution was good.
• 2
Doing day 3 today, will report back
• 1
@nitwhiz Its pain. Pain. Absolute pain.
• 2
@Haxk20 didn't get to do much, so I can't really tell. But my strategy is to keep the wires as lines from point to point (x0, y0, x1, y1), then check for all intersections between them and calculate their distance to the start.

Didn't have much time after work. But I wont forget you and share it with you when I'm done.(:
• 1
@nitwhiz be sure to. Would love to see it. In C right ?
• 2
@Haxk20 nah, I do it in JS, as I'm doing JS for a living.
Especially because with all the file parsing necessary, it'd create too much overhead imo.

But the idea of what I'm doing shouldn't be a problem in c. Let's guess our coordinates never exceed 65535:

struct line { short x0, short x1, ... }
struct line *wires;

wires = malloc (sizeof(struct line) * direction_count + 1);

direction_count being the comma separated instructions from the file. Plus one for 0,0 to first point.