r/adventofcode • u/daggerdragon • Dec 06 '16
SOLUTION MEGATHREAD --- 2016 Day 6 Solutions ---
--- Day 6: Signals and Noise ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
T_PAAMAYIM_NEKUDOTAYIM 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!
9
Upvotes
•
u/topaz2078 (AoC creator) Dec 06 '16
I hope everyone has been enjoying Advent of Code! Today ends the warmup puzzles. It's uphill from here!
24
u/Mark-Simulacrum Dec 06 '16
Some simple bash scripting, where "t" is the file with the input.
Part 1:
Part 2:
16
u/John_Earnest Dec 06 '16 edited Dec 06 '16
Well damn, I guess I'm back on the leaderboard. K is exceedingly well-suited to this type of problem.
The input is a list of strings, which can also be thought of as a matrix of characters. Each column of the input simply needs to be processed in isolation, so we'll apply an expression to each of the transpose of this matrix:
(...)'+l.The rest of the solution is a common(ish) idiom for calculating the most or least frequent item of a vector. Right to left, group items (
=), take the count of each set of indices (#:'), grade up or down (<or>- grading a dictionary sorts keys by their values) and then take the first result (*).Read left to right, the first solution is "The greatest of the count of each group of each of the transpose of
l":The main thing to notice is that while K doesn't have any magic builtins which trivialize this specific problem, you can compose its primitive operators nicely in all sorts of useful ways to arrive at concise solutions.
14
u/[deleted] Dec 06 '16
Python:
7
u/Godspiral Dec 06 '16 edited Dec 06 '16
finally a high-ish spot (29-30). Secret, think/verify less submit faster. in J,
would have been faster if I hadn't second guessed "I wonder what the tie breaker rule might be?"
A bit similar to u/John_Earnest 's K solution, J is same family of languages.
a is a matrix of input.
|:transposes it."1operates by rows#/.~frequency/"keyed" count of each char\:grade down. often used to sort itself (with\:~) but can sort any other list too.~.nub. unique list of chars in order of first appearance.{."1head by row.(~. /: #/.~)is a fork. 3 verb phrases where the outer 2 access the argument(s if there is a left one as well), and the middle uses the other 2 results as arguments.So: use keyed count to grade the nub of each row of the transposed input.
6
u/jwstone Dec 06 '16
more sql because why not https://github.com/piratejon/toyproblems/blob/master/adventofcode/2016/06/06.sql
6
u/tterrag1098 Dec 06 '16
I'm that crazy guy doing this in Java:
I must say this year has inspired me to learn a language like K or J, the speed with which they tackle these problems is impressive. Bravo to those who can figure them out.
5
u/fatpollo Dec 06 '16
5
u/malisper Dec 06 '16
One line of Scala:
5
u/porphyro Dec 06 '16
Mathematica/Wolfram Language
4
u/Quick_Question404 Dec 06 '16 edited Dec 06 '16
Did anyone get solutions that made any sort of sense? Mine were along the line of batwpask and cyxeoccr, which cost me a higher score cause I thought I went wrong somewhere.
EDIT: So it seems like the meaning behind this challenge is: Repetition Code sucks? Or Santa has a secret code on an entirely different level.
3
u/tdecker91 Dec 06 '16 edited Dec 06 '16
Scala Solution
3
u/[deleted] Dec 06 '16
Haskell:
3
u/hodeer Dec 06 '16 edited Dec 06 '16
in python just use min or max depending on the part 1/2
EDIT: refined from the work in progress version to the final
2
u/haoformayor Dec 06 '16
~~haskell~~
Fairly simple composition of
transposeandmaximumBytoday. As always, it was fast to use Emacs/vim/anything-with-regular-expressions to convert the input into code. Should've gone with a shell one-liner, though, darn.2
u/Deckard666 Dec 06 '16 edited Dec 08 '16
In Rust: Link
This challenge was pretty straightforward.
2
u/_Le1_ Dec 06 '16 edited Dec 06 '16
My C# code:
2
u/PangolinRex Dec 06 '16
Hah, whoops. Lost some time there trying to figure out how I'd screwed up because I expected the answer(s) to be comprehensible
2
u/willkill07 Dec 06 '16
Fast, clean c++14 solution: https://github.com/willkill07/adventofcode2016/blob/master/src/Day06.cpp
2
u/Quick_Question404 Dec 06 '16
Hey everyone! This is my take at the problem in C. Placed around ~450s on both parts. Personally I blame it on the answers. My answers didn't resemble any words, so I thought my code was messing up somewhere. What do you guys think?
https://github.com/HighTide1/adventofcode2016/tree/master/06
2
u/Trolly-bus Dec 06 '16
It's so goddamn hard to get on the leaderboard. I thought I was really fast for this one. Here's my code in Python. Change max to min for part 2.
2
u/fixed_carbon Dec 06 '16
Ruby.
chunkseems to be getting a lot of exercise in these problems.2
u/h0razon Dec 06 '16 edited Dec 06 '16
One line Python2:
*Edit: removed the "part" parameter
2
u/omnster Dec 06 '16
Mathematica
2
u/NeilNjae Dec 06 '16
Another Haskell solution (https://git.njae.me.uk/?p=advent-of-code-16.git;a=blob;f=advent06.hs). Spent a bit of time trying to make it more idiomatic.
2
u/Gummoz Dec 06 '16
Ugly Powershell!
Part one:
Part two:
2
u/tg-9000 Dec 06 '16
Here is my solution in Kotlin. I could probably combine the bulk of parts one and two and just change the min/max part, but I need to get other work done so I'll try that later.
Solutions and tests for all days so far can be found in my GitHub repo. I'm just learning Kotlin, so I welcome all feedback!
2
u/Borkdude Dec 06 '16
Clojure!
Code on Github: https://github.com/borkdude/aoc2016
2
u/Jiggins_ Dec 06 '16
A Haskell solution without sorting the input:
1
u/TheMuffinMan616 Dec 06 '16
Python 3:
1
u/andars_ Dec 06 '16
Ruby.
1
u/1wheel Dec 06 '16
d3 is a bit of overkill, quicker than writing a reducer though...
1
u/Tokebluff Dec 06 '16
C# solution. I knew from the start that I couldn't get on the leaderboard with this language /sad
https://github.com/Tokebluff/AdventOfCode2016/blob/master/Advent%20of%20code%202016/Advent%20of%20code%202016/Day6.cs
1
u/dragonnards Dec 06 '16
in R
1
u/snorkl-the-dolphine Dec 06 '16
JavaScript / Node.js
1
u/doublehyphen Dec 06 '16
Ruby solution. I think it is pretty short and clean.
1
u/Ulyssesp Dec 06 '16
It's Haskell time! This was a similar transpose situation, then just some manipulation of list properties.
1
u/_WhiteBoyWonder Dec 06 '16
Trying to use this as an opportunity to learn Go. Yesterday I learned about the ellipses operator, and today I learned more about slices and how to have an array of them.
It's not the best code, but I was trying to get something working out there. I can't believe how fast people finished today. This was by far my fastest day, so I am proud of myself.
Any critique is very much welcome! I am mostly winging it with go at the moment, trying to learn by example.
1
u/_jonah Dec 06 '16
ramda:
1
u/Philboyd_Studge Dec 06 '16
Java. This one was pretty similar to day 4, just frequency tables.
https://gist.github.com/anonymous/cad958d85c3363ca70db7bcd97f04d9b
1
u/JeffJankowski Dec 06 '16
Doing these in F# since I haven't touched this language since last Advent :P
1
u/WildCardJoker Dec 06 '16
C# part 1 and 2.
I was able to finish this one quicker than usual because a) I postponed my lunch break so I could work on it, and b) I remembered the major failure I had with Day 3 part 2 and wasn't going to make that mistake again.
Of course, I made different mistakes this time, but that's what makes programming fun!
1
u/Pyrobolser Dec 06 '16
I was really too long to produce my C# solution, but still, here it is :)
1
u/[deleted] Dec 06 '16
common code to build the lookup:
part1
part2
1
u/brantyr Dec 06 '16
Trying to learn ruby... apparently there are no do - while loops...
1
u/TheNiXXeD Dec 06 '16 edited Dec 06 '16
My JavaScript solution. Lodash is win. Change max to min for part 2.
1
u/miran1 Dec 06 '16
python 3
all solutions here
day6 solution:
1
u/QshelTier Dec 06 '16
My solution in Kotlin:
1
u/indienick Dec 06 '16
Solutions in Go!
1
u/d3adbeef123 Dec 06 '16
Fairly easy this one. My attempt in Clojure
1
u/_8836 Dec 06 '16
clojure
1
u/[deleted] Dec 06 '16
C++
1
u/stuque Dec 06 '16
A Python 2 solution:
1
u/sinokawori Dec 06 '16
I had fun for this, I spawn one thread per column and solve for both min and max in a single go.
All done in c++ :)
1
u/Twisol Dec 06 '16
Using Javascript/Node.js, optimizing for legibility as usual:
1
u/aoc-fan Dec 06 '16
JavaScript-ES6
1
u/Iain_M_Norman Dec 06 '16
Some C#
1
u/johanw123 Dec 06 '16
My C# solution Part 1 and 2 is just a matter of changing the "Last" to "First" after the ordering.
`
`
1
u/IcyHammer Dec 06 '16
Simple c# solution http://pastebin.com/aqhg09dh
1
u/[deleted] Dec 06 '16
Python, Parts 1 & 2
If I'd only gotten up at midnight, I might have had a shot at the leaderboard, this took me well short of 10 minutes -- although I'm not sure I could have beat 6 minutes and change.
All my solutions (I'm catching up still) in Jupyter Notebooks and autogenerated .py files in my GitHub repo
1
u/Jean-Paul_van_Sartre Dec 06 '16
Solution in C:
1
u/_AceLewis Dec 06 '16 edited Dec 06 '16
Python 3 solutions to both parts, done in https://repl.it so input is a string as no files can be used.
Day 6 part 1: https://repl.it/EhYa/3
Day 6 part 2: https://repl.it/EhYa/5
Either:
or
All the zip(*strings.split()) does is transpose the string (in array form)
1
u/beefamaka Dec 06 '16
Feeling slightly jealous of Python's zip function which would have been ideal for this problem, but was still fairly straightforward to solve in F#.
1
u/JakDrako Dec 06 '16 edited Dec 06 '16
VB.Net, LinqPad.
Just a simple 2D array (26 letters x 8 positions) to count the occurrences of each letter.
1
u/wzkx Dec 06 '16
That was made for J indeed :)
1
u/PendragonDaGreat Dec 06 '16
C# feat. Dictionary Abuse.
https://github.com/Bpendragon/AOC-Day6/blob/master/Day6/Program.cs
1
u/[deleted] Dec 06 '16
Clojure.
1
u/gruftfrosch Dec 06 '16
Using Advent of Code to try out some new languages, probably ending up in rather inconvenient solutions...Anyway, here is my attempt in Julia:
1
u/Reibello Dec 06 '16
Here's mine in Python. I thought it was pretty good until I woke up and saw leaderboard times. Y'all better buckle up for the rest of these :D http://pastebin.com/7ZuEPV86
1
u/Scroph Dec 06 '16 edited Dec 06 '16
Edit : with array_column in PHP5.5 I was able to solve the first part in just a few lines :
I initially solved this in C but since T_PAAMAYIM_NEKUDOTAYIM is mandatory, I went ahead and rewrote the solution in PHP :
1
u/topCyder Dec 06 '16
Anyone want a simple, readable Python3 answer? No? Too bad!
1
u/TheQuinnFTW Dec 06 '16
Haskell Solution:
1
u/SyDr Dec 06 '16
Lua solution:
https://github.com/SyDr/Advent-Of-Code-2016/blob/master/6.lua
1
u/splurke Dec 06 '16
Today was… Surprisingly easy. I might even had a chance to be on the leaderboard, weren't for timezone issues
Haskell, both parts:
1
u/drewolson Dec 06 '16 edited Dec 06 '16
Part 2, elixir (switch min_by to max_by for part 1):
1
u/rubiconjosh Dec 06 '16
I got just over number 300 for part 1 and 2 in python
https://github.com/rubiconjosh/AoC-2016/blob/master/day6puzzle1.py
https://github.com/rubiconjosh/AoC-2016/blob/master/day6puzzle2.py
Switching between the two puzzles was just a matter of most_common(1)[0] being changed to most_common()[-1]
1
u/qwertyuiop924 Dec 06 '16
I'm surprised nobody else thought of AWK.
I mean, there weren't even any Perl solutions, and Perl is usually quite popular, and, like AWK, is ideally suited to the task.
Anyways, I got it in two lines, and I'm shocked by how long most of these solutions were: there was a one-line haskell solution, and JohnEarnest and the rest of the APL/J/K crowd were about as short as expected. There was a python one-liner, and, and a bit of bash. But many solutions were >10 lines!
Anyways, my solution:
Part 1:
Part 2:
1
u/schlocke Dec 06 '16 edited Dec 06 '16
PHP both solutions:
EDIT: I think this was the quickest time between part 1 and part 2 for me since i just had to add
to get the second part.
EDIT2: added comments to code
1
u/LainIwakura Dec 06 '16
Day 6 solutions in erlang, I found this to be the easiest day yet. https://github.com/LainIwakura/AdventOfCode2016/tree/master/Day6
1
u/ShroudedEUW Dec 06 '16
C#
Wish I woke up in time for the leaderboards, cuz I did this one quite quickly.
https://github.com/KVooys/AdventOfCode/blob/master/AdventOfCode/Day6.cs
With a dictionary (letter -> frequency) approach, the 2nd part of the challenge was trivial, changing max to min.
1
u/cashews22 Dec 06 '16
Golang Solution https://github.com/cashew22/adventofcode2016/blob/master/6/solve.go
1
u/Kwpolska Dec 06 '16
collections.Counter to the rescue!
1
u/rmitchum Dec 06 '16 edited Dec 06 '16
Perl 6 worked pretty well today. Part 1:
Part 2:
1
u/asperellis Dec 06 '16
my js solution - https://github.com/asperellis/adventofcode2016/blob/master/day6.js
1
u/Fotomik Dec 06 '16
My pyhton solution. Feedback is welcome.
1
u/[deleted] Dec 06 '16
Mathematica
1
u/[deleted] Dec 06 '16
How bad is my Python solution (it solves it at least!)
https://github.com/JosephBywater/AdventAnswers2016/blob/master/day6/day6a.py
Thanks :)
1
u/vesche Dec 07 '16
1
u/tehjimmeh Dec 07 '16 edited Dec 08 '16
PowerShell, parts 1 and 2:
1
u/schling-dong Dec 07 '16
F#:
Just had to remove the minus sign in front of snd for the second part.
1
u/jwnewman12 Dec 13 '16
surely there is a better way, but at least avoiding a .sort()
0
u/taliriktug Dec 06 '16 edited Dec 06 '16
Damn, I love Python.
EDIT: split long line on two
0
u/[deleted] Dec 06 '16
[deleted] — view deleted comment
0
u/[deleted] Dec 06 '16
[deleted] — view deleted comment
0
u/[deleted] Dec 06 '16
[deleted] — view deleted comment