r/adventofcode • • Dec 13 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 13 Solutions -๐ŸŽ„-

--- Day 13: Packet Scanners ---


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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


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!

16 Upvotes

199 comments sorted by

View all comments

Show parent comment

1

u/gyorokpeter Dec 13 '17

Non-brute-force solution for part 2 - but apparently it's not much faster (6 sec vs 2 sec on my machine).

gcd:{$[x<0;.z.s[neg x;y];x=y;x;x>y;.z.s[y;x];x=0;y;.z.s[x;y mod x]]};
lcm:{(x*y)div gcd[x;y]};

d13p2:{
    v:"J"$trim each/:":"vs/:"\n"vs x;
    t:`p xasc ([]d:v[;0];p:1|2*v[;1]-1);
    t2:0!select d:(p-d)mod p by p from t;
    min last {al:x[1];m:x 0;nm:y`p;nbad:y`d;mul:lcm[m;nm];(mul;(raze al+/:m*til mul div m) except raze nbad+/:nm*til mul div nm)}/[(1;enlist 0);t2]};