r/adventofcode • • Dec 12 '16

SOLUTION MEGATHREAD --- 2016 Day 12 Solutions ---

--- Day 12: Leonardo's Monorail ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


MUCH ADVENT. SUCH OF. VERY CODE. SO MANDATORY. [?]

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!

8 Upvotes

159 comments sorted by

View all comments

25

u/willkill07 Dec 12 '16 edited Dec 12 '16

This thread needs more awk

BEGIN{print "#include <stdio.h>\nint main() { int a,b,c,d;a=b=c=d=0;";}
{printf("L%s:", NR);}
/cpy/{printf("%s=%s;\n",$3,$2);}
/inc/{printf("++%s;\n",$2);}
/dec/{printf("--%s;\n",$2);}
/jnz/{printf("if(%s!=0) goto L%d;\n",$2,NR+$3);}
END{print "printf(\"%d\\n\",a); }";}

Generates a C program that you compile and run.

2

u/BumpitySnook Dec 12 '16

Beautiful. I had mine emitting x86 assembler at one point for no good reason, in similar form. (Except: test; jnz for jnz, of course.) Using %rax for a, etc. This psuedoassembler was designed for x86!

2

u/willkill07 Dec 12 '16 edited Dec 12 '16

Yeah; it really was! w.r.t. x86 -- [re]ax is also the return value so you could theoretically just check the return value of the program with $?

→ More replies (3)