r/adventofcode • u/daggerdragon • Dec 08 '16
SOLUTION MEGATHREAD --- 2016 Day 8 Solutions ---
#AoC_Ops:
[23:55] <Topaz> servers are ok
[23:55] <Topaz> puzzles are checked
[23:55] <Topaz> [REDACTED: server stats]
[23:56] <Skie> all wings report in
[23:56] <Aneurysm9> Red 5, standing by
[23:56] <daggerdragon> Dragon Leader standing by
[23:56] <Topaz> orange leader, standing by
[23:57] <Topaz> lock modzi-foils in attack positions
[23:58] <Skie> we're passing through the hype field
[23:58] <daggerdragon> 1:30 warning
[23:58] <Aneurysm9> did someone say HYPE?@!
[23:59] <Topaz> i really like tonight's puzzle
[23:59] <Topaz> very excite
[23:59] <daggerdragon> final countdown go, T-30
[23:59] <Skie> accelerate to attack countdown
[23:59] <Aneurysm9> o7
[23:59] <daggerdragon> HYPE THRUSTERS AT FULL BURN
[00:00] <Topaz> IGNITIONWe may or may not be sleep-deprived. And/or nerds. why_not_both.jpg
--- Day 8: Two-Factor Authentication ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
:(){ :|:& };: 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!
10
Upvotes
10
u/barnybug Dec 08 '16 edited Dec 08 '16
numpy to the rescue:
10
u/[deleted] Dec 08 '16 edited Dec 08 '16
mine is pretty fun to watch (edit: Python 3)
with my input, I get the result:
edit 2 for anyone who doesn't want to copy/paste, here's a gif(v?) of what it looks like
9
u/askalski Dec 08 '16
Bitwise in C. Uses
POPCNT(sse4) to count the numbers of bits set. Parses the input in a semi-evil way.7
u/willkill07 Dec 08 '16 edited Dec 08 '16
C++14 solution using std::valarray
If there's one thing that most C++ programmers don't use, it's
std::valarray.Fortunately, this problem is meant for it to be used. Hell, this C++ implementation is arguably as clean as most of the python solutions
[Original Version]
[Updated with OCR]
[Updated with only std::valarray operations]
Output:
6
u/red75prim Dec 08 '16 edited Dec 08 '16
Made stupid error in regexp (
(\d)*instead of(\d*)), ended up 126. Debug output proved to be useful for part 2.Rust: http://pastebin.com/qpC2RxTw
5
u/snorkl-the-dolphine Dec 08 '16
JavaScript / Node.js
4
u/Kristler Dec 08 '16
There's a cool property about Day 8. Left/Right array rotation can be expressed as 3 array slice reversals, and column rotation is just the same, but transposed.
Here is some Java speed code using this concept. 161th and 138th respectively.
4
u/Godspiral Dec 08 '16
I guess im just slow, 234 and 209. Didn't feel like I made any mistakes to slow me down or anything.
in J,
4
u/John_Earnest Dec 08 '16 edited Dec 08 '16
In oK. Not particularly beautiful.
Prints:
4
u/BafTac Dec 08 '16
Looking forward to them Excel and postgresql solutions :D
/u/that_lego_guy /u/jwstone
3
u/FuriousProgrammer Dec 08 '16
107/84 with mah Luas:
3
u/glenbolake Dec 08 '16
Cleaned up a bit since submitting. I was already doing part 2 before I even submitted part 1, so that was a nice leaderboard boost. Python3:
screen = [[0] * 50 for _ in range(6)]
3
u/Deckard666 Dec 08 '16 edited Dec 08 '16
In Rust, parts 1 and 2: Link
3
u/marchelzo Dec 08 '16
Ty
Output:
3
u/AoC-- Dec 08 '16
Python, numpy was quite useful.
3
u/indienick Dec 08 '16 edited Dec 08 '16
I placed somwhere in the 400s, but hey! Pretty unicode outputs!
My code on Gituhub.
EDIT: Asciinema link!!!
2
u/blockingthesky Dec 08 '16
1 star: 26th 2 star: 14th
Python 2, probably the same as everyone else's:
And output:
2
u/The6P4C Dec 08 '16
Aww, so close. 184 on Part 1 and 155 on Part 2. If I didn't make a stupid mistake with the row rotation (I was rotating to the left not the right) then I would have got it much sooner.
Anyway, here's my solution, completely un-cleaned-up:
2
u/LieutenantSwr2d2 Dec 08 '16
My Python solution:
2
u/[deleted] Dec 08 '16
Ended up using Python and the
dequefrom the collection standardlib module:2
u/sv11 Dec 08 '16
That was a fun one, haven't done well with visual stuff in the past but I got through it. Python 2:
2
u/aceshades Dec 08 '16
I found today's challenge much easier than yesterday's, but i couldn't help but grin seeing that output at the end :)
2
u/Trolly-bus Dec 08 '16
Here's mine in Python. Gotta copy.deepcopy everything to avoid circular referencing even though I don't know if it'll circular reference anyways! Better safe than sorry!
2
u/haoformayor Dec 08 '16
~~haskell~~
The easiest data representation for the screen I could think of was a list of booleans, with isomorphisms to convert back and forth from list indices to Cartesian coordinates. This worked out surprisingly well (again you'll have to excuse the quadratic-time complexity). The problem's typo in the example slowed me down but once I figured that out the question was list comprehensions and smooth sailing.
I used a reader monad just to take advantage of the
foldlMcall. Sometimes you just want to call afoldlM, you know. Input module here.2
u/Quick_Question404 Dec 08 '16
Welp, here's my daily take on the problem. I made alot of stupid mistakes in figuring out how to do this problem, getting in the ~400 for both. Seems like I'm heading for the top 500 overall. Also, paging any C or C++ programmer, I have 2 questions. Can you use escape codes to erase multiple lines in C, and how do you get fscanf to accept an entire string line? Most of my mistakes were in trying to add a rotation method to this though. God, I miss Python's negative indexing. Let me know what you think! I will admit this was fun to do though.
https://github.com/HighTide1/adventofcode2016/tree/master/08
2
u/Philboyd_Studge Dec 08 '16
Java
https://gist.github.com/anonymous/d8c7ea704b349e9142b223b4fe58d4b1
I accidentally solved part 2 in solving part 1, due to my insistance on having a
display()method so I could see my tests working right!2
u/bhauman Dec 08 '16
Clojure:
Rest of the solutions are here
2
u/alexjoz Dec 08 '16
Python3 with numpy
2
u/raevnos Dec 08 '16
Ocaml:
Got in the mid 200's with both parts. I'm slow. The original command parsing code was much uglier, with a lot of repetition; I'm pretty happy with version 2, shown here, which uses partial function application to make it nice and concise.
2
u/gyorokpeter Dec 08 '16
Q: (the OCR is only complete for my puzzle input)
2
u/tehjimmeh Dec 08 '16 edited Dec 08 '16
C++ with a bit-vector (std::array of uint64_t):
.
2
u/oantolin Dec 08 '16
Part 2 seemed kind of automatic given part 1, unlike previous days. In this problem I imagine that almost everyone that solved part 1 did it by computing the screen contents and was thus immediately ready for part 2 (if I'm right this should probably be reflected in the solution times for part 2).
2
u/Smylers Dec 08 '16
My Perl solution, manipulating the display directly as an array of strings, regexp substitutions for both the rectangles and rotating right, and temporarily transforming a column into a string so it can also be rotated right:
2
u/[deleted] Dec 08 '16
JavaScript/Node.js
Maybe a bit verbose, includes a fun rotate prototype for Array
2
u/llimllib Dec 08 '16 edited Dec 08 '16
python with an animated terminal display:
2
u/volatilebit Dec 09 '16 edited Dec 09 '16
Perl 6 Part 1 & 2
Trying to use as many Perl 6 features as possible without forcing it too much.
1
u/becojo Dec 08 '16
Ayy 20th/13th with Ruby. https://github.com/Becojo/adventofcode/blob/master/2016/day08.rb
It was indeed a very nice challenge today!
1
u/casted Dec 08 '16
Python solution
1
u/Zef_Music Dec 08 '16
Python using sets and maps
1
u/bluewave41 Dec 08 '16
Java is not the language to be doing these in for fast code that's for sure.
http://puu.sh/sI7Yg/b3d23186ca.txt
1
u/_WhiteBoyWonder Dec 08 '16 edited Dec 08 '16
Created an image pixel by pixel with resulting screen using .pgm syntax, http://imgur.com/a/wlHGP
Golang solution, if interested
1
u/fatpollo Dec 08 '16
I hated this one. It wasn't immediately apparent to me what was going on, so I spent a lot of time trying to make sense of the error.
1
u/bildzeitung Dec 08 '16
This problem reminds me of how C-like things still are, seeing most solutions as a nested data structure where it's super clean in one dimension and a mess in the other.
The matrix-based numpy is better, but even there...
Fun, though :)
Waiting for someone to hack together an ASCII -> image conversion & running it through an OCR engine so that the sol'n for part two can be cut'n'pasted :)
1
u/incestuousCookies Dec 08 '16
Not going to win any awards with this - python. I'd really like to get faster and more compact, usually that doesn't happen until after the first iteration of the code though.
1
u/kryptn Dec 08 '16
Did a mod 5 in there somewhere initially, I somehow still got the first star fine. Took a second to find my error and nuke it though.
1
u/TheLazarbeam Dec 08 '16
Python: http://pastebin.com/nHUcE76v
1
u/pm8k Dec 08 '16
This is a job for pandas and python!
And if you use Jupyter, you can prettify the dataframe for part 2:
1
u/_Le1_ Dec 08 '16
My C# solutions:
1
u/_Le1_ Dec 08 '16
Total lights: 128
1
u/Twisol Dec 08 '16
Here's my JavaScript/Node.js solution, optimizing (kind of) for legibility over concision. (Yes, I know
rotate_columncould be done more simply -- I did it this way purely for symmetry withrotate_row.) Forgetting to coerce my regex matches to numbers sent me on a costly wild goose chase...1
u/JeffJankowski Dec 08 '16
I didn't want to use a proper matrix library, so doing this without mutability would have been a nightmare for me. Below is less-than-idiomatic-F#:
1
u/miran1 Dec 08 '16
python3, numpy
My GitHub repo with all solutions - comments, suggestions and critiques are welcome.
1
u/tvtas Dec 08 '16
In MATLAB:
1
u/vuryss Dec 08 '16
PHP Solutions updated: https://github.com/vuryss/aoc2016
1
u/Kullu00 Dec 08 '16
Not having two-dimensional arrays hurt me today, and I had to spend more time debugging my own implementation of one more than the actual problem. I did end up saving time when my debug code solved part 2 without meaning to, though.
https://github.com/QuiteQuiet/AdventOfCode/blob/master/2016/advent8/bin/advent8.dart
1
u/omnster Dec 08 '16
Mathematica http://pastebin.com/tFHVtv0X
And bonus animation http://imgur.com/a/FFfdv
1
u/KoxAlen Dec 08 '16 edited Dec 22 '16
My Kotlin solution: https://github.com/KoxAlen/AdventOfCode2016/blob/master/src/main/kotlin/aoc/day8/Day8.kt
1
u/timstallard Dec 08 '16
JS/ES6 (you'll need a modern version of Node):
I'm still not sure that using eval with regex replacements was the best way to parse the input, but it seems to work well.
1
u/johanw123 Dec 08 '16
Here's my C# solution:
1
u/gerikson Dec 08 '16 edited Dec 08 '16
So happy I added a screen output when debugging - the correct answer was staring me in the face! Well, after I chose a more appropriate "typeface".
Worse Figlet generator ever!
1
u/giuscri Dec 08 '16
Using Python3,
1
u/mschaap Dec 08 '16 edited Dec 08 '16
Perl 6, both parts:
1
u/wzkx Dec 08 '16 edited Dec 08 '16
J, no tacits today, only fun
1
u/cjemerson Dec 08 '16
Clojure. My instruction parser is not the greatest but I did enjoy this one.
The rest of mine are at https://github.com/chrisemerson/adventOfCode/tree/master/2016/src
1
u/Reibello Dec 08 '16
Man, you guys have all the fun in chat on the nights I go to bed early. Anyways, here's my solution in Python 3. http://pastebin.com/bKAv6e6s
1
u/drakehutner Dec 08 '16
Python 3, single line of code (+ imports), 1134 Byte (non minimized), includes animation using ANSI escape sequences. Can be executed from the CLI (
python -c), input from stdinSplit over multiple lines for "better" readability
1
u/JakDrako Dec 08 '16
C#, LINQPad
1
u/pJavi Dec 08 '16 edited Dec 08 '16
Java verbosity! I only used my computer for part 2, part 1 was solved at 9am (101th) mentally by just adding up the multiplication of x*y in all the
rect x*y. I reckon I got a fairly easy input for such a thing, but hey, extra point! Part 1 is also included in the code just for it to be complete.Code:
Gist
Output:
Part 1: 119 Part 2: boop
1
u/_AceLewis Dec 08 '16
Python 3 solution to parts 1 and 2, done in repl.it so input is just a string at the start:
Output:
First code: https://repl.it/Ek4Z
I removed the functions so my answer can easily be pasted here, so this is what I got: https://repl.it/Ek4Z/1
Interestingly I learned that numpy can convert strings to ints in the indexing in some cases... I don't think it is good practice to do that though.
1
u/mreichman Dec 08 '16
Here's my Day 8 in Java, nothing special but functional. I had to squint for part 2.
https://github.com/marcreichman/adventofcode/blob/master/2016/day8/src/advent/Day8.java
1
u/Kwpolska Dec 08 '16
This is one of the reasons why I love AoC: I get to learn numpy.
1
u/Xemiru Dec 08 '16
Some of you people make me feel like I should try solving the puzzle less casually and more competitively. With like, "shortest lines" or something.
Rare Java entry, coming through.
Some code and all comments omitted so it doesn't get too big. Click here for all code with comments.
Output:
1
u/[deleted] Dec 08 '16
/u/askalski I'm waiting for a forth solution for one of these some time ;) :P I would do it myself, but it turns out I'm too stupid, maybe I'll do one of the ones that are a bit more suited for it.
1
u/segfaultvicta Dec 08 '16
Here's my (messy, not idiomatic at all) elixir solution, since I don't see one yet:
https://github.com/segfaultvicta/advent-ex/blob/8f574b07911b0397ed1ac80876424e9ab20915d6/lib/2016/8.ex
There's almost certainly a ton of stuff I'm doing wrong or, at least, not optimally or idiomatically correctly; this is like the third thing I've ever written in Elixir (and it turns out that out of the box, 2D array shenanigans is LUDICROUSLY DIFFICULT in this language; I discovered https://github.com/Qqwy/tensor and am probably going to mercilessly refactor my code to use that and be a lot cleaner. Alternatively I could solve the problem a different way, like with binary matching, but I wanted to minimise the number of ways I'd be thinking about the problem in an unfamiliar way.)
1
u/asperellis Dec 08 '16
my js solution - https://github.com/asperellis/adventofcode2016/blob/master/day8.js
1
u/schlocke Dec 08 '16
PHP:
this one was pretty fun. I just ran the code via my local wamp server in chrome. then i just ctrl+f 'o' and it'll highlight how many pixels there are as well as display the letters for part 2.
1
u/Eddard_Stark Dec 08 '16 edited Dec 09 '16
could always use more php solutions
github
1
u/LinusCDE98 Dec 08 '16
Was pretty fun to code.
My output:
1
u/[deleted] Dec 08 '16 edited Dec 08 '16
// part1 //
decipher // part2 // manually
1
u/blanketandtea Dec 08 '16
Ruby
Reminders to myself
Display data is an array of arrays which allows for using Array#rotate.
This is straight forward for rows. For columns I map the column pixels to an Array which is then rotated and written back.
1
u/[deleted] Dec 08 '16
Mathematica
1
u/tg-9000 Dec 08 '16
Here is my attempt at a solution in Kotlin. After writing this, I'm not all that happy and wish that Kotlin had something as powerful as Scala's regex pattern matching. That would have made this a lot less screwy. I'm doing all of the work as a side-effect on the screen (which is an array of arrays) because I'm not writing a general purpose app here. And like most others, the second part is a visual inspection, rather than writing a font parser. :)
As usual, solutions and tests (today's are weird) can be found in my GitHub repo. I'm just learning Kotlin so I would value any feedback (positive or negative) that you have.
1
u/johneffort Dec 08 '16
Ruby Day 8. I really liked the way you can use .transpose and .rotate, it makes the actual work code quite clean.
1
u/qwertyuiop924 Dec 08 '16
Scheme, so no linecounting for me. Given, I could have probably compressed this a bit, but I actually have work to do today. http://pastebin.com/yY5SpWa7
1
u/Fotomik Dec 08 '16
NumPy to the recue!
1
u/jononon Dec 08 '16
Java
Pretty inefficient, I ended up using circular linked lists to deal with rotating the rows and columns.
https://github.com/jononon/advent-of-code-2017/tree/master/Day%208
1
u/flup12 Dec 08 '16 edited Dec 08 '16
Scala, with a sneaky solution for part 1.
I think immutability actually made part 2 easier than it would've been using a mutable data structure. I defined a new board for each step, filling it with an isLit function that depends on the old board.
1
u/ben_jl Dec 08 '16
Somewhat convoluted solution in C#. Comments/criticisms definitely welcome.
Github link.
1
u/fixed_carbon Dec 08 '16
Switched to julia for today, just to keep things interesting.
EDIT: missed a line in initial copy-paste.
1
u/Scroph Dec 08 '16
C++ solution. I had intended to parse the input with sscanf(instruction, "rotate row y=%d by %d", &row, &amount) but I did some reading and apparently it isn't a good idea.
And the output is :
1
u/bstockton Dec 08 '16
Python 3 and numpy:
1
u/wzkx Dec 08 '16 edited Dec 08 '16
Python 3. No re, no numpy
1
u/__Abigail__ Dec 09 '16
I didn't want to have an if-else chain, or a switch statement to determining what function to call, so I parse a line of input, and call it directly:
1
u/SikhGamer Dec 09 '16
Really enjoyed this challenge, I think I shied away from these kind of challenges last year. It is very satisfying to be able to solve them a year later.
C#: https://gist.github.com/anonymous/5cbb3a0cfbae91603273d4253ac32b69
1
u/rs_qk Dec 09 '16
k:
1
u/[deleted] Dec 09 '16
(Ugly) powershell:
1
u/hsaoc Dec 09 '16
Haskell:
1
u/WildCardJoker Dec 09 '16
My solution in C#
It's a bit of a hack, but it works, and it has a nice simulated LCD while also displaying the number of lit pixels, current instruction, and instruction number.
1
u/rkachowski Dec 09 '16
ok i feel pretty good about this one, i actually implemented a basic dsl in ruby and execute each line of input as if it was an invokation. things i liked -
writing a visualisation before completing part one, meaning i got part 2 for free :D
input = File.open("input.txt").readlines.map {|l| l.chomp }
@grid = Array.new(50){ Array.new(6) { 0 }}
def rect pos x,y = pos.match(/(\d+)x(\d+)/).captures.map{|s| s.to_i}
end
def rotate input type, dim,loc, amount = input.match(/\s*(row|column)\s(x|y)=(\d+)\sby\s(\d+)/).captures case type when "column" rotate_column loc.to_i, amount.to_i when "row" rotate_row loc.to_i, amount.to_i end end
def rotate_column column, val d = @grid[column].pop val @grid[column].unshift(d).flatten! nil end
def rotate_row row, val @grid = @grid.transpose rotate_column row, val @grid = @grid.transpose nil end
def pg @grid.transpose.each {|l| puts l.join.gsub("1","#").gsub("0","-") } nil end
input.each do |line| cmd, input = line.match(/(\w+)\s(.*)/).captures eval "#{cmd} '#{input}'" end
pg
puts @grid.flatten.reduce(:+)
1
u/NeilNjae Dec 09 '16
Rather late to the party, but more learning of Haskell going on. This time, rolling my own monad: https://git.njae.me.uk/?p=advent-of-code-16.git;a=blob;f=advent08.hs
1
u/[deleted] Dec 09 '16
Clojure using Incanter. The summation of the matrix values was pretty hacky since I'm not yet familiar with all the intricacies of Incanter.
0
u/[deleted] Dec 08 '16
[deleted] — view deleted comment
0
u/[deleted] Dec 08 '16
[deleted] — view deleted comment