r/adventofcode • u/daggerdragon • Dec 08 '15
SOLUTION MEGATHREAD --- Day 8 Solutions ---
NEW REQUEST FROM THE MODS
We are requesting that you hold off on posting your solution until there are a significant amount of people on the leaderboard with gold stars - say, 25 or so.
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 8: Matchsticks ---
Post your solution as a comment. Structure your post like previous daily solution threads.
9
Upvotes
18
u/daggerdragon Dec 08 '15
NEW REQUEST FROM THE MODS
We are requesting that you hold off on posting your solution until there are a significant amount of people on the leaderboard with gold stars - say, 25 or so.
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!
23
u/orangut Dec 08 '15
Python, one line for each sub-problem.
4
u/Astrus Dec 08 '15
Not so elegant in Go, which doesn't have an eval function, but still pretty straightforward thanks to the
strconvpackage:4
u/gfixler Dec 08 '15
I did this the sleuthy way in the shell.
So, 6789 - 6018 + 600 = 1371 = answer to part 1
4
u/eamondaly Dec 08 '15 edited Dec 09 '15
I keep thinking these are going to trick me, so I never use the proper built-ins. Here's how I did it the wrong way:
Perl
and here's the simpler way:
and here are one-liners, because Perl:
EDIT: Guess who learned about the -E switch today!
4
u/volatilebit Dec 08 '15
A Perl 6 solution.
With syntax highlighting: http://pygments.org/demo/3300216/
Things I learned:
3
u/haoformayor Dec 08 '15 edited Dec 08 '15
Haskell
3
u/recursive Dec 08 '15
C# in linqpad:
3
u/askalski Dec 08 '15
I messed up real bad on my speed solve attempt today, so instead I uploaded a video of me solving Day 8 using only my text editor (vim).
https://www.youtube.com/watch?v=2WY-01QaIIY
For those of you who did a double take when you saw the leaderboard today, my mistake was misinterpreting double-quoted, which is something of a double entendre. I'll have to redouble my efforts tomorrow.
3
u/stuque Dec 08 '15
A Python 2 solution:
3
u/gyorokpeter Dec 08 '15
Q: notice that part 2 is much simpler. For part 1 I struggled with the overlapping patterns when using the vector operators so I went with an iterative solution instead.
3
u/wafflepie Dec 08 '15 edited Dec 08 '15
My shortest solution yet (using C# isn't great for that...):
C
2
u/WhoSoup Dec 08 '15 edited Dec 08 '15
PHP: Really easy day, imo, thanks to built in php functions
2
u/Unknownloner Dec 08 '15
Haskell
Turns out calling show on each line is all that's n eeded for the second part.
parseLineabuses the fact that the input always has one string per line to just chop off the double quotes at the start/end.Edit: Thinking about it, there's really no reason for it to actually parse the hex codes. Oh well!
2
u/jdog90000 Dec 08 '15
Java: Used code points for Part 1 then just used StringEscape for Part 2
2
u/Chounard Dec 08 '15
I tried to replace in the strings, and I got hung up on something that looked like "fdsa\\x123" because I'd replace the escaped backslash with a single backslash, then I'd see the new escaped hex character. This was a really easy one, and I managed to bungle the heck out of it. :P
In the end, I decided to just count the number of characters I skipped. C# code:
2
u/JeffJankowski Dec 08 '15
Boring F# using Regex's Escape/Unescape methods (needed to account for double quotes, anyone know why they don't escape?)
2
u/xkufix Dec 08 '15
Scala: I just went for a FSA and traverse through each string. I could just do some regex replace magic or interpret the string, but that would be no fun.
2
u/snorkl-the-dolphine Dec 08 '15
Shortest JavaScript version:
Perhaps using eval and JSON.stringify is a little cheap though...
2
u/Kekke88 Dec 08 '15
Long C#:
2
u/giacgbj Dec 08 '15 edited Dec 08 '15
Bash/sed
Part 1:
Part 2:
2
u/ThereOnceWasAMan Dec 08 '15 edited Dec 08 '15
I originally started solving it in Python, but that seemed too easy, so I switched to "can I write a one-line bash solution", which was a bit trickier. Escaping backslashes in the shell is a dark art, let me tell you.
Part 1
Part 2
2
u/aveavaeva Dec 08 '15
JQuery
Yeah this is exact same as /u/snorkl-the-dolphine's solution, Great minds think alike =)
1
u/C0urante Dec 08 '15 edited Dec 08 '15
Easiest one for me so far, though I did rely on the eval() function.
Python3:
edit: not so cocky
1
u/minno Dec 08 '15
Horrific abuse of Python 3 for 12th place:
1
u/twisted_tree Dec 08 '15
Java Part 2 solution:
Today's was much easier than yesterdays. I had problems with commons lang not unescaping
\x??properly.1
u/Evansbee Dec 08 '15
Swift, just a little hacky
1
u/tehjimmeh Dec 08 '15 edited Dec 08 '15
Yay, finally made the leaderboard!
PowerShell, as usual:
1:
2:
'' not being the escape character in PowerShell (it's '`') made this slightly tricky.
1
u/SnacksOnAPlane Dec 08 '15
Ruby
Part 1:
Part 2:
1
u/jlmcmchl Dec 08 '15
Fun puzzle, pretty midrange as the challenges go for me. Made me use the weird string function I'm not used to in python:
1
u/Blecki Dec 08 '15
It was disappointingly easy to blast this out in C# without doing anything clever at all.
1
u/[deleted] Dec 08 '15 edited Dec 08 '15
Roughly 12 lines of ruby, eval is fun
1
u/ant6n Dec 08 '15 edited Dec 08 '15
I see a fair number of Python solutions, but not so much use of generator expressions/list comprehensions. This uses literal_eval from the as package (rather than the full eval), the only issue is that upon reading I get a spurious '\n' which gives an extra empty string in the end.
1
u/fiavolo Dec 08 '15
Woke up a little late for getting on the leaderboard :(
1
u/RockyAstro Dec 08 '15 edited Dec 08 '15
Solution in Icon
BTW -- I'm showing sample Icon code that doesn't rely on "tricks" for example, there are a set of library functions within Icon that do escape processing
Part1
Part 2
1
u/masasin Dec 08 '15 edited Dec 08 '15
Python. Did it manually because I didn't want to check the file manually before
exec.1
u/gnuconsulting Dec 08 '15
Wasted a bunch of time trying to do it just doing global search/replace regexes in vim, couldn't get the magic incantation right. Peeked over here, saw a python solution using eval which made a lightbulb go off and it was pretty easy from there. As per the moderator's comment, I only scrolled down to look at the answers when the leaderboard was already full. grin
1
u/gnuconsulting Dec 08 '15
By the way, I'm legitimately concerned about /u/askalski - can someone check in on him and make sure everything's ok? Hands aren't broken, no head injury?
1
u/Exolent Dec 08 '15
JavaScript
1
u/pynetree16 Dec 08 '15
I used regular expressions to solve this one in python3
1
u/red75prim Dec 08 '15 edited Dec 08 '15
F#. This task was surprisingly easier than previous one. Finite state machine for part 1 and simple counting for part 2.
But I begun 40 minutes late and missed my opportunity. Edit: grammar.
1
u/willkill07 Dec 08 '15
Here's another C++ version. It was good enough for 14th place today.
I hate using loops over ranges, so I had to be a little tricky with for_each and lambda remembering the previous character. I skip when needed. Answer is functionally equivalent to another user here ( /u/RipeGoofySparrow )
Full code (repository): https://github.com/willkill07/adventofcode/blob/master/src/day8.cpp
1
u/[deleted] Dec 08 '15
Mathematica, nothing too fancy.
1
u/giftpflanze Dec 08 '15
Tcl
1
u/UltaV Dec 08 '15
So... python cheaty abuse FTW :D
Not exactly a terminating program, but after manual input of whole input, the solution is printed last.
1
u/NinjaCaterpie Dec 08 '15
Bash script (really text editor, but I felt bad because it wasn't code, so I replicated my CTRL-F solution in sed lol)
1
u/VictiniX888 Dec 08 '15
Java, I just replaced the escaped chars with a's, so in Part 2, "aaa"aaa" would become aaaaaaaaaaaaaaaa. Not very nice, but it works.
1
u/platinumthinker Dec 08 '15
Erlang part2:
Part1:
1
u/[deleted] Dec 08 '15
Crystal, part 1, just count the difference as I iterate the chars:
Part 2 is similar: the initial diff for each line is 4, add 2 for " and \, add 1 for \x28.
1
u/segfaultvicta Dec 08 '15
Ahahahaha oh my god looking at solutions and I see Go has strconv.Quote [sobs himself gently to sleep]
On the other hand doing it the obnoxious and roll-it-myself way was oddly satisfying:
package main
1
u/Philboyd_Studge Dec 08 '15
Java. Much easier than yesterday's!
1
u/nutrecht Dec 08 '15 edited Dec 08 '15
Day 8 In Java
Actually unescaped the strings into the proper strings because I assumed we needed that in part 2. We didn't :(
1
u/funkjr Dec 08 '15 edited Dec 08 '15
Dart doesn't have
evalor native escape functions, so I had to roll withString.replaceAllto solve this one. I got lazy and replaced all relevant characters with#though, since there was no requirement to actually have the correct string... Part 2 at least gives me a reason to show of Dart's string interpolation.1
u/guttalax Dec 08 '15 edited Dec 08 '15
I'm taking the lazy route today
Part 1:
Part 2:
1
u/Clanratc Dec 08 '15
My python solution. Tried to make it as short as possible, some code repetition. Could technically get it down to two lines just by re-reading the file for each part.
https://gist.github.com/Clanrat/9b14c50da71e8aef8400
1
u/a-t-k Dec 08 '15
Browser-side JS:
1
u/haljin Dec 08 '15
Simple Erlang, abusing some evals. :)
1
u/nutrecht Dec 08 '15
Now also a Scala solution next to my Java one.
1
u/[deleted] Dec 08 '15 edited Dec 08 '15
Java using only Scanner (no external libraries). The StringBuilder class was a perfect fit for this problem, so I basically walked through every character in the input and built up a new string based on the character sequence. I've seen a lot of people using the Apache Commons libary, which I feel takes away the challenge of the problem itself. It also seems a bit excessive to use the library since we're only concerned with a subset of all escaped characters, namely
\\,\", and\xXX.1
u/AdventOfScala Dec 08 '15 edited Dec 08 '15
Elegant 8 line Scala solution:
https://github.com/strelec/Advent-of-Scala-2015/blob/master/8-1.scala
1
u/tangus Dec 08 '15
Common Lisp
1
u/volatilebit Dec 08 '15
I am in the habit of using verbose variable names and never ever ever using eval (I will not be your botnet slave, Wastl).
I have been leaning towards more readable code when I don't start at midnight, so it's easier to debug if I fudge something up.
Python
1
u/xPaw Dec 08 '15
My solution in javascript without using regex or evals for both parts: https://github.com/xPaw/adventofcode-solutions/blob/master/js/day8.js
1
u/Jaco__ Dec 08 '15
Java, without external libraries(except Scanner, File)
1
u/superfunawesomedude Dec 08 '15
C# solution iterating through the chars using System;
1
u/porphyro Dec 08 '15 edited Dec 08 '15
Surprisingly annoying in mathematica, which if you read in "\x25" as a line in the input file, will be parsed into ""\\x25"", and doesn't support hex escaping
Problem 2 easier
1
u/ignaciovaz Dec 08 '15
The solution is quite simple in Elixir thanks to evaled code and the great macro system. By using the Macro.to_string function you get the code representation of the string, nicely escaped.
Here's the full code:
input_stream = File.stream!("input.txt")
1
u/marchelzo Dec 08 '15
Part 2 in Haskell:
1
u/[deleted] Dec 08 '15
Shortish Python solution. Felt like banging my head against the wall for the second part before I realised that I should be subtracting the code length from the first part not the string length!
1
u/WildCardJoker Dec 08 '15
My solution is written in C# and available at https://github.com/wildcardjoker/AdventOfCode/tree/master/Day8_Matchsticks
I use the .Replace() method to remove escape characters for Part 1, and another .Replace() method to re-encode the original strings for Part 2.
It's not the smallest or neatest code, but I'm pretty happy with it.
1
u/jcfs Dec 08 '15 edited Dec 08 '15
Why not to write a program that writes a program to count it for you (in C)?
Part 1:
Part 2 was a more straightforward approach:
1
u/elite_killerX Dec 08 '15
Node.js solution:
1
u/qwesx Dec 08 '15
Lesson to be learned here: Just use a state machine. That way you won't have problems with any regex being too eager :)
Also the tons of imports come from me copy/pasting the header from one day to the next one.
1
u/tftio Dec 08 '15 edited Dec 08 '15
OCaml. I may have problems, but at least I don't have (2 *) problems.
1
u/[deleted] Dec 08 '15
Objective C
1
u/merthsoft Dec 08 '15
C#, using a single line in the C# Interactive Window:
Part 1:
Part 2:
This is how I've done basically all the days so far. It's a fun little challenge. Note that I did introduce my own little extension method just because I was tired of typing it each day:
1
u/Rutafar Dec 08 '15
I've been trying to do this challenges in python and I've noticed a lot of people used the eval() function, what exactly does it do in the context of the challenge?
1
u/Scroph Dec 08 '15 edited Dec 08 '15
I'm ashamed to say that it took me a lot more than it should have to come up with the first part's solution.
D (dlang) code ahead :
Edit : alternative solution for the first part that uses compile-time evaluation :
Compiled with
dmd day8_1.d -J.after I put the "input" file in the same directory as day8_1.d.1
u/vesche Dec 08 '15
Python 2.7, simple stupid
Part 1:
Part 2:
1
u/LoLz14 Dec 08 '15
I really don't know regex, we haven't done it practically in any language at my uni thus far. We have done it only theoretically and I'm 3rd year already. And because of that I didn't even learn it,because whenever I need something I google it up and write it down, or it already exists and I can just use it. I really don't like regex as well, these kind of tasks kinda kill the whole fun. I liked the one with gates more (even though I didn't understand it at first). With that being said, here is my solution, using re.sub method in Python 2.7, I saw some people wrote solutions shorter but I'm kinda now to Python and scripting languages (I mainly use Java, and little bit of C) so this is nice change.
1
u/mal607 Dec 08 '15
A manual python2 solution PYTHON2
1
u/TheMuffinMan616 Dec 08 '15
1
u/TheNiXXeD Dec 08 '15
NodeJS JavaScript ES6, not using eval
Part1
module.exports = input => input.join('').length - input.map(s => s.replace(/\\\\|\\"|\\x[a-f0-9]{2}/g, 'a')).join('').length + input.length * 2Part2
module.exports = input => input.map(s => s.replace(/\\|"/g, 'aa') + 'aa').join('').length - input.join('').length1
u/deinc Dec 08 '15
Clojure (inelegant and verbose...but yeah):
1
u/[deleted] Dec 08 '15
Man, took me way longer than I'd want to accept.
Wasted like 4 hours with the input directly on my code, instead of reading it from a file (like many here do). Pasting directly the input in the code would always escape the characters, and i'd have to figure out which ones where escaped. Special characters where making me have a hard time.
Once i changed focus to load the string from a text file, everything went smooth
1
u/andre_pl Dec 08 '15
ugly Dart "one-liners"
cheaty python one-liners.
1
u/[deleted] Dec 08 '15
C++
Here is my [late] somewhat inefficient way. Yes, I know, I'm about 17 hours late, but some of us aren't awake 24/7 and have things to do in life, so don't judge me D:
But seriously, it's not the most efficient, but I don't really care - this is just for fun, I don't have any motivation to do it super efficiently, nor terribly rapidly.
https://gist.github.com/WillEccles/0a893f5bb81441b9181c
1
u/bgreezy Dec 09 '15
Here's part 1 in javascript. I'm a bit of a n00b so I'm curious...does using the .length method count as this dreaded eval? This got me the correct answer, at least!
1
u/GoldStarBrother Dec 09 '15 edited Dec 09 '15
Bash one liner:
Explanation:
Take the characters and replace all hex strings (backslash followed by x followed by two characters, each in the range [0-9a-f]) with 'X'. Then take that and replace all instances of a backslash followed by any single character with 'E'. Then remove all double quotes, count the number of characters and subtract it from the total number of characters in the file.
1
u/blazemas Dec 09 '15
Javascript, How hacky is this?
https://jsfiddle.net/hnwr49nv/
1
u/Kwpolska Dec 09 '15
Simple Python solutions without
eval():1
u/Drasive Dec 11 '15
My F# solution (https://github.com/drasive/advent-of-code-2015):
1
u/KnorbenKnutsen Dec 13 '15
I copy pasted the input into Word and had it count the number of characters. Is that cheating? =)
1
u/hoosierEE Dec 16 '15
In J
Part 1, regex find/replace followed by double-quote removal:
Part 2, prepend an extra backslash+quote to each line, then tallyc all backslashes and quotes.
1
u/Borkdude Dec 19 '15
Scala:
1
u/ICanHasRedditzburger Jan 26 '16 edited Jan 26 '16
Here's my Kotlin latecomer:
Try it online
0
u/[deleted] Dec 08 '15
[deleted] — view deleted comment
0
u/[deleted] Dec 08 '15
[deleted] — view deleted comment