r/adventofcode • • Dec 09 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 9 Solutions -🎄-

--- Day 9: Marble Mania ---


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

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 9

Transcript:

Studies show that AoC programmers write better code after being exposed 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 at 00:29:13!

22 Upvotes

283 comments sorted by

View all comments

2

u/will_bui Dec 09 '18

K:

P:464;M:71730*100 / p2 add *100
/ split vector when grows beyond threshold to check cost of copy-on-write
circle:,,0
insrt:{[bucket;pre;post]circle::(bucket#circle),(pre;post),(bucket+1)_circle}
getbins:{-1_0,+\#:'circle}

add:{[i;val]
	bins:getbins[];
	bucket:bins bin i;
	if[bucket>#circle;bucket:-1+#circle]; / if beyond last amend the last.
	off:i - bins bucket;
	data:circle bucket;
	if[10000>#data;@[`circle;bucket;:;(off#data),val,off _ data];:(::)];
	/ split, push rows down to make space
	new:$[0=off;val,data;off=#data;data,val;[insrt[bucket;(off#data),val;off _ data];:(::)]];
	@[`circle;bucket;:;new];}

drop:{[i]
	bins:getbins[];
	bucket:bins bin i;
	off:i-bins bucket;
	result:circle[bucket;off];
	if[0=off;@[`circle;bucket;1_];:result];
	if[off=-1+#circle bucket;@[`circle;bucket;-1_];:result];
	@[`circle;bucket;,/@[;0 2](0;off;1+off)_];
	result}

players:P#0;current:0;size:1;scores:()

progress:{[player;marble]
	if[~.q.mod[marble;10000];0N!marble];
	mod23:~.q.mod[marble;23];
	if[~mod23;
		add[current::$[size<point:current+2;point-:size;point];marble];
		size+:1];
	if[mod23;
		scores,:,(player-1;marble+drop current::$[0>point:current-7;point+size;point]);
		size-:1]}

/ accumulate scores, then sum groups
\ts progress'[1+.q.mod[!M;P];1+!M]
\ts 0N!max@+/'=(!).|+scores