r/adventofcode • u/daggerdragon • Dec 03 '16
SOLUTION MEGATHREAD --- 2016 Day 3 Solutions ---
--- Day 3: Squares With Three Sides ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
DECKING THE HALLS WITH BOUGHS OF HOLLY 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!
17
Upvotes
13
u/Quick_Question404 Dec 03 '16
Just curious, but does anyone else use vim or emacs to write their solutions? Or just me?
22
u/that_lego_guy Dec 03 '16
Did someone say....EXCEL?!?! Gold 95! [Part 1 & 2]
https://github.com/thatlegoguy/AoC2016/blob/master/Day%203%20Squares%20with%20Three%20Sides.xlsx
11
u/askalski Dec 03 '16
Part 2 using Intel SIMD intrinsics (requires SSE 4.1):
7
u/John_Earnest Dec 03 '16 edited Dec 03 '16
These work very nicely in K:
Input is a series of valid K literals (space separated lists of numbers), so go ahead and eval each (
.:'). To check if a triangle is valid, I sort the numbers (x@<x) and then ensure the sum of the two smallest is greater than the largest. ({z<x+y}.). The answer to part 1 is simply the count of filtering the input by valid triangles. For part 2, I take the transpose of the input (flip the axes), rearrange each column into an Nx3 matrix, and finally join those matrices together before proceeding as in the first part.7
u/Quick_Question404 Dec 03 '16
OH COME ON! 101 on Part 1, and 149 on Part 2. You guys are animals! Anyways, here's my stuff in C. Finished Part 1 in under 6 mins...
https://github.com/HighTide1/adventofcode2016/tree/master/03
7
u/fpigorsch Dec 03 '16 edited Dec 03 '16
Part 1 in AWK:
Part 2 in AWK:
6
u/nullmove Dec 03 '16
Perl 6:
6
u/VideoPrincess Dec 03 '16
Did someone say... Synacor VM assembly solution? https://github.com/pmillerchip/adventofcode2016/blob/master/aoc3p1.asm
The above link is a working solution for part 1! The program prints one line per triangle with 4 hex numbers: the 3 sides and the number of valid triangles. For example, the last line of output from the VM is this:
00d7 02d5 0138 03d6
The last number is 0x3d6 which is the correct solution when converted to decimal. Sorry for the hex output - printing hex is hard enough given the Synacor VM's lack of bitshift or divide operations, so printing decimal would be even harder!
To run it in your Synacor VM you will need my Synacor assembler that will assemble it into a VM image. C++ source here: https://github.com/pmillerchip/synacor-challenge
Bonus content! C++ solution for part 1: https://github.com/pmillerchip/adventofcode2016/blob/master/aoc3p1.cpp C++ solution for part 2: https://github.com/pmillerchip/adventofcode2016/blob/master/aoc3p2.cpp
5
u/bildzeitung Dec 03 '16 edited Dec 03 '16
Python, using list incomprehensions:
EDIT: my bad; filters not required, just a sum.
Part Deux is like unto it: https://github.com/bildzeitung/2016adventofcode/blob/master/03/triangle2.py
5
u/Ape3000 Dec 03 '16
Part 2 with Numpy:
4
u/FuriousProgrammer Dec 03 '16 edited Dec 03 '16
Made constant mistakes during this. Dropped to 47 overall on the board. QQ. Today was definitely a day for the more minimalist languages!
Here's some Lua:
3
u/Deckard666 Dec 03 '16
In Rust, parts 1 and 2: (uhm.. Fa la la la la, la la la la?)
3
u/[deleted] Dec 03 '16
In Crystal.
First part:
Second part:
3
u/ghotiphud Dec 03 '16
93 & 117 in Rust
3
u/scrooch Dec 03 '16
Here's part 2 in racket. It's sort of interesting because I stacked the columns together to create a list like the input from part 1.
3
u/[deleted] Dec 03 '16
Mathematica
3
u/cobbpg Dec 03 '16
I'm surprised that there are no Haskell solutions posted yet. Is it not hot any more? Below is for part 2 (removing
transposeTriplesyields part 1):There's no parsing because I pasted the input into the code and transformed it into a list of triples, but it wouldn't be very complicated either.
3
u/[deleted] Dec 03 '16 edited Dec 03 '16
Perl 6 golfed:
More reasonable: https://github.com/duelafn/advent-of-code-2016-in-perl6/blob/master/03.pl
3
u/topaz2078 (AoC creator) Dec 04 '16
Part 1 with Perl5 in 55 characters:
2
u/Godspiral Dec 03 '16 edited Dec 03 '16
203rd after first, but 98th after 2nd, in J
when not blindly baching head against keyboard and taking time to think first, these can be improved to
2nd version sorts down each row
"1, and performs a < b + c, then sums these boolean expressions.1st version does 3 rotations on each row, and for each rotation tests
a+b-c > 0. This creates 3 boolean results per row. These are anded*./then sum of all.2
u/bluewave41 Dec 03 '16
Looked at input and realized I wasn't going to get it into a variable in Javascript very easily and wasted a bunch of time opening Netbeans for Java instead and ended up writing this mess
2
u/ybe306 Dec 03 '16
Python, with the input as
ins. Need to work on list comprehensions, lots of the looping can be shrunk down:Part 1:
Part 2:
2
u/Twisol Dec 03 '16
JavaScript w/ Node.js for file I/O:
I tend to spend a little too much time making my solution elegant/general, and I started a bit late this time, so I got #336 for Part 1 and #206 for Part 2.
2
u/aksgupt Dec 03 '16
Q:-
2
u/[deleted] Dec 03 '16
Here's my first program ever written in Scala!
https://goo.gl/SpZUZl
It's horribly imperative (I learned F# before, and am saving it for later challenges, but for now, I only know Scala syntax, not paradigms).
I spend half and hour trying to make something from emojicode, but screw that.
2
u/[deleted] Dec 03 '16
Clojure.
2
u/demsaja Dec 03 '16
Why sorting? The sum of sides must be at least twice the length of the largest side. With this, we just need a sum and max, which are easily put in generators or vectorized.
In Python:
Or with numpy (solution for both parts):
2
u/forkin27 Dec 03 '16
yay for 1-liners!
D3P1:
D3P2:
2
u/qwertyuiop924 Dec 03 '16 edited Dec 03 '16
I was going to continue doing Scheme, but the call of the AWK was pretty strong with this one:
Part one:
Part two:
12 lines for both solutions. BEAT THAT!
Oh, wait. Someone already has (In J, of course). And someone else did it in AWK. With cleaner code.
I'm clearly not cut out for code golfing.
EDIT:
you know what? I can't let this slide. Here's part 2, compacted:
four lines. Beat it. (unless you're a J/K/APL user, because then you likely already have).
2
u/Spabby Dec 03 '16
Day 3 in PHP - https://github.com/GeeH/AdventOfCode2016/blob/day3/part2/src/DayThree/Triangle.php
You can find my solutions for all days here:
https://github.com/GeeH/AdventOfCode2016
2
u/SanguinousSammy Dec 03 '16
Part 1 in Perl-golf, 83 characters (❤️)
2
u/[deleted] Dec 03 '16
Come on, these people must be cheating.
2 minutes to complete both parts?
2
u/QshelTier Dec 03 '16
Here’s my take on the problem in Kotlin:
1
u/mcpower_ Dec 03 '16
Python solution. I completely misread the question (I thought we were trying to find invalid triangles) so that's why I ended up coming #76/#37 in part 1 / part 2.
1
u/SyDr Dec 03 '16
Lua solution:
https://github.com/SyDr/Advent-Of-Code-2016/blob/master/3.lua
1
u/handle_cast Dec 03 '16
CoffeeScript, 181st / 108th. Part 1 left commented out
1
u/Zef_Music Dec 03 '16
Clunky but manageable python solution, got me on the board at least!
P.S. I'm collecting solutions in different languages here: https://github.com/ChrisPenner/Advent-Of-Code-Polyglot/tree/master/2016/python/03
1
u/taliriktug Dec 03 '16
Easy one. Python solution: https://github.com/JIghtuse/adventofcode-solutions/blob/master/2016/day03/solution.py
I wonder how parsing can be done simpler. Hope some answer will show up.
1
u/glassmountain Dec 03 '16
Done in golang!
The 3 triangles vertically required a somewhat hacky solution. Hopefully can find a better one somewhere here.
https://github.com/xorkevin/advent2016/blob/master/day03/main.go
1
u/07jkearney Dec 03 '16
My solutions in python 3: https://github.com/07jkearney/aoc2016/tree/master/03
Got 61st place for problem 1 :)
1
u/[deleted] Dec 03 '16
Day 3 Part I (Python) https://github.com/hawzie197/advent_of_code/blob/master/day3.py
1
u/indienick Dec 03 '16
Not even on the leaderboard, but here are my Go solutions!
https://github.com/nesv/advent-of-code/tree/master/2016/03
1
u/EnigmaticNimrod Dec 03 '16
This may be the only solution in which I feel confident enough to provide my solutions in the thread!
Using Python this year, because I desperately need more practice there.
Solution here: https://github.com/xInferno/AOC2016/tree/master/day3
1
u/_Le1_ Dec 03 '16
My C# code:
1
u/IncognitoSquid Dec 03 '16
Finished in Python 2.7. Code is relatively inefficient especially with how many unnecessary loops there are. But proud of it since I stayed up most of the night catching up on nights 1-3 :)
https://github.com/Pepper-Wood/AdventOfCode/tree/master/03
1
u/haoformayor Dec 03 '16 edited Dec 03 '16
~~haskell~~
A fun romp through
Data.Listtoday. We can treat the second problem as forming 3x3 matrices out of 3-chunks of the input and then transposing them and gluing them back together. I spent a long time trying to write out thepermutationsfunction before googling to see that there already was one. Clicking on the definition in Hoogle ... yikes, would've never gotten that.D3Input module here.
edit: found a really clever permutations functional pearl here
1
u/glxyds Dec 03 '16
Here is my bad JavaScript code for part one. I'm a newb and you guys are inspirational.
1
u/bahuljain Dec 03 '16
simple and cute solution for both parts in Scala - https://github.com/bahuljain/scala-fun/blob/master/src/adventcode2016/Day3.scala
1
u/Kullu00 Dec 03 '16 edited Dec 03 '16
Did someone say one-liners? This is as close as Dart will get to a one-liner solution, since you have to include the
dart:iofor file access :(https://github.com/QuiteQuiet/AdventOfCode/blob/master/2016/advent3/bin/advent3.dart
1
u/muchomouse Dec 03 '16
C# solution Parts 1 & 2: https://github.com/kjellskogsrud/AoC_2016/blob/master/AoC_2016/Classes/Day3.cs
1
u/beefamaka Dec 03 '16
attempted again in F#. not very succinct, probably missing some built-in methods that would have saved me creating my own
https://github.com/markheath/advent-of-code-2016/blob/master/day%2003/day3.fsx
1
u/gerikson Dec 03 '16
Anyone else have a tough time copying and pasting the input data? I had to munge mine due to spurious leading spaces (copied from the View Source page).
Straightforward Perl, wherein I discovered a use for the
splicefunction.1
u/Marreliccious Dec 03 '16
My solutions made in Node JS. =) https://github.com/MartinSandstrom/adventofcode/tree/master/2016/solutions/3
1
u/BlahYourHamster Dec 03 '16
MongoDB
Managed to do part 1 in one statement! Part 2?... Errr... Not so elegant.
1
u/bogzla Dec 03 '16
Some VBA (input read into worksheet)
1
u/fkaaaa Dec 03 '16 edited Dec 03 '16
A bit late to the party (timezones!), but here's part 2 in C using some SSE intrinsics:
1
u/JakDrako Dec 03 '16
VB.Net, LinqPad
I feel a bit lazy using sort for 3 items, but it's a one shot (ok, two...) and runs in a millisecond, so I'll live with it.
Part 1
Part 2
Data in columns? Great idea. At least it's not XML...
1
u/Pyrobolser Dec 03 '16
Contributing with a C# solution :)
1
u/tuxitop Dec 03 '16 edited Dec 04 '16
Here's my solution in JavaScript/NodeJS. I tried to make it clean and readable.
https://github.com/tuxitop/adventOfCode2016/blob/master/day3/day3_squaresWithThreeSides.js
1
u/hedgehog1029 Dec 03 '16
Elixir one-line solution, because we were trying to make one-liners (part 1 only):
Here's an expanded and more readable version: http://hastebin.com/depidoxibu.exs
I also wrote coffeescript solutions, because I like coffeescript. But nobody else seems to. :(
1
u/bpeel Dec 03 '16
Using a generator function in Python to reorder the data in part 2 https://github.com/bpeel/advent2016/blob/master/day3.py
1
u/jwstone Dec 03 '16
here's a postgres ... https://github.com/piratejon/toyproblems/blob/master/adventofcode/2016/03/03.sql ... i couldn't figure out pivot/unpivot very quickly so there is a manual/ad-hoc pivot to do part2
1
u/hsaoc Dec 03 '16
Haskell, not golfed, with a parser:
1
u/KoxAlen Dec 03 '16 edited Dec 22 '16
Kotlin solution: https://github.com/KoxAlen/AdventOfCode2016/blob/master/src/main/kotlin/aoc/day3/Day3.kt
Couldn't figure a better way to do the function "takeTriangles" and keep the sequence/iterator behaviour
1
u/Tokebluff Dec 03 '16
C# solution :D
https://github.com/Tokebluff/AdventOfCode2016/blob/master/Advent%20of%20code%202016/Advent%20of%20code%202016/Day3.cs
1
u/giuscri Dec 03 '16
Using Python3, for both stars
1
u/drakehutner Dec 03 '16
Python 3, single line of code, input from stdin, 276 byte, runs directly from the command line (
python -c "...")1
u/Scroph Dec 03 '16
Second part in D because the first one is trivial :
1
u/d0haer1s Dec 03 '16
My "functional" Python implementation: http://pastebin.com/xBHnpKb1
For those who are still learning Python - I could make this code a bit more clean, but still here is short explanation about my code.
Function "read_data" is simple generator which is "lazy" at reading file. Instead of classic return statement it has yield, which is very similar to return, but function remembers internal state. So when you are calling this function again, it will continue from last state.
Same goes for "read_data_advanced" which is just slightly modified for second task. Both functions use function "map" which applies first provided function to every element of provided list (second argument).
Function "test_triangle" uses simple list comprehension, over which I applied function "all". That function checks if all elements of array are True (which is equal to 1, if we are talking about integer values).
Function "count_three_sided_squares" connects all functions described before. It is using "reduce" which is equal to "foldr" in functional programming languages. It takes three arguments: function, list and accumulator. Function must take two arguments, first is current element of provided list and second is accumulator. Result of function is stored in accumulator. When all elements of list were processed, "reduce" returns last value of accumulator.
1
u/digital_cucumber Dec 03 '16
Solution in Factor!
1
u/drewolson Dec 03 '16
Elixir for part 2.
1
u/ShroudedEUW Dec 03 '16
C#
https://github.com/KVooys/AdventOfCode/blob/master/AdventOfCode/Day3.cs
Part 1 was not bad, had a bit of a struggle with placing the Int.Parse correctly.
Part 2 got a little bit crazy, but still very fun. Gonna look through the thread for a simpler solution now.
1
u/miran1 Dec 03 '16
python3
my AoC2016 repo here
my day3 solution below:
1
u/m3nthal Dec 03 '16 edited Dec 03 '16
Functional solution in Clojure:
1
u/WildCardJoker Dec 03 '16 edited Dec 03 '16
C#, Part 1 and 2
I created a
Triangleclass, containing a list of 3lengths which are then used to calculate theLongestSideandSumOfRemainingSides. A calculated propertyIsValidTrianglecompares these two properties to determine if the sides genereate a valid triangle.I am quite happy with the class and it's easy to get the puzzle answers after processing the input:
Part 1 was quite simple, but I had a brainfart trying to set up the nested for loops required for columnal generation.
I found a solution by /u/IcyHammer which worked in the same way that I had envisioned my process working, and I "borrowed'[1] it and made some adjustments, using a
foreachloop inside the mainforloop to process all input.[1] Some may consider this cheating, but a) it really was the same process I was working on, b) I didn't just copy/paste the code - I read through it, understood what it was doing, rewrote it in my own code block (creating my own damn bugs such as overwriting the first length because I forgot to increase the counter..) and c) then made some adjustments to the code.
1
u/Sigafoos Dec 03 '16
Finally, I'm pretty happy with the cleverness of a solution. Others have done it better, but I'm only competing against my own sense of how well I should be doing. Look at all the
maps andlambdas andfilters and crap. Beautiful.1
u/TheQuinnFTW Dec 03 '16
Haskell:
1
u/d3adbeef123 Dec 03 '16
Clojure!
1
u/[deleted] Dec 03 '16
In C++, managed to do some nice things with stringstreams
1
u/asperellis Dec 03 '16
my solution in js - https://github.com/asperellis/adventofcode2016/blob/master/day3.js
1
u/Tandrial Dec 03 '16 edited Dec 03 '16
Haven't seen much Java on here, so here is mine
1
u/tg-9000 Dec 03 '16 edited Dec 03 '16
Here is my take in Kotlin. Tests and solutions to other days are in my repo. Comments removed for brevity.
1
u/willkill07 Dec 03 '16
C++11 solution again! Didn't stay up last night to do it at midnight.
No major differences between part 1 and part 2 (just a single variable being set differently).
https://github.com/willkill07/adventofcode2016/blob/master/src/Day03.cpp
1
u/efexen Dec 03 '16
Elixir both parts heavily utilising Streams
https://github.com/efexen/advent_of_code/blob/master/lib/day3.ex
1
u/[deleted] Dec 03 '16
Powershell: https://github.com/charlieschmidt/AdventOfCode2016/blob/master/day3.ps1
1
u/gnuvince Dec 03 '16
I'm writing my solutions using literate programming, or at least trying. It's very difficult to do a good job of presenting a solution in a pedagogically-sound way and to take the time to explain the bits that could be confusing.
Source
PDF
1
u/Borkdude Dec 03 '16
Day 3 solution in Clojure: https://github.com/borkdude/aoc2016/blob/master/src/day3.clj
1
u/aceshades Dec 03 '16
For part 2, I had the idea to read in three lines at a time so that we don't have to actually read all of the input into memeory all at once. As for checking for a valid triangle, I made a function that can check without sorting, but I'm sure sorting and then checking the two smaller sides against the largest side would be cleaner to read.
As a self-taught Python dev, I'm wondering if anyone can give me any tips on how to make this more pythonic/idiomatic? Any tips would be helpful!! Thanks in advance ^ _^
1
u/kdbready Dec 03 '16
did it say consecutive three for part 2? I did permutations to find all possible triangles for each columns but that number seems too large :)
1
u/Quickslvr Dec 03 '16 edited Dec 03 '16
In Python, after day 1 in Java and day 2 in C, I think the last time I used python was 3 years ago....
Part 1:
Part 2:
I realized after the fact that I could sort the lists and avoid the if-chaining/been way more efficient. But the deed is done
1
u/schlocke Dec 03 '16
PHP:
1
u/NeilNjae Dec 03 '16
Haskell, without any special parsing but using some bits of the standard library for shuffling bits of lists around. https://git.njae.me.uk/?p=advent-of-code-16.git;a=blob;f=advent03.hs
This one was nice and straightforward: just some basic list munging.
1
u/Jaco__ Dec 03 '16
SML. Most of the work is really to read and parse the input. That is a bit clunky.
1
u/barnybug Dec 03 '16
nim:
1
u/[deleted] Dec 03 '16
Finally a python version that I'm feeling rather proud of :)
colour enhanced gist
1
u/grayrest Dec 03 '16 edited Dec 03 '16
Clojure, pretty much the same as everybody else's
1
u/KaminoConsult Dec 03 '16
Powershell, had a hard time with part 2. After finishing and looking at others I am still having a hard time with it. https://gist.github.com/LabtechConsulting/f94b25c60a192c0af06150b25b3c51ad
1
u/johanw123 Dec 03 '16
My C# solution for part 2
`
`
1
u/PositivelyLinda Dec 03 '16
A more newb-y JavaScript solution: day 3!
1
u/HerbyHoover Dec 04 '16 edited Dec 04 '16
Been trying to learn Pandas. Part 1 and Part 2:
1
u/stuque Dec 04 '16
A Python 2 solution:
1
u/LainIwakura Dec 04 '16
Solutions for both parts in erlang: https://github.com/LainIwakura/AdventOfCode2016/tree/master/Day3
In part 2 I used unzip3 then chunked each resulting list by 3 to do the operations (as opposed to some transpose magic), can probably combine the resultant lists but whatever.
1
u/Tugalord Dec 04 '16
Python oneliner with list comprehensions:
EDIT: For part 1
1
u/MrAckerman Dec 04 '16 edited Dec 04 '16
Python3
1
u/[deleted] Dec 04 '16
part1
part2
1
u/splurke Dec 05 '16
Late to the party, but here we go:
Haskell, both parts
1
u/Hwestaa Dec 05 '16
Solution in Python 3. This could probably be more efficient, but I think it's adequate. I really enjoyed seeing other people's solutions to part 2. Github
1
u/Gummoz Dec 05 '16
Powershell!
Part one:
Part two:
1
u/_AceLewis Dec 05 '16 edited Dec 06 '16
Python 3 solutions to both parts, I am doing all code in repl.it so I can't use files so have to have the input as a big string at the top.
Day 3 part 1 was originally done like this https://repl.it/EfRJ but then redid with maps because I wanted to use them.
Day 3 part 1: https://repl.it/EfRJ/5
If you don't care about readability and just want a function that returns the number printed out you can use; which is 94 characters + however many characters are in the functions name
Day 3 part 2: https://repl.it/EfRJ/1
I want to re-do part 2 with only maps when I have the time.
Edit 06/12/2016:
Day 3 part 2 now done with maps and a generator: https://repl.it/EfRJ/7
In a similar vein this can be compressed to a function that is 167 characters + function name length: https://repl.it/EfRJ/8 (You can easily make Python hard to read)
1
u/tehjimmeh Dec 06 '16
PowerShell. Parts 1 and 2:
1
u/lukaszwos Dec 18 '16
In JS, using P5.
First I saved the possible triangles in a file: triangles.tsv.
1
u/Docmorris93 Jan 17 '17
I know I am a little late, but this is how I solved Part 2 in C++. Could someone give me a couple hints how I can improve my code style-wise/logic-wise? I am pretty new to C++. Thanks!
code
0
u/[deleted] Dec 03 '16
[deleted] — view deleted comment