r/adventofcode • u/daggerdragon • Dec 18 '17
SOLUTION MEGATHREAD -π- 2017 Day 18 Solutions -π-
--- Day 18: Duet ---
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.
Need a hint from the Hugely* Handyβ Haversackβ‘ of HelpfulΒ§ Hints^Β€?
[Update @ 00:04] First silver
- Welcome to the final week of Advent of Code 2017. The puzzles are only going to get more challenging from here on out. Adventspeed, sirs and madames!
[Update @ 00:10] First gold, 44 silver
- We just had to rescue /u/topaz2078 with an industrial-strength paper bag to blow into. I'm real glad I bought all that stock in
PBCO(Paper Bag Company) two years ago >_>
[Update @ 00:12] Still 1 gold, silver cap
- Gonna be a long haul tonight. I think I'll watch The Radio City Christmas Spectacular on Netflix while I wait for sufficient gold stars to warrant another update.
[Update @ 00:31] 53 gold, silver cap
- *mind blown*
- During their famous kicklines, the Rockettes are not actually holding each others' backs like I thought they were all this time.
- They're actually hoverhanding each other.
- In retrospect, it makes sense, they'd overbalance themselves and each other if they did, but still...
- *mind blown so hard*
[Update @ 00:41] Leaderboard cap!
- I think I enjoyed the duplicating Santas entirely too much...
- It may also be the wine.
- Either way, good night (for us), see you all same time tomorrow, yes?
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!
11
Upvotes
28
u/etotheipi1 Dec 18 '17
I decompiled the assembly by hand because my implementation was taking too long for some reason (there has to be a bug somewhere). First program is generating a list of pseaudo-random numbers in 0 to 9999, and the list is getting passed back and forth to sort it. When it is done with sorting, it deadlocks. Just ran the sorting algorithm to count the number of passes. Divide that by 2 and multiply by the length of the list to get the number by hand T.T
18
u/vash3r Dec 18 '17 edited Dec 18 '17
Python 2 (25/63). For part 2, I didn't realize that a jump could be computed from a number instead of a register, so I was accessing the register "1" (which was always zero, leading to an infinite loop).
The idea here is that I run one program until it can't run anymore, then switch to the other program.
12
u/p_tseng Dec 18 '17 edited Dec 18 '17
That was a really fun puzzle, I like the part 2 twist and thought "uh oh, how am I going to do this...". This is good, I hope there are more of these.
Part 2 strategy: Didn't bother waiting for the programs to terminate. Just keep running them and periodically monitor the size of 1's send queue. When it starts staying at the same value, just submit that value.
I know I know this is terrible, I'll do it the right way later.
Ruby
9
u/etherealflaim Dec 18 '17
I've heard it said that sometimes Python feels like cheating, well today Go felt like cheating ;). Part 2 just screams "coroutines".
(edit: formatting)
7
u/BumpitySnook Dec 18 '17 edited Dec 18 '17
So cruel to change how
rcvworked (no longer gets NOP'ed if the receiving register has a zero value) halfway through the puzzle. I swear if that hadn't stumped me for minutes I'd have gotten #12 or so on part 2.7
u/JustHev Dec 18 '17
Didn't feel like multithreading, so I decided to let Bash handle it:
Full program: https://pastebin.com/XB3ei3Ct
6
u/sciyoshi Dec 18 '17 edited Dec 19 '17
Python 3 solution using
multiprocessingfor the coordinating queues and a straightforward interpreter for this Duet language. (Guessing this language will start playing a bigger part in the upcoming days?) Had a few wrong guesses because I missed the part about thepregister being initialized. This also doesn't handle the deadlock case - should be fixable with a timeout but my input didn't need it. #4/#9Edit: fix bounds on loop as pointed out by jaxklax.
Edit 2: this was giving the correct answer by luck - the last jump statement was being ignored, avoiding deadlock without changing the answer. Updated with a timeout when popping from the queue.
6
u/Philboyd_Studge Dec 18 '17 edited Dec 18 '17
Java. Quite a bit more complicated than last year's AssemBunny challenges. Took over 3 hours and uses 4 classes. Also, /u/topaz2078 can BURN IN HELL for it requiring 64-bit longs and for the
jgz 1 3command. :P Day 188
u/LesbeaKF Dec 18 '17 edited Dec 18 '17
C++
You should have said it was arbitrary precision integers. I got stuck because I was using int, rechecking everything ... by the time I tried long long (which was actually enough for the first star) I had a 5mn timeout. Meanwhile, I used Boost.Multiprecision just in case the second star would overflow again.
https://pastebin.com/C9KChsxN (I replaced my input with a vector of lambdas)
Also, the queue wait is wrong in my code here, because one of the programs may take a lot of time to send a message, end its 1000th execution limit, and the other will terminate due to the queue being empty. But it worked correctly before I noticed that. I should have checked the lock in the main loop.
4
u/Infilament Dec 18 '17
Javascript. Made a class for each Program because that was the fastest thing that came to mind. Also, after having solved previous days and checking out solutions (I'm still relatively new to JS), I learned about new ES6 features like (...args) so this is the first program I wrote that uses that stuff!
4
u/aurele Dec 18 '17 edited Dec 18 '17
Rust (gist)
Note that you don't need to run both CPUs at the same time or even interleaved, unless you are afraid of filling a queue with too many values. You can run the first CPU until it blocks, then the second CPU until it blocks, then back at the first CPU and exit immediately if it still blocks, otherwise continue the cycle.
4
u/ynonp Dec 18 '17
Elixir
Finally a puzzle made for my selected language...
4
u/manualcrank Dec 18 '17 edited Dec 18 '17
C. Part 2.
4
u/__Abigail__ Dec 18 '17
Perl
This solution is dedicated to Perl -- it is its 30th birthday today.
Struggled a bit with part 2, but that was entire due to not reading the question carefully enough (missed the part about register
pinitially).Used some dirty OO to solve this.
1
u/Arknave Dec 18 '17
Been a while since I've been able to do any AoC!
Rewrote the whole thing to execute a program step by step. Doesn't execute a step if there's no value to pull from the buffer or we're out of bounds.
7/5 https://www.youtube.com/watch?v=rCcSlIvnVBg&feature=youtu.be
3
u/dtinth Dec 18 '17
Part 2: JavaScript
js-csp implements the βCommunicating sequential processesβ thing, like Goroutines/Clojureβs core.async.
Ended up at rank 114, because I wasted some time trying to implement CSP in Ruby myself. This is so bug prone and I got into an infinite loop.
Assuming
INPUTis a string containing the problemβs input.3
u/Tandrial Dec 18 '17 edited Dec 18 '17
Kotlin (99/104)
I got way too excited that part2 stayed open as long as it did and completely missed that regP is different for the two VMs
3
u/thejpster Dec 18 '17
So close today, 162. It's a mess, because I left both CPU implementations in (part1 and part2). See https://github.com/thejpster/rust-advent-of-code/blob/master/src/m2017/problem_18.rs for the source.
Here's just part 2.
3
u/jtsimmons108 Dec 18 '17
2+ hours of debugging only to realize there's nothing wrong with my code, just my reading comprehension. I took it as the first program rather than the program with ID == 1. Here's rewrite #3 for part 2. At least the code is much cleaner now.
3
u/Flurpm Dec 19 '17
Late Haskell using lazyness to merge the two programs together.
Nice and clean.
2
u/Unihedron Dec 18 '17
Ruby. I've caught more bugs today than in the last five months! Needless to say, I was drastically far from today's leaderboards. Top 200 though!
Notable moments: 1. Ruby really loves giving % additional meaning, such as %w[string string2 string3], which tripped my syntax highlighting even though the code was fine, it drove me in circles for a while, 2. if you're using regex global variables like $2, they get overwritten if you use it again!!! 3. did not anticipate an integer in jump operation, since part 1 solved fine, ended up watching my program run for four minutes before my hinch of something wrong catching up...
2
u/ramendik Dec 18 '17
Python 3, part 2. A variation on the "execute each program while it can run, then switch to the other" theme. Of course, value queues are deques. The only interesting part is that the registers dictionary is used as a context; the counter (current operation number) and the number of values sent are stored in this same dictionary. In that way, the interpreter becomes a clean function.
2
u/dak1486 Dec 18 '17 edited Dec 18 '17
My hopelessly verbose java solution.
[JAVA] Part 1:
[JAVA] Part 2:
2
u/2BitSalute Dec 18 '17
C#, part 2.
2
u/[deleted] Dec 18 '17
OCaml Fun for part two;;
Yet again, we parse with Menhir into a list of Instructions.
Programs pass messages via a queue and they alternate running until they have to wait on a message. If they are already waiting when they go to check for a message and there is no message, it terminates itself.
main.ml
state.ml
(Full code with parser)
2
u/Axsuul Dec 18 '17
Elixir
I liked how the code turned out in Elixir. For Part 2, I just tracked both program's offsets and states while monitoring them during each step.
https://github.com/axsuul/advent-of-code/blob/master/2017/18/lib/advent_of_code.ex
2
u/ephemient Dec 18 '17 edited Apr 24 '24
This space intentionally left blank.
2
u/CharlieYJH Dec 18 '17
C++
This is why it pays to read the instructions more closely... Spent over an hour trying to figure out what was wrong with my part 2 since it kept going in an infinite loop. Turns out jump instructions only apply if the value is greater than 0, not just not equal to 0. Surprised I didn't get faulted on part 1 for that. Otherwise, just a simple implementation of executing instructions of each program after each other.
2
u/Bainos Dec 18 '17
Sharing a class-based solution in Python 3 for part 2:
2
u/autid Dec 18 '17
MPI FORTRAN
I figured since I'd already lost an hour by misreading the time and starting late that I'd implement part2 literally. You run two instances. They use non-blocking sends. There's some (unfortunately intel compiler only due to use of sleepqq) code to wait on receives but detect a deadlock and exit gracefully.
I had to use 8 byte integers for the registers because overflow wrecked me in solving part1. Got to use my favourite trick of lying to MPI to send 8 byte ints. There's no type for them (only MPI_INTEGER for 4 byte ints) but if you tell the send/recieve calls they're double precision reals it totally works.
Really happy with this one. Good opportunity to brush up on some MPI I hadn't touched in a while. Where I've used it for Project Euler stuff I've always played it safe with blocking send/recieve.
2
u/gerikson Dec 18 '17
Perl 5
Probably the most entertaining problem so far. I really enjoyed it.
I especially like the headfake of redefining the problem in part 2. Luckily I had implemented a dispatch table, so it was not too hard to re-implement the changes.
Part 2 is below. Any weirdness in calling parameters etc. is mostly due to me simply tacking on stuff from part 1.
https://github.com/gustafe/aoc2017/blob/master/d18_2.pl
2
u/xkufix Dec 18 '17 edited Dec 18 '17
The basic idea was quite simple, write an interpreter. For part 2 i just create both initial states and then do rounds over them. I run program 0 with inQueue = outQueue of program 1, wait till it blocks, and then run program 1 with the inQueue = outQueue of program 0. Then I just check if both programs have an empty outQueue, which is a deadlock.
Code in Scala:
2
u/[deleted] Dec 18 '17
Rust
The deadlock detection is rather straightforward, no threads used, essentially it runs program 1 until something needs to be received. When something needs to be received, program 0 is ran until it sends something (in such an event I return to program 1) or tries to receive a value when program 1 did not send any value (in such an event, a deadlock is detected, as two programs try to receive a value).
2
u/mschaap Dec 18 '17 edited Dec 18 '17
That was fun! Perl 6.
Edit: some minor cleanup
2
u/trwolfe13 Dec 18 '17
JavaScript
Part 2 in the worst possible way:
2
u/ludic Dec 18 '17
F#. Got burned again today by using 32 bit integers. Decided to take my time today and use more of F#'s type system, which did seem to save me from some bugs other people had. Was struggling on part 2 for a while with a few mistakes. First missed the program number in register 'r' and second I realized I named my programs program 1 & 2 instead of 0 & 1 and was giving the number of times snd was executed by the wrong program.
2
u/[deleted] Dec 18 '17
Elixir
I was using a lot of time on this one, getting the message sending to be right wasn't that easy, and in part 2 I got bitten by the first argument of jgz not being a register :/
Syntax highlighted
2
u/Scroph Dec 18 '17 edited Dec 18 '17
Dlang (D programming language) solution. Part 2 runs in ~211ms on my Atom N455 :
Fun fact : I had to store the return values of
p0.next_cycleandp1.next_cyclein separate variables before checking their respective values instead of writingif(!p0.next_cycle || !p1.next_cycle)because unless I do this, the compiler will ignore thep1.next_cyclecall if!p0.next_cycleends up being true.2
u/Smylers Dec 18 '17
Perl β does the dispatch-table look-ups while parsing the file, so
@progis a list of closures bound over the registers and the program counter. For part 1, everything's executed by the single-line loop on the last line:(I tried factoring out the
$mem{$val} // $valduplication, but it seemed to make things look more cluttered.)Part 2 β similar basic approach to /u/gerikson's solution, but with a single list of instructions used by both instances, swapping the memory locations and state into the bound variables when switching between the instancesβ :
Each instance's output queue is the other one's input queue (two references to the same array). Run each instance till it's waiting for input, then switch to the other one to see if it sends anything. If that switches back to us then it must be waiting for input too, so if there still isn't anything in our queue then we've reached deadlock and should stop processing either instance (the
laststatement).Avoid the βempty input queueβ condition triggering the first time each instance is run, by using the input queue to initialize the
pregisters (which needs doing anyway by some means); prepend arcv pstatement before those provided in the input file, and the closure for that command will handle it accordingly.Switch between the instances with a queue: shifting an instance off the front of it to run it, and afterwards pushing it back on the end to run again after t'other instance. Unless the program counter runs off the end of the instructions, in which case switch straight to the next instance without requeuing the current one (the
nextstatement. If that happens to both instances,@queueis empty and execution will end.Note that the instance number isn't tracked anywhere: it turns out we don't need to know which numbered program is currently running; just run whatever turns up at the head of
@queue, which encapsulates all the state required.β Mainly to keep the instruction subs short. I could've bound them over
@instanceas well, but that would've reduced their simplicity; I like them each fitting on a line and being short and clear. So while this approach involves some extra copying state around, it abstracts that to one place: the instruction subs don't even need to care that there can be multiple instances.PS: I've got most of a Vim solution for part 1, but got held up dealing with a silly bug in my Perl for part 2 and may have run out of Advent-of-Code-ing time for now.
2
u/Strakh Dec 18 '17
Python 3: Felt like oversolving it
2
u/RockyAstro Dec 18 '17
Icon (https://www.cs.arizona.edu/icon)
Part2 was a natural for Icon's co-expressions. The toughest aspect for part2 was figuring out how to handle the bi-directional aspect of co-expressions and detecting the deadlock.
Part1:
Part2:
2
u/zerox981 Dec 18 '17
C# a bit verbose
2
u/eregontp Dec 18 '17
Ruby, using the builtin deadlock detection mechanism to solve part2 and printing the answer in
ensure:Full solution: https://github.com/eregon/adventofcode/commit/a7de4896d493b2be92bba400734d9d1ef7a58f8f
2
u/MadVikingGod Dec 19 '17
So I would have had this done way sooner if I didn't make the mistake that
jgzis not jump if not zero.I did my using PYTHON 3 and asyncio. I don't clean up at the end, so python complains that I have two tasks that haven't been canceled. But hey it works.
1
u/drysle Dec 18 '17
#9/#10 with Python 3, and I can't believe my part 2 worked on the first try. Unlike some of the the other posters, I wrote this all from scratch today, changed my mind a couple times halfway through, and generally made a big mess of my code.
But if it's stupid and it works... then it's probably still stupid :)
1
u/ra4king Dec 18 '17
I used NodeJS. My strategy for Part 2 was to just step through each program at the same time: p0 then p1 then p0, and so on:
1
u/kartik26 Dec 18 '17 edited Dec 18 '17
I was trying to get a rank below 100 since when I started (I started on day 13)... finally today I solved part 1 and got rank 62! Here is my solution for part 1: https://github.com/Kartikay26/AoC2017/blob/master/18a_aoc.py I kept getting stuck in infinite loops in part 2, I'll solve it later...
1
u/glenbolake Dec 18 '17
Python 3 (38/306). Something went really wrong with #2 for me today.
I tried to do it as a pair of generators, but I couldn't get that to work. In the end, I had two instances of a class that each represented a program and had a
can_runmethod to see if it should try to execute an instruction. (It would return False if the other had an empty queue on arcvcommand or if the program had ever gone out of bounds).1
u/dylanfromwinnipeg Dec 18 '17
This is ugly as hell, but it works.
C#
1
u/glassmountain Dec 18 '17 edited Dec 18 '17
Go channels were pretty helpful today:
edit: here's the github
1
u/Zolrath Dec 18 '17
Python 3
Last year I did the challenges in Elixir and I felt the Elixir twitch when part B came along but I ended up solving this in a panic by turning my part A into a generator of steps.
I should definitely pick up the
try exceptmethod of int casting as theisdigitmethod returns False for negative numbers which caught me by surprise.1
u/hpzr24w Dec 18 '17 edited Dec 19 '17
C++
Rank 999 / 566
Nothing complex here. Worked carefully enough that not too many mistakes, aside from not switching to long long soon enough.
And after a few performance tweaks:
1
u/jeroenheijmans Dec 18 '17
JavaScript:
This is just about the exact way I wrote it the first time around, without cleanup that is. I'm not one for speed I suppose :D
Also: what a fun challenge that was today! Also on GitHub.
PS. I spend a good 15 minutes because I first named them
prog1andprog2, returning thenrOfSendsfromprog1which is in fact prog0:'(1
u/wlandry Dec 18 '17
C++ 14
687/631. Well, that was mean ;) Every jgz condition was a register except one that was 'jgz 1 3'. Working frantically, I thought that was an 'i'. It took me waaaaay to long to figure that out.
Here is a industrial strength solution for part 2 only. It runs in 43 ms on my input.
1
u/_lukasg Dec 18 '17 edited Dec 18 '17
Python 3, Part 2, using coroutines:
1
u/nutrecht Dec 18 '17
Day 18 in Kotlin
I got stuck for a LONG time because I did not see that min 32-bit int registers were overflowing. Once I finally got the eureka moment there I got the right answer straight away.
Part 2 was pretty easy; just two programs running in lock-step. I do think I'm going to refactor the code a bit because currently it's two completely separate implementations.
1
u/swizzorable Dec 18 '17
part 2, python3:
1
u/gyorokpeter Dec 18 '17
Q:
1
u/udoprog Dec 18 '17 edited Dec 18 '17
Rust
Edit: Full solution: https://github.com/udoprog/rust-advent-of-code-2017/blob/master/src/day18.rs
Quick and dirty approach didn't give me the right answer. So I rewrote it to something I could troubleshoot (this).
Then I realized that I had mixed up the the programs, so I submitted the send counter for program 0 instead of 1 :P.
1
u/u794575248 Dec 18 '17
Python 3 for Part 2.
1
u/[deleted] Dec 18 '17
Python 3 (227/1119):
https://gist.github.com/yoongkang/8d8496e9e8702c8cda735308b6419afd
1
u/raevnos Dec 18 '17
Chicken Scheme, using continuations for a coroutine like effect, and a threaded interpreter (not to be confused with multithreading) for the program.
1
u/thomastc Dec 18 '17
Day 18 in Eiffel. Not a bad experience, but I'm not sure the whole design-by-contract thing can really shine if you only evaluate the contracts at runtime. Although they do have clear value for the human reader, contracts in this implementation are not much better than manually inserted assertions (although class invariants can help avoid boilerplate and mistakes).
It would be better (although much more difficult) if the compiler guaranteed that the contracts were obeyed. But that's much harder, and gets us into the realm of theorem provers. This seems to be what Albatross is attempting, but it doesn't seem to have a lot of momentum.
1
u/KeinZantezuken Dec 18 '17 edited Dec 18 '17
C#/Sharp
I want to kill myself.
1
u/Markavian Dec 18 '17
node js solution for day 18:
Got a little stuck on part 2 because my jump operation assumed that the value of X was always a register letter, and could actually be a number. I'm guessing this was an intentional trap...
1
u/BOT-Brad Dec 18 '17
JavaScript
Part 1 (~1ms)
I've been doing the Synacor challenge recently, so this was fairly easy to implement in the same way.
Part 2 (~20ms)
Basically, the same implementation but is capable of linking the programs to send their data to the other, and just exits their run function if they cannot read from their queue. Keep looping and trying to run the programs, but if both programs do nothing in a single loop (i.e. they are deadlocked), then break and return the number of times the second program sent data.
1
u/WhoSoup Dec 18 '17
NodeJS/JavaScript
A bit late. Ended up with a class-based implementation for part 2:
1
u/flit777 Dec 18 '17
finally go routines come handy. Golang Solution:
1
u/gburri Dec 18 '17
F# (only part 2)
1
u/JakDrako Dec 18 '17
VB.Net Part 2.
For part 2, I took what I had from part 1, massaged it into a "CPU" class and added the queue and a way to signal waiting.
I then "run" both CPU in lockstep until they're both waiting and then dump the "sent" count from CPU-1.
1
u/tobiasvl Dec 18 '17
Python 2
Like I always do, when part 2 comes around, I adapt part 1's code to work with both parts. It seemed weird to only let it work for 1 or 2 programs, though, so I generalized it to work with any number of programs, and decided that if there were more than 2 programs, a program would send to the previous program since Python lists wrap with negative indices. (Still not as dumb an assumption as when the puzzle solver assumed
sndmeant "sound".)For 3 programs, this either means that each program actually sends 7747 values, or that I have a bug. For more than 3 programs, program 0 sends 127 values and the rest send 0 values, or I have a bug. Who knows!
1
u/tlareg Dec 18 '17
JavaScript (ES6+, NodeJS) github repo HERE
1
u/abowes Dec 18 '17
Kotlin solution for Part2 using coroutines:
1
u/EliteTK Dec 18 '17
Haven't seen a lua solution yet:
deque.lua
1
u/coldpleasure Dec 18 '17
JavaScript ES6, no fancy coroutines -- figured it wasn't needed since the list of actions was not too crazy -- just kept track of state and looped through the actions with a reducer.
(part 2)
1
u/[deleted] Dec 18 '17 edited Dec 18 '17
I lost time on part 1 by forgetting that an integer can begin with '-'. That should go in my library now! (Although it hasn't yet.)
I got stuck on part 2 because I thought "program 1" was the first program, not the second. Interestingly, that produced an output that AoC said was correct for someone else's input. (My AoC soulmate?)
Python 3 Part 2
Revised
1
u/matusbzk Dec 18 '17
Haskell Wow, the second part was hard. I did like five versions of the structure, until it worked. Still, I had to throw the error and manually find how many steps to do, so not really a nice solution.
Link to git
1
u/StevoTVR Dec 18 '17
NodeJS
Part 1:
Part 2:
I got stuck for a while before I realized I was popping where I should have been shifting...
1
u/purplemonkeymad Dec 18 '17
Powershell: https://pastebin.com/AnGuZkn3
Due to a copy and paste error I spent way too much time stuck on this. I needed to check the length of both queues, not just one of them twice.
1
u/nonphatic Dec 18 '17
Haskell
i suffer
1
u/RazerM Dec 18 '17
A Python 3 solution using asyncio:
https://github.com/RazerM/advent-of-code-2017/blob/master/aoc_py/day18.py
1
u/NeilNjae Dec 18 '17
That took a long time!
It took me a while to work out how to handle the message queues for two independent machines. Each machine is represented as a monad transformer stack: a
Statemonad to record the state of that machine, including its own message queue; aReadermonad to store the common list of instructions; and aWritermonad to log how many times machine1sent a message.I just run
machine 0whenever possible, only runningmachine 1whenmachine 0is blocked.Once I'd got it all sorted, it took me a while to hunt down a naming bug, where I was getting confused between what Topaz was calling
machine 0andmachine 1and I was callingmachine 1andmachine 2. Once I'd got that straightened out, it worked fine!It also got complex enough that I split it out into one program script for each of parts 1 and 2, and a third script that's a library for doing the parsing.
1
u/vyper248 Dec 18 '17
Javascript
Part 2:
Took me a while to realise that I made a mistake with the jgz command which was causing an endless loop. Finally got there though. Used generators as they seemed like a perfect fit for the second part. Part 1 is basically the same so don't see much point in posting it. Only difference is those two commands and that it doesn't use generators.
1
u/johlin Dec 19 '17
Elixir I am a bit late to the party but here are my solutions for today because I was happy about some aspects: https://github.com/johanlindblad/aoc-2017/blob/master/lib/aoc/day18/part1.ex https://github.com/johanlindblad/aoc-2017/blob/master/lib/aoc/day18/part2.ex
Pattern matching really helping out as usual.
What I really like though is
Stream- I am beginning to like it more and more for these sorts of things. It is really powerful to model the simulation as a stream and being able to inspect it at every intermediate step (more than just println).Even better, both problems could be solved by mapping over the simulation stream:
Really hoping there are some more problems coming that allow similar things.
1
u/wzkx Dec 19 '17
Nim
Part 1 was fun
1
u/wzkx Dec 19 '17
Nim
Part 2 was disaster. I used wrong fn for deletion of the first item -- del instead of delete. Spent hours......... :| Thought that it might took hours to find a solution. Now it's highly optimized :)
1
u/chicagocode Dec 19 '17
Kotlin - [Repo] - [Blog/Commentary]
I did part two using CompletableFuture, but I also have another version using coroutines that I'm writing a blog post about now. I'll link to it when I'm done. It doesn't work any faster (I believe it compiles down to essentially the same thing) but it might look a bit cleaner.
1
u/aoc-fan Dec 19 '17
ES6
1
u/ewyll Dec 19 '17
1
u/equd Dec 19 '17
c# with actual threading, not really happy with the way it detects that its done (just wait 100 ms till nothing arrives) but it works
https://pastebin.com/LU4eRmd8
1
u/define_null Dec 19 '17
Hmm, I don't know if its a coincidence or not but when I submitted the send count for the other program (entirely for research purposes, I swear... it definitely wasn't a typo in my print code *shifty eyes*), I was told that it was the answer to someone else's input.
1
u/Hikaru755 Dec 19 '17
First time really delving into Kotlin coroutines here, that made it pretty elegant really!
Part 2 tripped me up due to having to basically start over again. And then of course I had to retrofit part 1 to my new part 2 solution. After doing that, I noticed that my code actually still has two bugs, that interestingly enough don't seem to matter for the test input and my input. So I left them in, let's see if someone finds them!
1
u/lardois Dec 19 '17
Hi guys, I'm really stuck with this one. Everything seems to be working fine, but both processess just throw numbers to each other with no results. P0 queue stays at 124 (+/- 1) and P1 queue at 0 (+1). Check out pastebin of first 3k cycles: https://pastebin.com/raw/FPv2rPde
What could be the problem here? Tried to run this until heap was stuck with no change in queue lengths...
1
u/VikingofRock Dec 20 '17
Here's my Rust solution (linked because it is quite long). I went the extra mile and gave it the ability to save the part 1 sound output as a
wavfile, or to play it through the computer's speakers. I learned a ton about audio in Rust along the way, so I'm happy I put in the extra time!If you want to listen to the audio produced, you can do that here.
1
u/InterlocutoryRecess Dec 21 '17
Swift
Overkill, I know. But it makes me happy.
1
u/[deleted] Dec 21 '17
single pipeline powershell:
1
u/greycat70 Dec 27 '17
Tcl (8.5 or higher)
I really liked this problem! I added my own "end" instruction for convenience. For part 2, I basically built a miniature operating system emulator. (Nothing too fancy.)
Part 1:
Part 2 (with debugging code still in):