Unable to load image

Advent of code day 4 :marseysad:

This is the advent of code day 4 thread for bragging about your solution and bullying people who haven't gotten it yet :marseyill:

35
Jump in the discussion.

No email address required.

I'm never sure if calling the same function with the same args twice in the same line makes it compute twice of if it's optimized away, so added another functional composition layer on top. This way is faster for part 2 than just intersecting ranges. in matlab (:gigachad2:)

A = readlines("input.txt","EmptyLineRule","skip")';

comp1 = @(x) ((x(1)>=x(3))&&(x(2)<=x(4)))||((x(1)<=x(3))&&(x(2)>=x(4)));
comp2 = @(x) ((x(1)>=x(3))&&(x(1)<=x(4)))||((x(2)>=x(3))&&(x(2)<=x(4)));
aux = @(u) comp2(u)||comp2([u(3:4) u(1:2)]);

iterfunc = @(u) [comp1(u); aux(u)];
iterfunc2 = @(i) iterfunc(sscanf(i,'%d-%d,%d-%d'));

res = sum(cell2mat(arrayfun(iterfunc2,A,'UniformOutput',false)),2);

fprintf("Part 1 : %d\n",res(1))
fprintf("Part 2 : %d\n",res(2))
Jump in the discussion.

No email address required.

@Jinglevann now it's emotes that are broken (in preview) when you have a code block in the post :marseytroll:

![](/images/16701870064056458.webp)

Jump in the discussion.

No email address required.

CLEAN IT UP CODEJANNY

Jump in the discussion.

No email address required.

Wait I can just do comp2 = @(x) ~((x(2)<x(3))||(x(1)>x(4))); and chuck out the aux lmao

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.