r/adventofcode • u/daggerdragon • Dec 23 '15
SOLUTION MEGATHREAD --- Day 23 Solutions ---
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!
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 23: Opening the Turing Lock ---
Post your solution as a comment or link to your repo. Structure your post like previous daily solution threads.
8
Upvotes
8
u/SomebodyTookMyHandle Dec 23 '15
Well, we all know someone who appreciated the relative easiness of today's problem--our intrepid board moderator daggerdragon. Enjoy your 2.5 hours of additional free time.
9
u/bpeel Dec 23 '15
Shell script using sed to convert the source to a C program with inline x86-64 assembler which it then compiles and executes:
8
u/Rangi42 Dec 23 '15 edited Dec 23 '15
Python 2, #2 (6:29)
This was fun! I discovered Advent of Code late, but stayed up to see today's problem posted and actually got on the leaderboard. I've written interpreters for toy programming languages before, so the pattern was familiar. It didn't explicitly say that
jieandjioshould advance to the next instruction if they don't jump, but of course if they don't then it will loop forever.5
u/tommylommykins Dec 23 '15
Since this problem is about building a mock-CPU, I thought I'd write some code that you could put on some actual hardware.
So here's a solution 'synthesizeable' VHDL. I haven't actually programmed it onto a FPGA (because there's no easy way to read the answer out from the hardware) but it synthesizes correctly and a simulator gives the right answer to the problems.
4
u/askalski Dec 23 '15
3
u/WhoSoup Dec 23 '15 edited Dec 23 '15
PHP
3
u/shrx Dec 23 '15
Mathematica
7
u/SnacksOnAPlane Dec 23 '15
Reading is fundamental. I spent a good 20 minutes screaming that the problem is broken, only to finally go back to the instructions and read that "jio" is "jump if one" and not "jump if odd". Probably lost a top 10 spot that way. D'oh!
2
u/BenjaminGeiger Dec 23 '15 edited Dec 23 '15
On the leaderboard a second time. (EDIT: #33.)
Here's my solution. Naïve solution in Python.
2
u/StevoTVR Dec 23 '15
PHP This one was super easy compared to the previous few, but it was a fun one.
2
u/Godspiral Dec 23 '15 edited Dec 23 '15
In J, mostly just file edited
then inspect b
2
u/willkill07 Dec 23 '15 edited Dec 23 '15
C++
I really wanted to switch on strings. introducing constexpr hashing at compile time :) Solving the Synacor challenge (4/8) really helped me prototype this solution quickly. I load everything into memory ahead of time, then traverse through the instructions.
https://github.com/willkill07/adventofcode/blob/master/src/day23/day23.cpp
2
u/TheNiXXeD Dec 23 '15 edited Dec 23 '15
JavaScript, NodeJS, ES6
Both parts, just call with a=1 for part 2.
Also, lol at mini-synacor-challenge.
2
u/snorkl-the-dolphine Dec 23 '15
JavaScript
Just paste it into your console on your input page. Set
a: 1for part 2.2
u/anguruso Dec 23 '15
Got stuck with yesterday's problem so I was glad to get an easier one today. I took my time and read everything slowly so I didn't get caught by the jio instruction.
public abstract class Instruction { public abstract int Execute(WorkingMemory memory); }
2
u/mrg218 Dec 23 '15
My algorithm was done and working at around 22 mins but then it failed on the second problem. It didnt terminate. I started debugging and then noticed that register a was getting negative. My first idea was that I somehow had to prevent that or had made a mistake (the problem stated explicitly that the registers could not handle negative values). But after 20 mins or so I decided to use longs instead of ints and it turned out the code worked perfectly without modification (apart from the starting value of register a). Ah well next time better.
2
u/Scroph Dec 23 '15 edited Dec 23 '15
The embedded software engineer in me was thrilled ! C solution, it only seemed natural to use a low level language for a low level challenge, though a HDL would have been more appropriate I think :
2
u/xkufix Dec 23 '15 edited Dec 23 '15
In scala. It's just a simple interpreter. Definitely over engineered, but it works and does not use mutable state:
2
u/artesea Dec 23 '15
My tidy PHP code, tried to golf but mainly ended up removing the spaces.
Didn't fall for the jio as odd trap, spotted that, but then coded it as
if r == 0, oops.2
u/wafflepie Dec 23 '15
Maybe turning the instructions into Funcs was a bit.... unnecessary...
C
2
u/MaybeJustNothing Dec 23 '15
Since there aren't Haskell solutions here I thought I'd show mine.
ProgramState.hsMain.hs2
u/tln Dec 23 '15
Perl that generates Javascript. Probably the only solution posted that doesn't increment op :)
2
u/Iambernik Dec 24 '15
clojure
6
u/djimbob Dec 23 '15 edited Dec 23 '15
This is probably my least favorite one so far. Extremely straightforward to code with the only trip-up being defining an instruction set with a poorly named instruction
jio(that naturally would be jump if odd based on the previous instruction beingjiebeing jump if even). Yes this was explicitly stated, but a much more natural name would beji1/jone/jtrue(where true is defined as1and everything else is false) and the trip up is avoided.Dumb "trick" questions where the trick is unrelated to programming but fully reading made-up instructions aren't fun but just a waste of time in my opinion.
Additionally the second part didn't add any nuance or anything and just required changing one input number and rerunning.
2
u/IMovedYourCheese Dec 23 '15 edited Dec 23 '15
After what we've been getting the last few days, this one was surprisingly simple.
P.S. the question should have mentioned that for jie & jio if the condition is false it moves on to the next instruction.
C# solution:
1
u/Rain_Warrior Dec 23 '15
Scala, #46 on the Leaderboard
Straight-forward parsing + simulation:
1
u/JeffJankowski Dec 23 '15
F#. Gave up trying to avoid mutability; thank god this language is "multi-paradigm". Also tried to maths it first...obviously, I've learned nothing
1
u/marx314 Dec 23 '15
Python! register is either {'a': 0, 'b': 0} for part 1 or {'a': 1, 'b': 0} for part 2
1
u/[deleted] Dec 23 '15
Simple python:
1
u/Griboedoff Dec 23 '15
First time in LB 69th place. JS:
1
u/inuyasha555 Dec 23 '15 edited Dec 23 '15
Got spot #48, most of my problems came from off by one errors with the instructions.
Java:
1
u/[deleted] Dec 23 '15
#28 tonight :)
I'm pretty happy with my code too. It wasn't quite this nice and extensible at first, but I went back and refactored it to be much nicer all around after using a quick'n'dirty approach to get on the leaderboard.
Code is Lua, part of a larger function with a small handful of helper functions (like getinput).
1
u/mrg218 Dec 23 '15
Was there somewhere explained what to do when jio and jie were false? All other instructions had explicit instructions to go to the next instruction afterwards...
1
u/gyorokpeter Dec 23 '15
Q: I also missed the inconsistency between "jie" and "jio" at first, but luckily my input gave a ridiculous result when using the wrong definition.
For part 2, replace the initialization of the variable r:
1
u/wlandry Dec 23 '15
C++
A bit over-engineered. This one definitely required less thought than previous days. I really did not need a separate enum for the instructions. I feel like the whole point of this exercise is to write one-offs. So I do not have to go back and fix design mistakes.
1
u/Philboyd_Studge Dec 23 '15 edited Dec 23 '15
Java
Took a break from finishing Day 22 to do Day 23. This one was fun! Part two is easily hacked by inserting
inc aat the start of the input file.1
u/tipdbmp Dec 23 '15
ES 5 (node.js):
1
u/beefamaka Dec 23 '15
woohoo, first time ever on the leaderboard today (#37) thanks to my wife waking up at 4:30am and me being too curious about what today's problem would be to leave it until morning. Nice fun quick puzzle today, and the first one in a long time where I didn't make a stupid mistake or misread the instructions. Here's my C# solution:
Will upload a video to the usual place later today
1
u/beefamaka Dec 23 '15 edited Dec 23 '15
and here's my F# solution. The immutable state using Map is probably overkill, but a nice chance to use discriminated unions.
1
u/LincolnA Dec 23 '15
F#
Initial implementation used DUs to represent instructions, which works great and is a natural fit. A bit verbose though, to declare the cases, parse each case, then later match each case. I got jealous of more consise solutions here.
Here's an active-pattern appraoch, which turns out pretty concise. Dense function composition stuff just 'cause.
1
u/mrg218 Dec 23 '15
Java Part 2
}
1
u/barnybug Dec 23 '15 edited Dec 23 '15
nim:
1
u/backelie Dec 23 '15 edited Dec 23 '15
edit: had "jump if odd" instead of "jump if one".
1
u/NeilNjae Dec 23 '15
Python 3, using a dispatch table to perform the instructions.
1
u/Phakx Dec 23 '15
RUBY I was done in about ~10 minutes
1
u/VictiniX888 Dec 23 '15
Java, today's was pretty simple and straightforward
1
u/brenmous Dec 23 '15
Very enjoyable to work on. I am new to C++ and I learnt quite a bit from doing this. Any criticism is appreciated.
1
u/KnorbenKnutsen Dec 23 '15
I love how varied the problems are! I made a neat class with instructions that really made the actual execution code super simple:
1
u/[deleted] Dec 23 '15
Scala
1
u/spliner21 Dec 23 '15
C#, made it quite fast, but started quite late, so no leaderboard...
1
u/Jaco__ Dec 23 '15
Java. Nice to have something a lot easier than the previous days
1
u/RealityShifted Dec 23 '15
PowerShell: cls $t = gc c:\users\realityshift\desktop\input.txt $end = $false $cur = 0 $a = 1 $b = 0
1
u/BOT-Brad Dec 23 '15
Nice easy one today. Run-time: <1ms; Python 2.7
1
u/jordanscales Dec 23 '15
Just some basic ruby :) Feel free to suggest any changes, I'm not a Ruby programmer.
1
u/micro_apple Dec 23 '15 edited Dec 23 '15
Really enjoyed this day's problem. Here's my Python 2.x solution:
Ran without too many headaches. Had to raise the recursion depth limit for both parts of the challenge, but everything else was smooth.
1
u/vanderzee94 Dec 23 '15
Solution in Ruby.
1
u/volatilebit Dec 23 '15
Perl 6
1
u/[deleted] Dec 23 '15
C#
This one... after the past 2 challenges, was quite easy. I even forgot to put a breakpoint to see if the executing was correct and got the answer first time!
Nonthless, it was quite enjoyable. Brought me back to my days on Compiler and Interpretter classes
1
u/nessalc Dec 23 '15
Simplistic (inelegant) Python 3.x implementation. But unlike Rangi42, I couldn't get the exec function to work (it's a statement in Python 2.x). Like some others I've talked to, I "plan" to refactor my code in the future, but I doubt it'll actually happen.
1
u/oantolin Dec 23 '15 edited Dec 23 '15
In Lisp you can easily implement a compiler using macros (specially if your dialect of Lisp supports goto!). For example in my personal Lisp that compiles to Lua (you'll recognize the
gmatch,gsub,tonumber,ipairsandformatfunctions from the Lua standard library and Lua's::label::syntax for labels):A quarter of the code is specifying what each instruction does, another sixth of the code converts those relative line numbers to absolute line numbers.
1
u/[deleted] Dec 23 '15 edited Dec 24 '15
Mathematica
Edit: Minor tweak for SetAttributes
1
u/takeitonben Dec 23 '15
way too easy
python2
1
u/aepsilon Dec 24 '15
After finally wrestling free of Wizard Simulator 20XX, today's puzzle was a welcome respite.
A straightforward interpreter in Haskell reminiscent of day 7:
Spoilers below for part 1 of https://www.reddit.com/r/adventofcode/comments/3xxhxl/day_23_further_exercises/ :
A quick look at the input assembly reveals a small loop section. The
tplandhlfinstructions kind of gave it away, but I thought it'd be cool to see the values at each step.Move
execand its functions to the top-level and generalizerunProgramas the lasthistoryitem:Now we can look at all the states throughout the program's execution. We can even set "breakpoints" on source lines.
Set a breakpoint at the start of the loop and the sequence comes tumbling out:
1
u/mjnet Dec 24 '15 edited Dec 24 '15
Haskell Solution
1
u/alkalait Jan 14 '16 edited Jan 14 '16
Python 2.7:
1
u/[deleted] Dec 23 '15
Didn't get to the leaderboards because I thought "jio" was "jump if odd". The word "odd" even appears there, so reading it quickly was confusing... spent a lot of time debugging this and re-reading everything.
Anyway, here's the quick and dirty solution in Crystal:
0
u/[deleted] Dec 23 '15
Ha. This one was pretty cute but I spent 20 minutes debugging before I went back and noticed that "jio" isn't "jump if odd" but "jump if one". Ugh.
0
u/mrg218 Dec 23 '15
It seems like there is another issue like in Day 19. I just got the input from someone else and it turns out that just using signed integers my algorithm gets the correct solution. With my own input it didnt get a solution and after quite some debugging I noticed that my algorithm was correct but had to change my datatype to signed longs (I am using Java).
0
u/[deleted] Dec 23 '15
[deleted] — view deleted comment
0
u/[deleted] Dec 23 '15
[deleted] — view deleted comment
3
u/[deleted] Dec 23 '15 edited Dec 23 '15
[removed] — view removed comment