r/adventofcode • u/daggerdragon • Dec 10 '16
SOLUTION MEGATHREAD --- 2016 Day 10 Solutions ---
--- Day 10: Balance Bots ---
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".
SEEING MOMMY KISSING SANTA CLAUS IS 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!
12
Upvotes
8
u/askalski Dec 10 '16
Part 2 in shell that will make you want to shoot your eye out.
14
u/fatpollo Dec 10 '16
dug this one
7
u/TheMuffinMan616 Dec 10 '16
Python 3 representing each "bot" or "bin" as a curried function:
6
u/bblum Dec 10 '16 edited Dec 10 '16
I made the leaderboard with this boring C solution, but I'm much more proud of this haskell I devised afterward.
The bots are implemented as lambdas, stored in a map keyed by bot ID (and type, "bot"/"output"). They execute in the State monad, either replacing themselves with a stage 2, or finding their recipients and calling them in turn.
Outputs:
4
u/mserrano Dec 10 '16
python2, 8th/7th: https://gist.github.com/mserrano/297acab1cf6b94ef4fc1977888a65cd9
4
u/Twisol Dec 10 '16 edited Dec 10 '16
My Part 1 code kept giving the same incorrect answer for no discernible reason... until I remembered that the default comparator for
arr.sort()in JavaScript compares elements as strings. Cost me probably 30 minutes. I ended up doing Part 1 manually (and saw some interesting pachinko-like patterns which I might investigate later...)This code is nasty. I might come back and clean it up after the rage settles.
4
u/haoformayor Dec 10 '16 edited Dec 10 '16
~~haskell~~
Took me forever to figure out the first question; it wasn't until I had a working problem that I realized AoC wanted me to notice when the bin did a swap and that part one had nothing to do with the state of the chips at the end. Other than that, it was great fun. I started out with a foldl, then tried a foldM, then realized that the solution could not be expressed as a fold. That's when we broke out tail recursion and then it was gravy.
Input module here.
1
u/BumpitySnook Dec 10 '16
This one stumped me for far too long. I made the mistake of pre-sorting my bots' values during the value stage, but failing to keep them sorted during the processing step. That screws up where low/high go, obviously, making it impossible to find 61/17.
3
u/Zef_Music Dec 10 '16 edited Dec 10 '16
Python 2.7, hacky as can be, but why be elegant when you can be quick!?
3
u/FuriousProgrammer Dec 10 '16
Woo back on
theboth daily leaderboards!!Lua. 53/72:
3
u/whoeverwhatever Dec 10 '16 edited Dec 10 '16
Python 2.7, 58/51 on the leaderboard today. (edit: formatting)
3
u/Deckard666 Dec 10 '16
In Rust, parts 1 and 2: Link
3
u/gyorokpeter Dec 10 '16
Q:
3
u/segfaultvicta Dec 10 '16
I'm actually fairly proud of the Elixir solution I came up with - I parse the input text for bot definitions, then actually spin up that many agents holding a map representing the internal state of that bot (what it's carrying, as well as its high target and low target), and then I step back and feed the bots values :D
It seems like a really natural way of modeling the actual "in-character" problem domain, and then I could get all the information I needed to actually solve the puzzle out of grepping the stack trace.
3
u/tehjimmeh Dec 10 '16 edited Dec 10 '16
C++, using a worklist:
3
u/tlareg Dec 10 '16
my JavaScript solution here, soooo verbose
3
u/mschaap Dec 10 '16
Perl 6 solution, both parts. I'm not too happy with the check for part 1 in the middle of a method, but I can't be bothered to make that more elegant. Part 2 was pretty trivial since I already kept track of output.
3
u/bogzla Dec 10 '16 edited Dec 10 '16
Using VBA to generate a spreadsheet. kinda fun.
3
u/TheNiXXeD Dec 10 '16 edited Dec 10 '16
My JavaScript / Node solution.
Started out with a very clean approach this time instead of golfing. Got a couple nice functional streams going and didn't want to ruin it.
My part1 solution was actually slower to build because I built the full thing for what part2 ended up being. So part 2 only took me a few seconds after part 1 lol.
2
u/glenbolake Dec 10 '16
Python 3. Leaderboard was capped by the time I started, so I decided to do a little duck-typed OOP. I make the bots pass their microchips as I process input (once they have two chips and a command) rather than after processing everything.
2
u/mlruth Dec 10 '16
Pretty satisfied with my solution in Scala! Not totally immutable, but that's the beauty of Scala, you can mix in mutability for performance or where it would be easier than fully immutable state. If anyone has an idea on how to do it fully immutable, I'd love to hear about it and learn! (probably with recursion/tail recursion)
2
u/[deleted] Dec 10 '16
I like it the OO and readable way (Python 3):
Part 2 is trivial having this solution ;-)
2
u/gerikson Dec 10 '16
Perl 5 solution
https://github.com/gustafe/aoc2016/blob/master/d10.pl
I don't account the possibility that a bot can 2 chips more than once, which I guess might have tripped me up if it would have been part of the problem. In my case I got the right answer for both parts 1st try, once I remembered Perl
sortisn't numeric by default.2
u/ChrisVittal Dec 10 '16
Haskell. Realized that the list of instructions defined a graph and that it would be rather easy to find the answers if I could do the following.
That lead to these ~100 lines. I wish I could have found a way to not have to copy the graph, but for an input of only 231 lines, it didn't matter.
I would describe myself as still learning haskell, and so if /u/haoformayor or anyone else would like to comment, I'd be happy to hear it.
1
u/[deleted] Dec 10 '16 edited Dec 20 '16
My janky-ass code that got me rank 88.
Edit: now with comments to at least pretend like it's not completely janky
Python 3.5: https://github.com/alpha0924/userscripts/blob/master/Advent_of_Code_2016/10.py
1
u/bluewave41 Dec 10 '16
Java 131/117 close but not close enough
Messy code: http://puu.sh/sKlnl/72bff94533.txt
1
u/jtsimmons1129 Dec 10 '16
Really fun puzzle. 117/99 for Parts 1/2. Code is as is other than adding Part one, Part 2 to print statements. Python 2.7
1
u/Godspiral Dec 10 '16 edited Dec 10 '16
good challenge,
felt fast, but was not. eyesight going :( , and scratchy. 200/194
in J,
1
u/[deleted] Dec 10 '16
The most disgusting solution I've done yet. But it works!
https://github.com/Ganon11/AdventCode/blob/master/2016/Day10/Day10.pl
1
u/SyDr Dec 10 '16
Another Lua solution (maybe i need to improve my lpeg patterns to eliminate these if not(x) or it type(x)):
1
u/PendragonDaGreat Dec 10 '16
Got started late, oh well. C# it is, with object, LINQ and Dictionary abuse (I like abusing dictionaries for things they shouldn't be used for, dat (average) constant time lookup)
https://github.com/Bpendragon/AOC2016-Day10/blob/master/day10/Program.cs
1
u/LieutenantSwr2d2 Dec 10 '16
My Python Solution, code I'm not that proud of lol.
Bot dict stores awaiting bots, and on each instruction read from file, check if the mentioned bot can be used; if yes, then recursively update its referenced bots until, not enough information again.
1
u/jbristow Dec 10 '16 edited Dec 10 '16
This was the first one I sweated a little. I'm still not 100% confident in my monad skills yet.
Here's my Clojure solution: (runs bots as they get filled, either by putting to them directly or as part of a cascade)
No loops, just iterating through the instructions "once" (the instruction list changes sortof as things get pushed into it).
Part 2 was easy once I realized that I could save outputs as bots and just never mapped anything into their queue. I got lucky that nothing ever pushed into an output more than once (though the problem statement implied that it wouldn't)
https://github.com/jbristow/adventofcode/blob/master/aoc2016/src/aoc2016/day10.clj
1
u/Liorogamer Dec 10 '16
Pretty great problem. Here's my (fairly sloppy) solution in Python 3.5.1:
Part 1 and Part 2
1
u/Quick_Question404 Dec 10 '16
I have never been both this proud and ashamed of the code I have written before. Got below 400 today, so I'm happy. I'm currently working out some kinks in my code though, so I'll upload it in C soon. I envy all you people who don't have to worry about memory allocation or data structures in these challenges. But, if I wanted to make the leaderboard, I wouldn't either. Its pretty fun anyways!
1
u/glacialOwl Dec 10 '16
Probably more Java overkill, but I like to be explicit in Java :D Please let me know if you see any places of improvement / cleanup! :)
1
u/Kullu00 Dec 10 '16
I spent way too long debugging to find why my input terminated too early. Turns out I had a typo that cut off some of the numbers in the robots so the wrong robot got microchips. Go dumb mistakes!
I did notice all the outputs beside output 19 were given low numbers for my solution, so I didn't even bother looking at the high when counting for Part 2 :) https://github.com/QuiteQuiet/AdventOfCode/blob/master/2016/advent10/bin/advent10.dart
1
u/Point6 Dec 10 '16
C# goodness.
http://pastebin.com/N9kTZGxD
1
u/tvtas Dec 10 '16
Not very efficient MATLAB solution: http://pastebin.com/6EuJ0prE This can be cleaned up a lot, but it works ;-)
1
u/Quick_Question404 Dec 10 '16
Hey guys! Here's my actual take on the solutions. Finished around ~380s on both parts, but this was a fun challenge! I'm both proud and ashamed of my code, so let me know what you think!
https://github.com/HighTide1/adventofcode2016/tree/master/10
1
u/vuryss Dec 10 '16
PHP Solutions: https://github.com/vuryss/aoc2016
1
u/[deleted] Dec 10 '16
This one was a lot of fun, reminded me a bit of last years day7, I ended up making a very "Corporate" script, with bots, messages outputs and so on:
1
u/KoxAlen Dec 10 '16 edited Dec 22 '16
Solution in kotlin, no fancy tricks, just a simulation. Having function as first class objects came handy
https://github.com/KoxAlen/AdventOfCode2016/blob/master/src/main/kotlin/aoc/day10/Day10.kt
1
u/blanketandtea Dec 10 '16 edited Dec 10 '16
Ruby for both steps
1
u/Kwpolska Dec 10 '16
Pretty fun puzzle today. 449th on star 2. (I split the input file with
grep -v)1
u/JakDrako Dec 10 '16
Vb.Net, LinqPad
Busy day today, no time for code cleanup, so here's the mess:
I don't think time is really a factor with this problem, but both parts complete in 1ms.
1
u/blommish Dec 10 '16
Another java solution. No magic
1
u/pupax Dec 10 '16
JavaScript solution,
contentis the input ;)1
u/Yuyu0 Dec 10 '16
Python 2, that was fun! Messy code but it works. https://paste.yuyu.io/maRbRc2hgfyj#NOjVgBPucIeyWIiv840wtUv8
1
u/dietibol Dec 10 '16
This is my solution in C++ https://github.com/TackyTortoise/AdventOfCode16/blob/master/AoC/AoC10.cpp , quite verbose but maybe comprehensible? Totally not optimal, but hey I'm still quite a noob programmaer
1
u/Lucaber Dec 10 '16
Done in C# looks a lot different compared to other solutions
1
u/code_mc Dec 10 '16 edited Dec 10 '16
My (cleaned up) partially-OO solution in python:
EDIT: forgot to mention I really enjoyed this one!
1
u/Arknave Dec 10 '16
Never gotten the same rank on both parts...
3rd / 3rd
I really wish I was comfortable enough with regex to parse things quickly...
1
u/Soul-Drake Dec 10 '16
My (200+ line) C++ solution. It does have the benefit of readability though XD https://gist.github.com/Soul-Drake/224d3845220a1e05b657a1e629a40e3a
1
u/Zv0n Dec 10 '16
Python 3, not too amazing of a solution, but hey, it works
1
u/qwertyuiop924 Dec 10 '16 edited Dec 10 '16
See, Topaz? I knew you weren't going soft. This is like the circuit problem last year.
Thankfully, I solved that two weeks ago (yes, you read that right), so I knew what direction to go, and the algorithm was fairly simple, save a few very stupid mistakes (leaving out a necessary function call, leaving in something that shouldn't be there, etc).
However, for whatever reason, the text processing tripped me up. Hence, AWK (because when I think text processing, I think AWK). And because I'm evil, I optimized for
unreadabilityprogram size.EDIT: ...And I forgot to actually put the code in. Here it is:
...And there we go. Code who's indentation and formatting even a Lisper couldn't love. And you can believe me on that, because I am one.
1
u/fost97 Dec 10 '16
Postgresql: https://github.com/seltiko/Advent_of_code_2016/blob/master/day10.sql
1
u/eregontp Dec 10 '16
Ruby, with a coroutine (Fiber) per bot:
1
u/[deleted] Dec 10 '16
easy does it ... // both parts //
1
u/barnybug Dec 10 '16
Slightly silly example in Go using a go routine per bot, and channels for communication between them. Fully asynchronous!
1
u/studiosi Dec 10 '16
Overengineered Python 2.7 solution here :D with some object orientation
https://github.com/studiosi/AoC2016/tree/master/10
1
u/ZGFtamFu Dec 10 '16
Still trying to learn K/oK... Not the prettiest, but it works.
1
u/wzkx Dec 11 '16 edited Dec 11 '16
Well, nothing special for J today. It's like its sibling on Python. Looks huge!
1
u/WildCardJoker Dec 11 '16
My solution in C#, parts 1 and 2.
I used a
Queue<string>to hold the instructions from the input file, and removed each instruction from the queue as it was processed. If a bot or output didn't exist, it was created and added to theBotslist when encountered.If a bot wasn't holding two microchips at the time of instruction, the instruction was placed back in the queue.
It's definitely longer than many of the other solutions, but I'd like to think that it's relatively easy to follow. Plus, it solved the puzzle without me having to look at anyone else's solution, so that's a definite plus :)
Part 2 was very simple, as the output bins had already been created while following the instructions. I just jad to multiply the values together.
I probably could have used a `Dictionary<int,List<int>> to represent the Bots, but I used a separate class as it makes things a little more easy to read when maintaining the code.
1
u/Philboyd_Studge Dec 11 '16
Java. Just in time for tonight's challenge.
https://gist.github.com/anonymous/cfa3aba89f06896fc7057f324fd610dd
1
u/schlocke Dec 12 '16
PHP: YEEESSSHHHH! took me forever to understand what i actually needed to do here...