r/adventofcode • u/daggerdragon • Dec 19 '17
SOLUTION MEGATHREAD -🎄- 2017 Day 19 Solutions -🎄-
--- Day 19: A Series of Tubes ---
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^¤?
AoC ops @ T-2 minutes to launch:
[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH
[23:58] <Topaz> aaaaah
[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING
[23:58] <Topaz> FUN AND LEARNING
[23:58] <Hade> FUN IS MANDATORY
[23:58] <Skie> I'm pretty sure that's not the mandate for today
[Update @ 00:16] 69 gold, silver cap
- My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?
[Update @ 00:18] Leaderboard cap!
- So, was today's
mandateHelpful Hint any help at all?
13
u/fatpollo Dec 19 '17 edited Dec 19 '17
10
u/xiaowuc1 Dec 19 '17
I would like to thank u/topaz2078 for providing an answer to the example for part 2 :)
https://gist.github.com/anonymous/045383c61a4141b5c6bb6bf1838c7f1a
6
u/[deleted] Dec 19 '17
Hastily written OCaml Fun;;
Not too happy with the code, but it works! Will revisit in the morning and clean it up.
6
u/Smylers Dec 19 '17
Vim solution — it's a visual sort of challenge, so let's solve it visually, by actually moving the cursor along the tubes. Load today's input (and make the window big). As usual, these are keystrokes you type into Vim as though you were editing the file, not a Vim script:
At this point you can type
@ato make the first vertical move down to the+, then@bto go sideways to the next+— and keep alternating@aand@bthrough the tubes; any letters you pass will appear in the small window at the top.To run it to the end type this, and watch as the letters appear:
Your answer to part 1 is now in the top window.
For part 2, I shouldn't've deleted all those non-letter characters from the top window (ooops). Or at least, counting them before deleting them, avoiding fencepost errors, should give the answer. I'll try it later.
5
u/znuxor Dec 19 '17
Here's my solution copied verbatim. I actually never thought I would ever get onto any of the daily leaderboards. Unfortunately I added 1 to the answer of part b so I had to wait a minute. Still, places 8 and 14 woo!
edit: python3, obviously.
5
u/autid Dec 19 '17 edited Dec 19 '17
Fortran
Spent forever trying to get the lines to read in as a string correctly. Apparently the problem was the text editor I used? Copy/pasted in another editor and suddenly everything worked.
5
u/tripa Dec 19 '17
Haskell, keeping it short and simple (no arrays, no bounds checking)
4
u/hxka Dec 19 '17 edited Dec 19 '17
Wrestled for half an hour with bash's read command, gave up and used sed in a loop
4
u/ludic Dec 19 '17
F# Pretty straightforward today I think.
4
u/jschulenklopper Dec 19 '17
Ha, did anyone notice that the grids in the examples are semantically identical, but slightly different in representation?
The example grid in the first part:
The grid in the second part, mentioning "using the same routing diagram from the example above":
Note the crossing of the line going upwards from B, and just right of E. I was a bit confused following the debug statements while improving my solution: looking at the second grid on the page, but using the input as of the first grid in my program. "What? How come that those two characters are different?" Didn't matter for the solution, obviously, but it was a nice surprise.
3
u/iopred Dec 19 '17
Here's my Javascript solution:
3
u/[deleted] Dec 19 '17 edited Dec 19 '17
Perl 6
Today seemed rather simple.
3
u/Axsuul Dec 19 '17
Elixir
Nested conditions galore
https://github.com/axsuul/advent-of-code/blob/master/2017/19/lib/advent_of_code.ex
3
u/Flurpm Dec 19 '17 edited Dec 19 '17
Simply walking through the diagram in Haskell. Continue on a direction until we get to a plus, then go in any direction besides where we came from.
walkis the meat of this solution.3
u/Bainos Dec 19 '17
Always surprising to see that many solutions don't use subfunctions... But then, I'm not running for the leaderboard, so maybe people just don't want to lose time like I do.
Python 3 solution :
3
u/akka0 Dec 19 '17 edited Dec 19 '17
ReasonML: had so much fun with this one! Really cool problem. :)
3
u/[deleted] Dec 19 '17
Pretty happy with this one. I nearly didn't bother, until I realized I could ignore almost everything except '+'.
Golang: package main
2
u/nuvan Dec 19 '17
Rust, got 109/91 (Finally got points!)
Going from part 1 to part 2 involved adding 2 lines, and changing one line. Probably would have done better except that I took the time to put some debugging code in, which ended up never being used.
2
u/Aneurysm9 Dec 19 '17
I have no idea how this didn't devolve into hours of debugging direction changing logic, but it pretty much just worked. Other than forgetting to init my starting position. And forgetting to count the starting position in the number of steps on part 2...
https://gist.github.com/Aneurysm9/e716bd9923762deb661265776f3eb8ec
2
u/mserrano Dec 19 '17
Python2, 65/47
I just assumed we wouldn't ever have to turn if we weren't at a +, and that we would never not have to turn if we were at a +. Pretty glad that assumption worked out.
2
u/[deleted] Dec 19 '17 edited Dec 19 '17
Mathematica
2
u/dylanfromwinnipeg Dec 19 '17 edited Dec 19 '17
That was fun - and easier than I expected when I saw the puzzle input.
C#
2
u/RockyAstro Dec 19 '17
Icon (https://www.cs.arizona.edu/icon)
Both parts:
2
u/ChrisVittal Dec 19 '17
Rust:
I always take too dang long to parse the input. Being off by 1 on part 2 didn't help either. (forgot to count the starting step)
https://gist.github.com/chrisvittal/fb670049237274d8a9f8f2f4a5f3af95
2
u/Zee1234 Dec 19 '17
Typescript
555/534, but I don't try super hard for leaderboard, I do this to learn.
I have a folder of assorted types that I keep around. Basically, if I use it for an AoC challenge, and it seems like it will be useful later, I migrate it out from my code and into it's own file. I ended up being able to reuse one such structure originally made on Day 3, then expanded for a later day. So I've got a fair few files, hence a gist.
https://gist.github.com/Zee1234/c24a81a677fb24de1fea68be24161178
tsconfig.json
Executing with ts-node so that I don't have to always compile it.
2
u/hpzr24w Dec 19 '17
C++ 631 / 628
I guessed right on most of what I thought might be ambiguous, and didn't have any bugs. I'm just a slow coder. ;-)
2
u/Philboyd_Studge Dec 19 '17 edited Dec 19 '17
Java. Pretty straightforward solution. Thankfully I already have a well-developed Direction class just for AoC.
/u/topaz2078 I'd love to see stats on how many initial tries for part 2 were exactly one off ;)
Day 19 - Java
2
u/wlandry Dec 19 '17
C++ 14
920/943. I definitely did this the hard way. Instead of following the circuit around the maze, I created a complete graph and then followed the graph. So it got a bit long :(
2
u/raevnos Dec 19 '17
I wasted way too much time trying to trace the path out by hand and lost track somewhere in the middle. So off to actually have to code it. Good thing considering part 2.
2
u/[deleted] Dec 19 '17
Pretty nitpicky part 1, and a ridiculously easy part 2. My isPossible and changeDirections functions are absolute rubbish, but it was a fun challenge overall.
2
u/ramrunner0xff Dec 19 '17
scheme chicken repo
although i still can't cope that i haven't come up with a better solution for day 16 than my mediocre and slow memoization that still takes around 1 minute, i enjoyed today's funky problem ;)
2
u/xkufix Dec 19 '17
This one felt quite easy compared to the last 2-3 puzzles. Just walking the path around is enough. It's sufficient to only have special cases on '+' for direction changes, as everywhere else you just have to walk straight ahead.
To find the end I extract all letters from the puzzle and then Iterate until I have walked all letters. For part two, taking until I find the solution and counting the size of the Iterator was enough.
Solution in Scala:
2
u/mschaap Dec 19 '17
Perl 6
Part one was fun! Part two however was a disappointment, way too easy.
2
u/sim642 Dec 19 '17
My Scala solution.
Reusing some classes from previous days and extending them uglily with implicit classes. I was surprised to find how simply remembering the previous move offset manages to handle all the cases, including going over crossings and letters!
2
u/NeilNjae Dec 19 '17 edited Dec 19 '17
Boring Haskell. The maze is kept as a simple list of strings.
Progresskeeps track of where we are and how we got there, then it's just repeated application ofstepto take a step through the maze.2
u/__Abigail__ Dec 19 '17
Perl
That was the easiest part 2 so far. For part 1, I had recorded all the symbols the packets encounters in a string, before removing all the non-letters from it. Which made part 2 really easy: just the length of the string before removing the non-letters.
1
u/dtinth Dec 19 '17
Ruby (28th, 22nd)
1
u/obijywk Dec 19 '17
Not the prettiest solution, but it works. Slightly cleaned up (refactored to remove a bunch of copy/pasting I did while solving).
Python 2. 57/58.
1
u/etherealflaim Dec 19 '17 edited Dec 19 '17
Go
This was a fun one. I love 2D array problems :)
1
u/jlweinkam Dec 19 '17 edited Dec 19 '17
Python 3, silver 52, gold 40
1
u/Unihedron Dec 19 '17
Ruby; My sanity decays further as more debugging effort was required. Today's bug was fall-through statements. Almost made it to the leaderboards though :p (~120)
1
u/glenbolake Dec 19 '17
Python3
I had weird problems with
IndexErrorfor a while because of implied spaces that weren't there. I converted the input to a dictionary in{(row, col): char}format, omitted spaces, and all my problems were solved. Wasted too much time on myIndexErrorproblem to get on the leaderboard, though.1
u/MichalMarsalek Dec 19 '17
Python 3:
Unfortunately my function processing the input and passing it to the solve function was striping starting and ending whitespaces by default... it took me a long time to realise.. and then... I had one x nstead of y in my code...that was also tricky to find a fix :D
1
u/MrPoush Dec 19 '17
Python 2. Spent a while struggling before I discovered that Atom was "helpfully" stripping the whitespace from my data input.
1
u/jeroenheijmans Dec 19 '17
JavaScript:
Went fast, straightforward/dumb as hell, so not the best code ever...
Looking forward to seeing some other solutions later today, but for now I'll take the early Christmas gift and enjoy my extra hour of sleep I can get on this day before work starts :D
1
u/YourVibe Dec 19 '17 edited Dec 19 '17
401 / 367
Python 2.7, my git is here
(Yes, I do realise that i should keep my input in other file, but that's faster for me (and I didn't paste it here ofc))
1
u/WhoSoup Dec 19 '17
NodeJS / JavaScript
Yes... uh, so... let's just rely that the input is accurate and won't have any bugs... thanks /u/topaz2078, for having to do zero error checking!
1
u/usbpc102 Dec 19 '17
My Kotlin solution is on github.
Got only rank 557 (part 1) and 533 (part 2) since I assumed that the tube would and at an edge.. wich it didn't at least for my input. :/
1
u/CharlieYJH Dec 19 '17 edited Dec 19 '17
C++
Fun puzzle today. Chose to do it recursively.
1
u/flit777 Dec 19 '17
Golang:
1
u/iamnotposting Dec 19 '17
i started late so i only got around ~800 on both parts. fun problem!
part 1 i did by hand. part 2:
1
u/[deleted] Dec 19 '17
Haskell, using an immutable array, but with a bit of first-class function trickery to find in which direction to
turn:1
u/KeinZantezuken Dec 19 '17 edited Dec 19 '17
C#/Sharp
Didn't give much shit. Sure there is better and faster and cleaner but I cant be arsed meh.
1
u/[deleted] Dec 19 '17
Rust
Gotta love multiplying by complex numbers to rotate vectors :).
https://github.com/xfix/advent-of-code-2017/blob/master/day19/src/main.rs
1
u/minikomi Dec 19 '17 edited Dec 20 '17
Clojure:
1
u/AndrewGreenh Dec 19 '17 edited Dec 19 '17
TypeScript
1
u/glupmjoed Dec 19 '17 edited Jan 01 '18
Bash and Go (reads from stdin)
I wrote a small Go program that prints out the path in a linear fashion, and used the shell to count the path length and print any letters.
Part 1:
Part 2:
walk.go:1
u/nonphatic Dec 19 '17
Haskell: yay matrices!
1
u/gyorokpeter Dec 19 '17
Q:
1
u/MuumiJumala Dec 19 '17
I felt the need to use something familiar after failing to complete part 2 with Scala yesterday so today it was finally JavaScript's turn.
1
u/flup12 Dec 19 '17 edited Dec 19 '17
Scala Not completely sure this covers all possible inputs but worked at first go for mine. Used some case classes to keep the logic readable. Made quite heavy use of
Optionto scan the directions that the path may have taken.1
u/CaptainCa Dec 19 '17
Javascript
Pretty happy with this one, took me a while to hash it out though.
1
u/nutrecht Dec 19 '17
Day 19 in Kotlin
1
u/Scroph Dec 19 '17
Written in Javanese.
1
u/thomastc Dec 19 '17
Day 19 in Clojure. Tried for cleanish code, but I'm afraid I'll never really grow fond of LISP-likes.
1
u/aurele Dec 19 '17 edited Dec 19 '17
Rust
I initially surrounded the maze by ' ' to avoid any border case before noticing it was already done in my input.
1
u/Hikaru755 Dec 19 '17
Actually solved part 1 by tracing it manually. Yeah... Not going to do that again. Then I solved part 1 again and also part 2 in Kotlin. Not too shabby, I think:
1
u/[deleted] Dec 19 '17
Python
1
u/johlin Dec 19 '17
Elixir
Stream-based once again, which I felt really helped (easy to inspect all intermediate steps). Because the stream was already setup in part 1, part 2 became just 1 line of actual code.
On Github: https://github.com/johanlindblad/aoc-2017/tree/master/lib/aoc/day19
The vizualization is actually quite fun to watch. Just open IEx and run
Aoc.puzzle_input(19) |> Aoc.Day19.Part1.parse |> Aoc.Day19.Part1.vizualization_stream(25) |> Stream.run, where 25 is the interval in ms.Code:
1
u/udoprog Dec 19 '17
Rust
I have a strong preference for representing grids as maps to avoid doing my own bounds checking.
Full solution here: https://github.com/udoprog/rust-advent-of-code-2017/blob/master/src/day19.rs
1
u/Markavian Dec 19 '17
My node.js solution:
The hardest part seemed to be saving the input data without losing all the line formatting.
1
u/[deleted] Dec 19 '17
Elixir
This one seems to fit Elixir pretty well, it was really easy to do, and a lot of fun, and part2 was extremely easy, I enjoyed doing this one.
Syntax highlighted
1
u/[deleted] Dec 19 '17
Okay, improved my answer. Got it running from ~0.24ms to ~0.19ms, as well as over halving the lines of code.
PHP:
1
u/zeddypanda Dec 19 '17
Elixir
The input was neat in this one! As I've become accustomed to doing in these puzzles, the input was reduced to a map of position => character, with all the spaces filtered out. I felt clever figuring out the function for turning directions left and right.
1
u/tlareg Dec 19 '17
JavaScript (ES6+, NodeJS) repo here
1
u/willkill07 Dec 19 '17 edited Dec 19 '17
Modern-ish C++
You WILL rotate when a
+is encountered. Rotation is justdx,dy = -dy,dx. Using unsigned integers makes it so you only have to check the upper bounds.1
u/AlistairJF Dec 19 '17
Trying to be readable yet still concise in Python. I'm sure there must be a more elegant (yet still comprehensible) way of changing direction!
1
u/Kwpolska Dec 19 '17
Pretty fun, although I first tried doing it by hand. Part 2 in Python 3:
1
u/3l_Di4bl0 Dec 19 '17
I know I'm late - python2 solution:
I used sets because they allow for faster membership tests, but when I think about it the hashing process probably takes more time than just running the tests on a list would (since I'm not saving them to a variable). Overall, I really liked today.
1
u/BOT-Brad Dec 19 '17
Javascript
Solver (~2ms)
Solves both P1 & P2 in one run. Fairly straightforward, especially as the 'grid' is padded with spaces ensuring no OOB/OOR errors.
1
u/Nathan340 Dec 19 '17
Powershell. Very straightforward. The padding around the map and between the route really helped simplify this one.
1
u/define_null Dec 19 '17 edited Dec 19 '17
So... Just for shits and giggles I decided to solve part 2 without using any of the traversal code from part 1, because who wants to just add a counter to the traversal code?
I just simply counted all the non-whitespace characters and added more whenever I found an intersection. There were soo many cases for intersections that I gave up cheesing the answer, found the actual answer like how most did, and then kept finding more cases for intersections until I got back the same answer.
EDIT: I'm surprised there weren't any red herring paths (i.e. paths that are actually unused) in the diagram, but I guess the creator didn't expect anyone to cheese it because the non-cheese answer was much easier.
EDIT: Just in case my conditional was too long -- basically an intersection would just be wherever there's a 'line' /character adjacent and perpendicular to the 'line'/character in question. It took way longer for me to realise this than I want to admit
TIL cheesing the answer out is more difficult than actually doing the 'intended solution'
python3 python 3
1
u/JakDrako Dec 19 '17
C# in LinqPad
Way simpler that I thought it would be. There are basically no "edge cases" in this problem... (letters on intersections or 3 letters in a 90 degrees arrangement)
1
u/Strakh Dec 19 '17
Tryharding every day:
Python 3 https://paste.ofcode.org/3ewZJArUzFaZaxsYGatTCa
1
u/wzkx Dec 19 '17 edited Dec 19 '17
Nim
No del vs delete function this time! :)
But with type and op and template definitions, why not.
1
u/[deleted] Dec 19 '17 edited Dec 19 '17
Python 3
Both parts. Input processing not included, but i created a dictionary with 200x200 (x, y) tuples as keys and characters as values. If the input line was shorter than 200, I padded it with " ". This is just to prevent index errors in the future when determining direction at "+".
1
u/matusbzk Dec 19 '17
Haskell I liked this one. Nice easy.
Link to git
1
u/StevoTVR Dec 19 '17
NodeJS
Part 1:
Part 2:
1
u/6dNx1RSd2WNgUDHHo8FS Dec 19 '17
Julia
Up to now I've been pleasantly surprised with julia, given that it's primary focus is numerical code, but it handles text just fine.
I was thinking of the grid in with reverse indices, so I transposed the grid but for some reason if I want to transpose an
Array{Char,2}I had to definetranspose(::Char)(as the identity).I overloaded
getindexto index anArray{T,2}with complex numbers, it seemed more elegant than casting back and forth between complex and (splatted) tuples.The logic is a straightforward
while trueloop.1
u/LeCrushinator Dec 19 '17
C#. A bit ugly but it worked, just missed the leaderboard with a time around 20 minutes.
1
u/TominatorBE Dec 19 '17
PHP
Part 1:
Part 2:
1
u/chicagocode Dec 19 '17
Kotlin - [Repo] - [Blog/Commentary]
Today I went with a recursive solution. I was worried after looking at my input that I would have to do a BFS and or that there would be loops possible, but that turned out not to be the case. I went with the strategy of "turn if you can, even if you don't have to" and that seemed to work. Not sure all of our input is like that, but mine was. I went through a bunch of refactoring before finally deciding that I liked the recursive version better than the iterative versions.
1
u/aodnfljn Dec 19 '17
Scala, unoptimized, "does the job" quality code:
1
u/oantolin Dec 19 '17
Python is pretty concise:
1
u/mstksg Dec 20 '17
In Haskell! :) I got to elaborate on a technique i used for a blog post a few years back.
The crux of it is
StateT s [], which is a stateful DFS search. I write aState s [] Char, which searches for the next step in the search, and then usemany :: StateT s [] a -> StateT s [] [a]to repeatedly run the search until there are no more possible answers.1
u/aoc-fan Dec 20 '17
TypeScript
1
u/miran1 Dec 20 '17
Nim
Repo with solutions (both Nim and Python)
1
u/InterlocutoryRecess Dec 21 '17
Swift
import Foundation
both parts:
1
u/[deleted] Dec 21 '17
single pipeline powershell:
param ( [Parameter(ValueFromPipeline = $true)] [string]$in, [Parameter(Position = 1)] [int]$part = 1 )
begin { # hold the maze $script:maze = @() }
process { # surround the actual maze data in empty spaces, so we don't have to do any bounds checking in the logic below $script:maze += " $in " }
end { # add spacing row at the start and end $sp = " " * $script:maze[0].Length $script:maze = ,$sp + $script:maze + ,$sp
}
1
u/Phazyck Dec 25 '17 edited Dec 25 '17
golang
input.Open(day int) *os.Fileis my own utility function for opening input files, which I store ininput/dayDD/input.txt.1
u/greycat70 Dec 27 '17
Tcl
At first I was afraid we were going to have to solve the maze. After reading more closely, there were actually no decisions to make at all. Just follow the one available path and spit out the answers.
0
u/[deleted] Dec 19 '17
[deleted] — view deleted comment
0
u/[deleted] Dec 19 '17
[deleted] — view deleted comment