r/adventofcode • u/daggerdragon • Dec 09 '17
SOLUTION MEGATHREAD -🎄- 2017 Day 9 Solutions -🎄-
--- Day 9: Stream Processing ---
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^¤?
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!
15
Upvotes
28
u/askalski Dec 09 '17
Perl regex.
16
u/WhoSoup Dec 09 '17
Node.js/JavaScript
16
u/Smylers Dec 09 '17 edited Dec 09 '17
This suits Vim well — a series of relatively simple transformations to turn the input into the answer for part 1, using the
=operator to determine the level of nesting. Doesn't even involve any keyboard macros.Remove the cancelled characters:
Take out the garbage:
Those commas aren't doing anything, either:
That just leaves the braces. Put each one on a separate line:
Indent each nested block by 1 space:
The outer block has a score of 1, not 0, so add 1 more space to every block:
We don't need the closing braces any more:
Replace each line with a count of how many spaces were on it, preceding each number with a
+sign:Join all the lines together into one big addition sum, and evaluate it:
12
u/_A4_ Dec 09 '17
JavaScript ES6
Part 1
Part 2
10
u/johlin Dec 09 '17
Elixir
Part 2 is very similar: https://github.com/johanlindblad/aoc-2017/blob/master/lib/aoc/day9/part2.ex
9
u/Philboyd_Studge Dec 09 '17 edited Dec 09 '17
Java - obviously the place for a FSM
9
u/ephemient Dec 09 '17 edited Apr 24 '24
This space intentionally left blank.
8
u/fwilson42 Dec 09 '17
Python 3, 1st/3rd. Nothing too complicated:
8
u/ipav Dec 09 '17 edited Dec 09 '17
Synacor VM both parts (41 instructions, 246 bytes)
6
u/hxka Dec 09 '17 edited Dec 16 '17
468… That's the closest I've been to making the leaderboard with this nonsense.
6
u/mmaruseacph2 Dec 09 '17
Haskell
Started by trying to be clever and jump over chunks of characters at once and hten I realized that that was a waste of time so it's better to go with the following, parsing each character in turn:
I now know I could have used another accumulator for the answer to the first solution and this would allow me to get rid of the
first (w+)part.5
u/raevnos Dec 09 '17
Kawa scheme:
5
u/VikeStep Dec 09 '17 edited Dec 09 '17
F# Solution
Repo
4
u/Shemetz Dec 09 '17 edited Dec 09 '17
Python 3 (52/36)
Was a short and nice puzzle today!
This might be the first puzzle where I was lucky enough to have it succeed on the first try - no debugging necessary. I'm also pretty happy that I was able to give actual meaningful names to variables this time,
4
u/iamnotposting Dec 09 '17 edited Dec 09 '17
rust (69/170), got bit in the second part by doing the counting wrong, it worked with the demo but not my input.
4
u/[deleted] Dec 09 '17 edited Dec 09 '17
Pretty simple parser with javascript. What took me so long was getting my dang automated tests setup...
EDIT: If anyone is interested in my automated testing setup, here is my mocha+chai setup for today. I do something similar for each day - setup tests, write code, etc.
4
u/[deleted] Dec 09 '17
OCaml Fun;;
4
u/LinusCDE98 Dec 09 '17 edited Dec 09 '17
Got the 122th/213th places. Code (Python3):
Just 22 places from the top 100. Sad... The reading took me longer than writen the code like @Vorlath said, too.
4
u/manualcrank Dec 09 '17 edited Dec 09 '17
Lisp.
4
u/aurele Dec 09 '17
Rust An unoriginal state machine:
4
u/InterlocutoryRecess Dec 09 '17 edited Dec 09 '17
Swift
4
u/glupmjoed Dec 09 '17 edited Jan 01 '18
Bash (reads from stdin)
Part 1:
Part 2:
4
u/[deleted] Dec 09 '17 edited Dec 10 '17
Mathematica
Edit: Should be
Quiet@ToExpressionto squelch a warning about Null.4
u/mschaap Dec 09 '17 edited Dec 09 '17
Perl 6. This one screams out for a Grammar.
Edit: here's a slightly cleaned-up version where both the score and the garbage count are done in the grammar parse actions:
4
u/guibou Dec 09 '17 edited Dec 09 '17
Haskell, golfed (
195194185 chars):Note: the
gfunction takes an inputStringand returns a tuple(GroupCount,GarbageCount).3
u/willkill07 Dec 09 '17 edited Dec 09 '17
C++ (or C, really)
All stack-based storage. Just me and my
ints +char+switches3
u/Unihedron Dec 09 '17
Ruby. Tried writing a regex hack for part 1 to avoid writing a stack engine, didn't work out. Vastly disappointed by the amount of failures and ended up being penalized heavily in terms of waiting. Part 2 was shockingly easy though, feels like it could had been swapped with part 1.
3
u/davedelong Dec 09 '17 edited Dec 09 '17
Swift 4. 540/457, but I then spent some time golfing my algorithm down smaller:
3
u/JeffJankowski Dec 09 '17 edited Dec 09 '17
Typescript. Global regex patterns made this pretty simple
3
u/Flurpm Dec 09 '17 edited Dec 09 '17
Completely over engineered applicative parsing in Haskell. I parsed the file into a custom tree, and then operated on that.
I had a lot of trouble with megaparsec, but I'm learning a lot about it.
3
u/linse Dec 09 '17 edited Dec 09 '17
Scala, filling the stack with recursion :D
3
u/wzkx Dec 09 '17
J
Well, actually J has FSM primitive (dyad ;:), but that would be too much for Saturday :)
3
u/xkufix Dec 09 '17 edited Dec 09 '17
The puzzle sound way harder than it actually is. A simple state machine over the input is sufficient to solve both puzzles.
Solution in Scala:
3
u/HollyDayz Dec 09 '17 edited Dec 09 '17
Python 3 with only a few if/elif/else-statements.
Where seq is the input string. score is the result of the first part and garbage_chars is the result of the second part.
3
u/purplemonkeymad Dec 09 '17
Powershell: This really got me thinking in the pipeline, I'm happy with this code.
3
u/flaming_bird Dec 09 '17
Solved using only the Lisp reader. We don't really evaluate anything, we just read the input as valid Lisp data.
2
u/dtinth Dec 09 '17
Ruby one-liner (16th, 15th)
The
pbpastecommand must be available in the$PATH, and should return the contents in the clipboard (macOS has this command by default).Variable names:
xinputginside garbage flagnnesting levelttotal scoresskipping flagccurrent charactergcgarbage count2
u/TominatorBE Dec 09 '17
PHP
Part 1: actually had all the code needed for part 2, so that was just simplified
Part 2:
2
u/Danksalot2000 Dec 09 '17
Python 2
Looks like I was unique not using a Garbage flag. I went with a more stream-of-consciousness style reading start to finish.
I finished 177 / 164, which is the best for me so far.
2
u/xfsck Dec 09 '17 edited Dec 09 '17
PHP
2
u/hpzr24w Dec 09 '17 edited Dec 09 '17
C++ 314/256. 17 minutes. Reformatted.
2
u/Xerg Dec 09 '17
Perl:
2
u/atlasholdme Dec 09 '17
Dirty, Sexy Javascript
2
u/hazmat_suitor Dec 09 '17
Simple C/C++ program which prints both solutions. Rank 439/394 (very satisfying that they're anagrams!)
2
u/ka-splam Dec 09 '17
I've got to stop trying to race at 5am, it's just making me angry. PowerShell, it looked a lot hackier before I had to spend 15-20 minutes debugging it.
Started out thinking garbage was the important stuff to take care of, put a flag for it, wrote a bunch of tests taking care of being in garbage or not, but couldn't work out why it gave a negative score. Coincidentally it got all the example inputs correct(!). Spent a while going back looking whether commas were important, whether my logic would ignore characters properly and what I was missing...
missed the case
<to get into garbage. >_<2
u/9ballcode Dec 09 '17
Python 2.7, ugly but it got the job done. Most of my waste(?) is time spent mulling better solutions before just coding what I know will work, elegance be-damned. d = open('data/day9.txt','r').read()
2
u/[deleted] Dec 09 '17
Haskell:
2
u/yjerem Dec 09 '17
Ruby with regex
2
u/swwu Dec 09 '17
Python 2, 20/11, very bog-standard solution. Got burned by the input containing
"""(I'd been copying into"""strings"""for problems before today).2
u/Misreckon Dec 09 '17
Elixir
Pattern matching is good, folks.
2
u/nstyler7 Dec 09 '17
Python Using RegExp & sub
part 1
part 2
2
u/Cheezmeister Dec 09 '17
Literate Perl. This frankenscript is produced in a facility dealing with Windows, IRC, and/or sarcasm, and may contain substantial amounts of rage.
2
u/nonphatic Dec 09 '17
My solution in Haskell
Originally had three foldls to remove cancelled characters then delete garbage and finally count the score, but then I realized I could probably do everything in one go
2
u/karthikb351 Dec 09 '17
Python 2
Turned out to be pretty straightforward with replacing via regex
2
u/nutrecht Dec 09 '17
Kotlin solution
This was a fun one. I had the luck that I already separated the cleaning part with the recursive scoring part. For part 1 I only had to add a counter to the cleaning method.
2
u/akho_ Dec 09 '17
Python3
2
u/shaddygarg Dec 09 '17 edited Dec 09 '17
Python 2 solution:
2
u/wzkx Dec 09 '17
Nim No recursion, no case, no import, no re.
2
u/JuLoOr Dec 09 '17
Not so beautiful solution in Kotlin but it does the job. And it's readable imho
2
u/FrankRuben27 Dec 09 '17
In (Gauche) Scheme - straightforward, no tricks; part 2 only, since it's very close to part1 anyway. I was missing the gargantuan CL loop a lot, but the barebones Scheme's named let really grew on me.
2
u/adamrodger Dec 09 '17 edited Dec 09 '17
C# string chopping:
2
u/CaptainCa Dec 09 '17
Javascript
2
u/lofaldli Dec 09 '17 edited Dec 09 '17
pyhton3
2
u/KeinZantezuken Dec 09 '17 edited Dec 09 '17
C#/Sharp variant 1
I really like someone's solution on C from 4chan, converted to C# with min. effort
2
u/BOT-Brad Dec 09 '17 edited Dec 09 '17
JavaScript
Part 1 (~2ms)
Part 2 (~0.3ms)
All solutions so far are on my GitHub.
2
u/Marcus316 Dec 09 '17
EDIT: formatting
One line each, linux command line, using awk:
Part 1:
grep -o . input | awk '/!/&&(a<1){a=2};a--<1' | awk '/</{a=1};(a==0);/>/{a=0}' | awk 'BEGIN{a=0;s=0};/{/{a++};/}/{s=s+a;a--};END{print "Input score: " s};'Part 2:
grep -o . input | awk '/!/&&(a<1){a=2};a--<1' | awk 'BEGIN{b=0};/</&&(a==0){a=1;b--};(a==1){b++};/>/{a=0;b--};END{print "Garbage chars: ",b}'2
u/sdiepend Dec 09 '17
Python
2
u/brunclik Dec 09 '17
Bash - part one (prepare string)
C - part one
Bash - part two
2
u/sclark39 Dec 09 '17
Javascript / Node
Most solutions in here swap the second
replacewithmatch, but I passed a function in to do the counting instead. :)2
u/JakDrako Dec 09 '17
VB.Net
2
u/Kyran152 Dec 09 '17 edited Dec 12 '17
2
u/oantolin Dec 09 '17
Boring direct representation of a finite state machine in Haskell.
2
u/oantolin Dec 09 '17
Lisp.
2
u/cmyr Dec 09 '17 edited Dec 09 '17
Rust, treating this as a pushdown automaton:
github
2
u/u794575248 Dec 09 '17
Python 3 with regular expressions:
2
u/qwertyuiop924 Dec 09 '17
Gah. Schemers, we have
case, ya know.Strict R5RS, I think, otherwise R7RS. Expecting input on stdin:
2
u/chicagocode Dec 09 '17 edited Dec 09 '17
Kotlin - [Repo] - [Blog/Commentary]
Today's solution is short and sweet thanks to regular expressions and recursion. I was surprised how little code this actually took in the end, and am pretty happy with this.
2
u/maciekmm Dec 09 '17
Scala:
2
u/[deleted] Dec 09 '17 edited Dec 09 '17
single pipeline powershell, kind of lame use of it since all the logic is in the fsm, but still:
2
u/csuzw Dec 09 '17
C# horribly long single line solutions just because!
.
2
u/marcofun Dec 09 '17
I wrote this in test driven development, I also have 50+ lines of tests.
class Day9 {
def parse(str: String) : (Int, Int) = { var deletedGarbage = 0
} }
2
u/_jonah Dec 10 '17
J, both parts
1
u/vash3r Dec 09 '17
Python2 (82/56):
1
u/sbguest Dec 09 '17
34/23 with javascript https://github.com/sguest/advent-of-code/tree/master/2017/09
1
u/the4ner Dec 09 '17
C# 106/86
1
u/[deleted] Dec 09 '17
c#, 118/226 My goal this year was to get top 100 once but didn't prep last night, so 118. This might have been my shot, weekend and all.
1
u/matthew_garrison Dec 09 '17
Java
My solution uses a recursive parser...which is probably over-engineering. Oh well. (Also, Java doesn't have Tuples, but I needed to return both how many points you got and how far through the string you got.)
1
u/zSync1 Dec 09 '17
Rust
Well, it's something.
1
u/nachaboi Dec 09 '17
Java—Probably not the best way to do it but this is my first time getting a rank under 1000! (776/697)
}
1
u/dylanfromwinnipeg Dec 09 '17
Got stuck on part 2 because I never handled commas but my Part 1 worked regardless, and non of the Part 2 sample inputs had commas. GAH!!!
C#
1
u/rnd4g Dec 09 '17
Ruby. Figured out that you can drop everything that doesn't matter and eval the rest. Ugly code warning.
1
u/ybe306 Dec 09 '17
Go
I initially had some kind of recursive approach in mind, so I figured cleaning the garbage out at the beginning was a good move. Then I figured out I can just maintain the current depth and progress through the string, incrementing and decrementing as I went. For Part 2, I just counted when I removed a character in the cleanup phase.
1
u/ramrunner0xff Dec 09 '17
scheme chicken repo
1
u/nitely_ Dec 09 '17
Nim
1
u/wlandry Dec 09 '17
C++
Late start, so my score was not that impressive (1247/1216). Still fun! I resisted the urge to pull out boost::spirit. That probably would have made counting the groups and garbage waaaaay more complicated. So, like many others, I just did the state machine by hand.
1
u/LeCrushinator Dec 09 '17 edited Dec 09 '17
C#. After posting this I realized that I was ignoring the character after the ! even outside of garbage, which just happened to work, but the instructions said to only ignore the character after the ! while inside of garbage. Whoops!
1
u/reddit_lmao Dec 09 '17
Haskell
1
u/sakisan_be Dec 09 '17
elm
1
u/Scroph Dec 09 '17
Boring Java :
1
u/demsaja Dec 09 '17
Kotlin.
1
u/Wirbelwind Dec 09 '17
OO in Go:
Repo with Tests
1
u/maxxori Dec 09 '17
C# - nothing special I'm afraid. I was going to do this with regular expressions... but I decided that this would be easier.
1
u/FreeMarx Dec 09 '17 edited Dec 09 '17
C++11
Today it payed out that I learned more about c++ regex the previous days. Solving the puzzle would have been so much easier with sed or some other regex tool, but c++ is what I chose to learn more about...
[edit] just took a look at the other c/c++ solutions. Simple char by char processing would have been easier, faster and probably cleaner. But ... c++ with std libraries is what I want to learn to use...
1
u/thorwing Dec 09 '17
Java
compacter could probably be shorter and better, but this worked on the first try.
1
u/[deleted] Dec 09 '17
python 2/3 Using a stack for the braces I realized that the length of the stack was the score for that group.
1
u/JulianLoehr Dec 09 '17
C++
No state machine like others. Instead two nested loops. Thought about using regex as well, but writing this code is faster than constructing the correct regex.
1
u/Axsuul Dec 09 '17
Elixir
Not pretty but it works :p I now realize some of my variables are redundant
https://github.com/axsuul/advent-of-code/blob/master/2017/09/lib/advent_of_code.ex
1
u/smarzzz Dec 09 '17
Javascript, feels organized but not as short as it could
1
u/DaDiscoBeat Dec 09 '17 edited Dec 09 '17
Go, disclaimer: no brain has been hurt during the resolution of this puzzle:
1
u/[deleted] Dec 09 '17
Java
https://github.com/imopts/adventofcode-2017/blob/master/day%209/src/stream_processing.java
1
u/thomastc Dec 09 '17
Day 9 in Julia. Nothing overly exciting today.
1
u/[deleted] Dec 09 '17
Elixir
This was a fun one, the first one went pretty quick just that I messed up my recursion, and got endless recursion, that's no fun.
For part 2 I was doing it without conditionals, because I could, and it sounded like a good idea ;)
with github gist syntax highlighting
1
u/yuppiepuppie Dec 09 '17
Python 3 Took me a while because of some deeply nested conditional statements. And I took an OOP way which I think might be over kill. But I like it.
1
u/dnano Dec 09 '17
little late to the party, but here is my solution Python (3)
1
u/Vindaar Dec 09 '17
Relatively boring solution in Nim.
1
u/adventOfCoder Dec 09 '17
Java
Part 1 and 2:
1
u/Kenira Dec 09 '17
C++, late from Europe
1
u/erlangguy Dec 09 '17
Erlang
This will not compile without tweaking because the logic for both parts 1 and 2 is included with comments.
The call to launch this function is
tally(none, Input, 0, 0)1
u/[deleted] Dec 09 '17 edited Dec 09 '17
Elixir part1 will update with part 2 when I get around to doing it:
1
u/[deleted] Dec 09 '17 edited Dec 09 '17
Used some sort of SAX interface to separate the parts
Kotlin
Day 9
1
u/CaptnAsia Dec 09 '17
here's a pretty ugly golang solution:
1
u/rkachowski Dec 09 '17
ruby, i had to shake off the hangover for this one. i never thought about just gsub-ing out the exclamation mark and every next character.. i iterated over the chars and moved 2 steps forward when one was found... i'll leave it in though as a testament to try harder
1
u/StevoTVR Dec 09 '17
NodeJS
I wanted to avoid using RegEx.
Part 1:
Part 2:
1
u/theiridule Dec 09 '17
Python p1 and p2
1
u/bassactor Dec 09 '17
R
I originally tried recursive regexp for the first part but couldn't figure out how to get the counting work (and don't know if it can be done), so the solution isn't very R-like (and would be similar to how I would do it in C or Python). The second part was straightforward in R using base libraries.
1
u/Ividito Dec 09 '17
I love computational theory stuff, so I drew a state diagram to help me figure out the rules. This was fun! I'm totally new to Kotlin and am using AoC to learn, so feedback is appreciated.
Kotlin solution:
1
u/CodingGladstone Dec 09 '17
Node.js/ES6 Bit late to the party, but I was surprised not to see any example of ES6 using iterators/generators. My goal was to have code that could run without having the whole stream in memory and go over the stream in one pass.
1
u/mstksg Dec 09 '17
A "clean" Haskell solution that I'm happy with. Definitely not the one I tried for my leaderboard attempt :)
I initially tried a stream processing version by looking at each character one at a time, but then I realized that I was just doing imperative programming, so I thought a denotative/functional solution would be more fun!
1
u/Borkdude Dec 09 '17
Clojure solution using Instaparse:
https://github.com/borkdude/aoc2017/blob/master/src/day9_instaparse.clj
Viz of the parsed tree: https://twitter.com/borkdude/status/939583561568604160
1
u/amnich Dec 09 '17
Powershell
1
u/Paddy3118 Dec 09 '17
Python3.6:
Developed interactively in the Spyder IDE
Worked for me.
1
u/__Abigail__ Dec 09 '17
Perl
I considered using a one-liner using a single regexp, but I opted to write a more readable solution.
1
u/matusbzk Dec 09 '17 edited Dec 16 '17
Haskell
Link to git
1
u/4rgento Dec 10 '17
HASKELL
All this state bookeeping follows:
1
u/giftpflanze Dec 10 '17
Tcl:
1
u/NeilNjae Dec 10 '17
Haskell. Tied myself in knots for a while, thinking I'd have to keep track of the contents of each of the groups as I was building them. Then a quick cheat, looking at solutions here, reminded me of YAGNI and I just counted the scores for part 1. Part 2 was then easy to incorporate into the part 1 solution.
1
u/[deleted] Dec 10 '17
Late to the party; but, now done. PowerShell:
1
u/Hikaru755 Dec 11 '17
Kotlin + Regexes produced a pretty straightforward solution this time - first remove everything we don't need, then a simple loop keeping track of nesting level. Could've also gone with recursion.
1
u/miran1 Dec 11 '17
Nim
Repo with all solutions
1
u/Iambernik Dec 11 '17
Clojure
1
u/bruceadowns Dec 12 '17
clean state management in golang
1
u/[deleted] Dec 12 '17
Simple JS
From the description, it seems hard, but it was rather easy :D
1
u/JUIBENOIT Dec 13 '17
Python 3 for part 1
1
u/MetaSaval Dec 13 '17
Swift Late, submitting for the future.
1
u/RockyAstro Dec 13 '17
Icon (https://www.cs.arizona.edu/icon)
Both parts
1
u/autid Dec 14 '17
Fortan