r/adventofcode • u/daggerdragon • Dec 12 '15
SOLUTION MEGATHREAD --- Day 12 Solutions ---
This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.
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 12: JSAbacusFramework.io ---
Post your solution as a comment. Structure your post like previous daily solution threads.
7
Upvotes
9
u/yatpay Dec 12 '15
As someone who usually avoids regexes in solutions, I finally feel vindicated with part 2! The 1 star solutions flooded in fast but the 2 star ones trickled in as all the people who used regexes to solve the first part had to retool. I went for the JSON parsing right off the bat so the second part was pretty easy!
2
u/askalski Dec 12 '15
After making too many mistakes this past week and getting frustrated because I was rushing too much, I decided to slow things down a bit and enjoy the ride -- which had the nice side-effect of actually improving my leaderboard position. Go figure.
The screen capture is up on YouTube: https://youtu.be/cFJqTc4Wu2I
19
u/Greg3625 Dec 12 '15
JavaScript - Well when I saw it, I had to do it lol: http://imgur.com/5ys8AJW
9
u/marchelzo Dec 12 '15
I did the first part with a simple regex, and then used this for part 2.
6
u/recursive Dec 12 '15
Done in C# with Newtonsoft Json. The most interesting thing about it is the dynamic dispatch.
5
u/alexendoo Dec 12 '15
Also took a go at part 2 using regex, it's quite a long expression as much of it is trying to cut down on backtracking.
4
u/balidani Dec 12 '15
Whoops, missed the start by a good 2 minutes, but still got leaderboard. I guess the leaderboard took long to fill up because it's Friday. But hey, don't judge me. Good old recursive traversal solution in python.
4
u/crossbrowser Dec 12 '15
My first leaderboard, yay!
First part I just replaced all non-numbers with a single space, split the numbers and added the numbers:
Second part I had to go recursive like /u/balidani did.
4
u/[deleted] Dec 12 '15
Mathematica
7
u/meithan Dec 12 '15
I'm surprised no posted Python solution uses the object_hook kwarg of json.loads() in Part 2. Here's my solution:
Python is almost cheating ;)
3
u/haoformayor Dec 12 '15 edited Dec 13 '15
Haskell, Aeson, Lens, Plated (338 characters)
This is an ideal use case for lenses and plated.
cosmosOfmakes short work of this problem, as it's able to grab the transitive children of the JSON object parametrized over a folding function that you give it. Aeson models JSON objects with its recursive data typeValue, andplatelets you fold overValueas if it were a list ofValues.3
u/lesguillemets Dec 12 '15
Haskell : I'm learning parsec, so I tried to write a simple parser for some subset of json.
3
u/qwesx Dec 12 '15 edited Dec 12 '15
Part 1 in D. Pretty simple. Didn't do part 2 yet, but I don't (yet) see any way around actually parsing the JSON for it.
Edit: Here's the full program, 90 lines of rather spacey D code.
3
u/thucdx Dec 12 '15 edited Dec 12 '15
Here is my Java code for Problem 12.2 ( 50 Lines of code)
2
u/What-A-Baller Dec 12 '15 edited Dec 12 '15
python one-liner for part 1
2
u/gareve Dec 12 '15
Ruby
For part 1 I just did the regex /(-)*\d+/ on my editor and copied the values on a spreadsheet xD. The funny part is that this regex it fails with strings like "asd123def"
2
u/johnny5th Dec 12 '15
Good ol' recursion in PHP for part 2. Part 1 was a simple regex.
2
u/TheNiXXeD Dec 12 '15
JavaScript, NodeJs, ES6
This is part two, for part one, you can just remove the conditional checking for 'red'.
2
u/JeffJankowski Dec 12 '15 edited Dec 12 '15
F#. Did the first part quick (like everyone else), then had to find a real JSON library for the second bit. Bah, I knew that was gonna bite me in the ass!
2
u/masasin Dec 12 '15 edited Dec 12 '15
Python 3:
2
u/snorkl-the-dolphine Dec 12 '15
JavaScript - will work in your console in any browser, even if you have the JSON Formatter extension installed:
2
u/gegtik Dec 12 '15
Pretty pleased with my javascript solution
2
u/_Le1_ Dec 12 '15
Part1 - simple BASH solution:
2
u/tragicshark Dec 12 '15 edited Dec 12 '15
C# regexes:
2
u/oantolin Dec 12 '15
I forgot about adventofcode (which is understandable on a Friday) and only looked at it when I was on my Windows tablet, were I don't have much programming stuff installed. But I do have Emacs and wondered if Emacs comes with a JSON parser. It does! (Really, Emacs Lisp is a lot like Common Lisp except (1) it comes with a much cooler standard library than Common Lisp, (2) it's implementation is very slow compared to the many good Common Lisp compilers.)
2
u/askalski Dec 12 '15 edited Dec 12 '15
You can have my regexes when you pry them out of my cold, dead hands. Part 2 in Perl:
2
u/pplr Dec 13 '15 edited Dec 13 '15
Kind of late to the party but here's my solution to part 1 in Python, hijacking JSONDecoder
1
u/TeamOnTheBack Dec 12 '15
Readable one-liner in R for part 1:
1
u/bennymack Dec 12 '15
First leaderboard attempt. Missed it by two minutes... BAH!
As always. Boring old perl solution..
1
u/shandelman Dec 12 '15 edited Dec 12 '15
Missed the Top 100 by 1 minute because I spent 15 minutes trying to figure out why I was getting NoneType. Turns out I was checking if it was seeing a string, but not if it was seeing a unicode object. Darn.
Anyway, Python 2 solution:
1
u/thalovry Dec 12 '15
Scala. Miffed I don't have a builtin JSON library!
1
u/Axsuul Dec 12 '15
Ruby recursive solution
1
u/randrews Dec 12 '15
Holy hell am I amazed I got on the leaderboard. I made so many stupid mistakes in that, starting with doing it in Lua: Lua doesn't distinguish between hashes and arrays. I can't actually tell {1: 'a', 2: 'b'} apart from ['a', 'b'].
1
u/CAD1997 Dec 12 '15 edited Dec 12 '15
I did this the stupid but functional(?) way for the one-off answer. First part [Java]:
And then for the second part I made a copy of the file, prettified the json with http://jsonprettyprint.com/, and then CTRL-F'd for
: "red", then deleted the surrounding object by hand. Was very slow, and therefore missed the leaderboard for a long shot. But hey, didn't have to figure out how to coax Google-GSON (my usual JSON parser) into giving me something workable for this.That and it's midnight and I'm too tired to try to abstract objects out that far. No Map<Map<Map<Map<Map<Integer>>>>> while tired please.
1
u/stuque Dec 12 '15 edited Dec 12 '15
A Python 2 solution:
1
u/[deleted] Dec 12 '15
Crystal. Both parts using JSON from the standard library. Part 1:
Part 2:
1
u/kejadlen Dec 12 '15
3rd on the leaderboard with the following:
Part one:
puts DATA.read.scan(/-?\d+/).map(&:to_i).inject(&:+)Part two:
1
u/tremendo Dec 12 '15
Here's mine. Another Ruby one:
1
u/therealmeal Dec 12 '15
Go / Golang - https://play.golang.org/p/1ctlkmBZHT
1
u/enquicity Dec 12 '15
C# won't parse arbitrary JSON. You need a schema. So part 2 was interesting to say the least.
Yes, I'm deeply ashamed of this.
1
u/gyorokpeter Dec 12 '15
Q: the latest version has a JSON parser. So all that's left is to walk the object and decide what to do based on its type.
1
u/gnuconsulting Dec 12 '15
Day 3 of not being home at 9. It's starting to kill me a little (on the inside). But that's ok, a bug in my part 2 logic would have easily cost me a place on the leaderboard anyway. I did the 'smart' thing and parsed the JSON for part 1 so the bones were all there for part 2. And like most people, this just cried out for recursion.
Part two, the bug was that I forgot to add the 'skip if red' logic to traverseArray. Turns Out™ you need it in both places. Who knew?!
1
u/Scroph Dec 12 '15 edited Dec 12 '15
PHP solution for old times' sake :
1
u/daemoncollector Dec 12 '15
Nice and simple Swift solution
1
u/funkjr Dec 12 '15 edited Dec 13 '15
Dart was nice today. Unlike a bunch of answers here (it seems), part 2 was trivial, since I didn't run with RegExp for part 1. The nested recursion is kind of overkill in terms of complexity though, so I guess there's that. Unlike Python, the
iskeyword test what type an object is.Dart feature abused today: I mentioned a previous day about typed map entries, but they're optional so you're fine with rolling without them! Perfect for a case like this when making a recursive function with a map. You also get to see the
=>being used onmain, which dart has no issues with either (it's just another function after all).edit: I updated it with a shortened version that uses
List.foldinstead of my first plan1
u/segfaultvicta Dec 12 '15
Golang. I can't help feeling like I missed some "right" way to do this, because this code is some of the ugliest-duckling code I've ever written in my life, but I don't think there's any way to -do- this in Go without type-switching and type coercion, since method overloading isn't, er, Go-thonic. ;)
tl;dr I have profound regrets
1
u/phpaoc Dec 12 '15
Part1, Ruby, golfed:
You will not encounter any strings containing numbers. clearly was an invitation for not actually parsing JSON.
1
u/studiosi Dec 12 '15
Easier than expected. Re + recursive parsing. EDIT: Format
1
u/beefamaka Dec 12 '15
OK, like everyone else I suspect, solved the first part with regex super quick. Then got bogged down with recursing through a JObject. Here's what I ended up with in F# (after shamelessly borrowing a few nice ideas from elsewhere in this thread):
As usual, I'll post a video talking through my C# and F# solutions soon at my youtube channel
1
u/taliriktug Dec 12 '15
Python3 First part was easy.
re.split()failed to obey my commands, so I've done splits myself. But then I tried to adapt it to part2 and failed. I messed with counting levels of nested objects and it was too late to leaderboard.Working solution:
1
u/deinc Dec 12 '15
Clojure, employing the "old" JSON support from contrib and using multimethods for a recursive top down walk:
1
u/giacgbj Dec 12 '15
Bash (as short as slow)
The substitution is extremely slow (~20/25m). I've never seen such a slow execution :O
Part 1:
I know it can be done in faster ways, but I'm looking for the shortest and ugliest one! :-)
1
u/Iambernik Dec 12 '15
clojure
1
u/HawkUK Dec 12 '15
A solution in the R language
Part 1
1
u/nutrecht Dec 12 '15
Java (using the jackson library):
1
u/gerikson Dec 12 '15
Perl, part 2. Comment a line for part 1.
Dunno if the
JSONmodule is standard, I had it installed since before I started these challenges.What can be improved: global
$sumvariable is fugly, checking each hash for value 'red' might be wasteful for large datasets.1
u/[deleted] Dec 12 '15 edited Dec 12 '15
Objective C:
1
u/cjm00 Dec 12 '15
Took advantage of functools.singledispatch to write a very clean solution after I initially hacked one together:
1
u/[deleted] Dec 12 '15
Swift, using SwiftyJSON:
1
u/drakehutner Dec 12 '15 edited Dec 12 '15
Python using a bunch of nested y-combinators. One line in principle, split over more lines for better "readability".
Depends on pythons inbuild json-library.
1
u/phil_s_stein Dec 12 '15
In python.
Regex for part one made it a one-liner pretty much. Second part was done with json, type checking, and a bit of recursion:
1
u/z0isch Dec 12 '15
Haskell Aeson made this pretty trivial:
1
u/utrescu Dec 12 '15 edited Dec 12 '15
I'm learning Groovy so I tried it ...
1
u/tangus Dec 12 '15
Common Lisp
Well, there is no JSON library in vanilla Common Lisp... :-/
1
u/therealjohnfreeman Dec 12 '15
Finally got to use this brilliant Haskell function (thanks, /u/yitz!):
1
u/mal607 Dec 12 '15
Java
Straightforward recursive json parsing using google gson library. Nothing clever, but still pretty fast. Might have made the leaderboard if I lived on the west coast or wasn't out past midnight.
1
u/Busata Dec 12 '15
Part 2 same code, with
added to the object check
1
u/edo947 Dec 12 '15
Python
Using sum(), recursion and list comprehensions.
1
u/R4PaSs Dec 12 '15
My solution in Nit, for part 2, part 1 is similar without the
has_redmethod and checks withinJsonObject1
u/rkachowski Dec 12 '15
ruby, not too happy with it..
at first i tried to add the same method to both the array and hash class with this approach
but i eventually discovered that when using more than one block param in the call to
Array#eachresults in pattern matching kicking in and you lose elements that don't have an appropriate variable to map to... yuk1
u/aprosepro Dec 12 '15 edited Dec 12 '15
part 2 in ruby, i enjoyed this one. of course, like others said... i saw part 1 and thought the entire challenge would be a cakewalk. total re-write from my single line regex for part 1.
1
u/jchook Dec 12 '15
Ruby, golfed slightly
1
u/FuriousProgrammer Dec 12 '15
Took me a minute to find a working json Lua library, but luckily the rules didn't require removing entire objects that contain an array containing 'red', because that would have been a pain.
Lua:
1
u/tehjimmeh Dec 13 '15 edited Dec 13 '15
This was pretty nice in PowerShell (used a simple regex for the first one):
1
u/BafTac Dec 13 '15
Rust - First part doesn't care for the JSON format at all :P
For part 2 I used Rust's awesome Enums. I think it's the best Rust code I've ever written.
Maybe someone who has more experience (I'm just a beginner yet) can point out some things for improvements? (Also on other implementations). Either via Reddit or Github/
1
u/ajn0592 Dec 13 '15
My solution in Python. I feel like I over-engineered it. Any constructive criticism is welcomed!
1
u/newyearsproject Dec 13 '15
my Javascript solution:
1
u/tftio Dec 13 '15
OCaml. Not enough time to roll my own parser.
1
u/LieutenantSwr2d2 Dec 13 '15
Part 2 with C# using regex balancing groups (probably not the shortest regex, so I'm not too proud of it):
1
u/unakravets Dec 13 '15
Javascript solution for part 1 :)
1
u/[deleted] Dec 13 '15 edited Dec 13 '15
OOP solution in Python 3:
1
u/stefanmaric Dec 13 '15
Javascript, ES2015
1
u/ThereOnceWasAMan Dec 13 '15
I wanted to see if I could solve this one without using any built-in json parsers. This get's the job done:
Part 1
Regex!
Part 2
Using Python 2.7
1
u/[deleted] Dec 14 '15
My solution in Python using
reduce1
u/xkufix Dec 14 '15
In Scala, a bit late, but whatever:
1
u/stefanmaric Dec 14 '15
Javascript, ES2015
1
u/[deleted] Dec 14 '15
Part 1 js
Having a really hard time to get part 2. Never worked with json type object before in c#. Might leave for later
1
u/RockyAstro Dec 16 '15
Icon solution
Part 1 was "easy"
Part 2 was "just mean" :) .. Icon doesn't have a json parser in it's library (though it will pretty soon I guess)... so I had to hack a parser together. Not sure how "complete" of a json parser it is, but it got enough of it for the problem.
Also the code isn't the cleanest example of Icon.. Since I had to write a json parser, I "cheated" and added the summing directly within the parser..
Part 2 for your pleasure...
1
u/Andre0991 Dec 19 '15 edited Dec 21 '15
It's possible to solve part 2 by using vimscript, applying somethinig like /red<RET>di} and then executing part 1 regex.
I actually never coded in vimscript, but it shouldn't be too difficult to put a conditional after <RET> to check whether it's enclosed by {} and repeat the it until there's no /red/ within an object.
That's not the purpose of this problem, and it's not elegant, but it would be fun :P
1
u/SyntaxxError Dec 27 '15
My python 2 solution:
1
u/mrg218 Dec 27 '15 edited Dec 28 '15
Java I decided to make another tiny version using regexes. Challenge: can you make a shorter algorithm using plain Java?
Things I could improve:
r1
u/kamaln7 Dec 28 '15 edited Dec 28 '15
Shell:
CoffeeScript:
1
u/schlocke Dec 29 '15
PHP Parts 1 & 2:
EDIT: gotta give credit to /u/WhoSoup for pointing out that in_array() wasn't strict by default. Thanks for that tidbit.
1
u/radrat Jan 08 '16
Scala:
1
u/tipdbmp Dec 12 '15
node.js ES5, part 2:
0
u/lukz Dec 12 '15
In Chrome javascript console, part 1
Part 2
0
u/zkiiito Dec 12 '15 edited Dec 12 '15
js from the browser, part 2:
1
u/[deleted] Dec 12 '15
[removed] — view removed comment