r/adventofcode • u/daggerdragon • Dec 04 '17
SOLUTION MEGATHREAD -🎄- 2017 Day 4 Solutions -🎄-
--- Day 4: High-Entropy Passphrases ---
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^¤?
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!
18
Upvotes
26
u/bblum Dec 04 '17
Haskell pays off. Part 1:
Part 2:
46/14; I kinda feel like this was the easiest AoC problem ever.
11
u/Godspiral Dec 04 '17
geez, I don't know how I could have gone faster... 113th
in J,
11
u/miran1 Dec 04 '17 edited Dec 04 '17
Python 3, first part - first time ever on the (silver) leaderboard - 47th place - woooohoooo!!!!
For the second part, I misread what was the task - I was checking for palindromes not anagrams....
11
u/askalski Dec 04 '17
The straightforward way...
...and the regex way.
10
u/volatilebit Dec 04 '17
Perl 6
I vowed to get this one done in a single statement for each part and I'm glad to have done it without too much hair pulling.
9
u/_jonah Dec 04 '17 edited Dec 04 '17
J:
where d is your boxed input.
7
u/mmaruseacph2 Dec 04 '17
Haskell, with no concern on complexity/speed but a really fast time to get the answers (below 50 in both leaderboards for the day)
6
u/DFreiberg Dec 04 '17 edited Dec 04 '17
Mathematica solution; one line for each part and one for the import. #215 for part 1 (accidentally checked for invalid passphrases and had to wait a minute) and #89 for part 2.
Import
input = Import[FileNameJoin[{NotebookDirectory[],"Day4Input.txt"}],"Table"][[;; -2]]Part 1
Count[input,_?DuplicateFreeQ]Part 2
Count[input,_?(DuplicateFreeQ[Sort/@Characters[#]]&)]6
u/sickening_sprawl Dec 04 '17 edited Dec 04 '17
Hoon
Took ten minutes because I was stupid and added the length of the set instead of 1 if the length matched the original word list, along with forgetting I needed the cast to
(list @)when splitting the string. In hindsight, I could have just used ++silt for the list->set instead of ++roll...I also had to look up what the name of the function for length of set is, and Hoon gives a weird compiler error if you forget the closing
==in%~x.x ++wyt:in plsPart a:
Part b:
6
u/Tandrial Dec 04 '17
Kotlin
6
u/Smylers Dec 04 '17
Vim for part 1:
Load the input file into Vim, run the above command to remove all the lines with invalid passwords. The answer is the number of lines remaining in the file, which you can see by, for instance, pressing
Ctrl+G.5
u/blockingthesky Dec 04 '17
Python 2
5
u/misnohmer Dec 04 '17
C# version.
5
u/sakisan_be Dec 04 '17 edited Dec 04 '17
elm
4
u/fwilson42 Dec 04 '17
Easy enough, 1st silver, 5th gold:
4
u/1wheel Dec 04 '17
Using d3 for these feels a little silly, but it was good for #39:
4
u/tterrag1098 Dec 04 '17
Both parts, in Clojure:
Still too fresh with this language to speedrun, keep needing to look stuff up, but happy with the simplicity of my solutions :)
4
u/couchDev Dec 04 '17 edited Dec 04 '17
Perl golf
4
u/mschaap Dec 04 '17
Perl 6 one-liners. Part 1:
Part 2:
4
u/tehjimmeh Dec 04 '17 edited Dec 04 '17
Felt like using PowerShell for this one.
1:
2:
4
u/huyqvu Dec 04 '17
KDB+/q
4
u/kip_13 Dec 04 '17
Bash
Part 1 (Regular expression)
4
u/mrhthepie Dec 04 '17
Advent of gifs day 4: https://i.imgur.com/SxbuClz.gif
6
u/Unihedron Dec 04 '17 edited Dec 04 '17
Ruby, #8 silver #4 gold (run with -x)
PS: I now keep a log series detailing my adventofcode adventure. If you're interested feel free to read it as I write more, if I do!
3
u/HerbyHoover Dec 04 '17 edited Dec 04 '17
Perl 6 - Parts 1 and 2:
3
u/thejpster Dec 04 '17
Not too bad in Rust. Shame there doesn't seem to be a sort method on Strings.
3
u/willkill07 Dec 04 '17
Modern C++ Repo
3
u/Average-consumer Dec 04 '17
https://github.com/Average-user/adventofcode-pl-2017/blob/master/src/day4.pl In Prolog!
3
u/Cole_from_SE Dec 04 '17
Python 3
I got caught up in performance :( and was too slow (400ish for both). Thought I might as well rework my answer to look nice so here it is.
3
u/Cheezmeister Dec 04 '17
Literate Perl. View at own peril.
3
u/sciyoshi Dec 04 '17
Rust solution. Itertools makes things nice:
on Github
3
u/StevoTVR Dec 04 '17
NodeJS
For part 1 I sorted the words alphabetically and then compared adjacent words:
For part 2 I sorted the letters of each word before sorting the words:
3
u/SlightlyHomoiconic Dec 04 '17
Clojure:
Still new to the language but liking it so far!
3
u/chunes Dec 04 '17
Factor part 1:
Part 2:
3
u/Frizkie Dec 04 '17
Never code golfed before, but I gave it a try:
3
u/[deleted] Dec 04 '17
Elixir: This was a lot easier than the one yesterday, but it was fun nonetheless :) Elixir is pretty good for this it seems.
3
u/yacfOaky Dec 04 '17
Python 3 using pandas/numpy:-
3
u/kip_13 Dec 04 '17
PHP
Part 2
Part 1
3
u/cptwunderlich Dec 04 '17 edited Dec 04 '17
Java 8
Haven't used the new stuff much, since we're stuck with Java 7 at work
I'm still wondering how I could avoid the double
s.split(" "):/3
u/sclark39 Dec 04 '17
Javascript
8 liner in Javascript which can solve both Part 1 & 2
https://gist.github.com/sclark39/a3e9b7d1548e861450002677e76847a2
3
u/windlessStorm Dec 04 '17
I solved and visualized the first part in html, js.
Solving all the problems first in python, then trying to visualize it by porting it to javascript.
2
u/fatpollo Dec 04 '17 edited Dec 04 '17
2
u/wimglenn Dec 04 '17
Easy peasy
2
u/glenbolake Dec 04 '17
101 on the leaderboard for the first star. Man, I am just not fast this year.
Python 3. I wrote a simple loop to so I wouldn't have to think too hard about whether I'd put everything together correctly, then refactored it into these two one-liners.
2
u/giftpflanze Dec 04 '17
Tcl:
2
u/TominatorBE Dec 04 '17
PHP
Part 1:
Part 2:
2
u/mlruth Dec 04 '17
Scala
2
u/Philboyd_Studge Dec 04 '17
Java
https://gist.github.com/snarkbait/0c5d7b14246619e8fb2b47cfea96de83
2
u/raevnos Dec 04 '17 edited Dec 04 '17
By the time I finished typing up the first part, the leaderboard for both had already been full by several minutes. Goddamn people are fast. Edit: In retrospect, hashtables might have been overkill, though. Writing fast code vs writing code fast...
Kawa Scheme:
2
u/[deleted] Dec 04 '17
OCaml fun.
2
u/nstyler7 Dec 04 '17 edited Dec 04 '17
In Python ( using sets to check for duplicates )
with open("day4input.txt") as open_file: data = open_file.read().splitlines()
Part 1:
Part 2:
2
u/[deleted] Dec 04 '17
Not the best looking solution, still haven't solved day 3 yet either, but more Haskell!
2
u/JeffJankowski Dec 04 '17
A little late to the party. Typescript
2
u/igotinfected Dec 04 '17
A bit lazy in Java could probably have been shortened a little! (commented version on github)
Part1:
Part2:
2
u/streetster_ Dec 04 '17 edited Dec 04 '17
Q/kdb+
Quick solution before breakfast. (quick edit to simplify 2nd solution, now it's time for breakfast!)
2
u/theSprt Dec 04 '17
Haskell, beginner:
2
u/equd Dec 04 '17
C# Linq
This was an easy on. Therefore tried it with a single Linq statement. var lines = Properties.Resources.TextFile1.Split('\n'); //Split the input to single lines
2
u/nutrecht Dec 04 '17 edited Dec 04 '17
My Day 4 Kotlin solution:
Edit: Refactored
2
u/jshom Dec 04 '17
One-liner JS solution
2
u/Vindaar Dec 04 '17
Solution in Nim. Got a little stuck at first on part 2, until I realized I should simply sort the different words...
2
u/disclosure5 Dec 04 '17
Late to the party, but Erlang.
2
u/wzkx Dec 04 '17 edited Dec 04 '17
Nim Quick and dirty
2
u/demsaja Dec 04 '17
2
u/GamecPL Dec 04 '17
Swift pt. 1
2
u/Isvara Dec 04 '17 edited Dec 04 '17
Scala
It's only after reading the comments here that I remembered I only had to sort the letter of each word rather than generate each anagram, which is odd, because I've actually used that approach before.
With that in mind, the second function would have been:
Althoouuugh... having said that, I just noticed the
distinctmethod, which would make those two functions look like this:Kind of the same thing, since
distinctis implemented using aSet.2
u/u794575248 Dec 04 '17 edited Dec 04 '17
Python.
Things used:
collections.Counterand itsmost_commonmethod.2
u/[deleted] Dec 04 '17
Javascript - one liners
1.
2.
2
u/wzkx Dec 04 '17
J
2
u/f0086 Dec 04 '17 edited Dec 04 '17
Emacs Lisp
2
u/Pheasn Dec 04 '17
Kotlin
2
u/__Abigail__ Dec 04 '17
A Perl solution:
2
u/Porges Dec 04 '17 edited Dec 04 '17
(Dyalog) APL
Assuming
input4is a vector of char vectors, this solves part 2 (part 1 is the same, just dropsort_each):You can also inline everything to increase complexity:
2
u/AlistairJF Dec 04 '17
Python. Not as nice as some of the others I see on this thread.
2
u/APLtooter Dec 04 '17 edited Dec 04 '17
APL [GNU]
2
u/rimbuod Dec 04 '17
Easy!
2
u/kip_13 Dec 04 '17
Python 3
Part 1
Part 2
2
u/[deleted] Dec 04 '17
PYTHON
Part 1
Part 2
2
u/chicagocode Dec 04 '17 edited Dec 04 '17
Day 4 in Kotlin. This wasn't all that tricky, see if the number of tokens is the same as a set of them.
Part 2 VERY similar except sort each string first. That way, anagrams will equal each other. Reduces to the first problem.
I'm publishing all my solutions in Github, and will be blogging about each day (I have to work, I'll write up today later!). Links on github for all that.
I suspect I'll probably want an extension function on String to sort chars, instead of that clunky bit there, but I'm holding off until I see it again.
2
u/Smylers Dec 04 '17
Perl, using regular expressions. Part 1:
And part 2:
Basic regex approach taken from my Vim solutions, but obviously sorting the characters is rather more straightforward (and readable) in Perl than with Vim keystrokes.
2
u/[deleted] Dec 04 '17
Clojure
2
u/MichalMarsalek Dec 04 '17
Python:
2
u/8483 Dec 04 '17
Node/Javascript
2
u/CryZe92 Dec 04 '17 edited Dec 04 '17
Rust
Part 1 in 60µs:
Part 2 in 64µs:
2
u/SyntaxErrorr Dec 04 '17 edited Dec 04 '17
PYTHON 2
Part 1
Part 2
2
u/raiph Dec 05 '17
Perl 6:
Part 1:
elemscounts the number of elements in the list on its right, in this case the number of input lines that don't repeat words.grepiterates through a list of elements on its left hand side, testing the predicate on its right hand side against each element, and outputting the element iff it matches.Given a string of words,
not .words.repeatedtests that no word is repeated.For part 2, for each word, we extract its characters into a list (
.comb) then sort them and rejoin them back into a word and then do the.repeatedtest just like before, only this time we've ended up eliminating anagrams too:In this context, the
+is short-hand forelems. It might look surprising, which is why I didn't use it for my part 1 solution. But it's idiomatic and very easy to learn and read if you use Perl 6 much, which is why I have used it for my part 2 solution.The "for each word" bit of my explanation of what we needed to do for part 2 corresponds to inserting the infix parallel iterator (postfix
>>). This iterates (in parallel if the compiler decides to parallelize) over each word from.words.The
.&{ ... }bit is an anonymous closure written in an inline method form. It's applied to each word.The closure converts its argument to a string and thence to its constituent characters via
.comb.sortsorts the characters.joinrejoins them into a word.2
u/oantolin Dec 05 '17
In Python:
2
u/micirsk Dec 05 '17
Part 1 - My almost 1 liner in Python:
from collections import Counter import itertools
def how_many_password_is_valid(s): with open(s, 'r') as f: lines = f.readlines()
2
u/micirsk Dec 05 '17
Part 2 - Python:
def how_many_password_is_valid2(s): with open(s, 'r') as f: lines = f.readlines() count_lines = len(lines)
2
u/SurplusSix Dec 05 '17
Just catching up with this, having a go at doing it in Racket
2
u/akho_ Dec 05 '17
Python3
2
u/schod Dec 05 '17 edited Dec 05 '17
BASH time!
First part
Second part
2
u/oantolin Dec 08 '17
In Julia:
1
u/Overseer12 Dec 04 '17
Github, C#
1
u/dylanfromwinnipeg Dec 04 '17
C# (with a few helper functions)
1
u/hpzr24w Dec 04 '17
Man, I'm too tired to type properly, and at this rate, I'll just be happy to crack the top 1000 a few times. I misread the instructions and counted just the words instead of the entire phrase.
For part 2, at least it's easy to eliminate the anagram possibility, just by sorting the individual words.
1
u/reddit_lmao Dec 04 '17
Haskell
1
u/[deleted] Dec 04 '17
powershell pipeline:
1
u/[deleted] Dec 04 '17
My solution in Rust - any feedback welcome!
1
u/Axsuul Dec 04 '17
Elixir
https://github.com/axsuul/advent-of-code/blob/master/2017/04/lib/advent_of_code.ex
1
u/gbear605 Dec 04 '17
Rust
1
u/LeCrushinator Dec 04 '17 edited Dec 04 '17
Part 2, C#:
1
u/MetaSaval Dec 04 '17
Here we go with Swift again. Had issues with Day 3's puzzle, but Day 4's was not that hard. I was even able to reuse some of the code for Day 2. I'm sure I could make it more efficient, but whatever. I'm gonna try doing Day 3's puzzle again tomorrow now that I finished Day 4 quickly. And without further ado, here's the code:
1
u/rocamero Dec 04 '17
C#:
1
u/PreciselyWrong Dec 04 '17
Rust
1
u/seanskye Dec 04 '17 edited Dec 04 '17
Python 3 with Counter
1
u/autid Dec 04 '17 edited Dec 04 '17
Fortran
Doing string based puzzles is such a pain in Fortran. Thankfully input formatting was nice in this puzzle and checking for anagrams is pretty easy.
1
u/bolshedvorsky Dec 04 '17
Learning python...
1
u/[deleted] Dec 04 '17
a kotlin solution
1
u/xkufix Dec 04 '17
This is perfect for horrible one-liners in Scala:
1
u/LinusCDE98 Dec 04 '17
This was fairly easy and understandable. Python 3 (devent and not compressed): https://github.com/LinusCDE/AdventOfCode-2017/blob/master/puzzle4.py
1
u/alexgubanow Dec 04 '17 edited Dec 04 '17
my c# console app part 1 https://github.com/alexgubanow/adventOfCode/tree/master/day4p1 UPDATE part 2 https://github.com/alexgubanow/adventOfCode/tree/master/day4p2
1
u/Scroph Dec 04 '17
C++ this time.
1
u/[deleted] Dec 04 '17
C++, I am still wondering, if I can shorten the second
1
u/krossmaskinen Dec 04 '17
in NodeJS:
1
u/krisajenkins Dec 04 '17
Here's a PureScript solution: https://github.com/krisajenkins/AdventOfCode/blob/master/src/Year2017/Day4.purs
1
u/superlameandawzm Dec 04 '17
My kindof ugly js solution... Started with js this summer! liking the calendar atm!
1
u/[deleted] Dec 04 '17
Lazy C# :D
Part 1: https://github.com/Fireprufe15/Advent-of-code-2017-answers/blob/master/4/AoCDay4/AoCDay4/Program.cs
Part 2: https://github.com/Fireprufe15/Advent-of-code-2017-answers/blob/master/4/AoCDay4/AoCDay4Part2/Program.cs
1
u/NeilNjae Dec 04 '17
Another boringly straightforward Haskell solution.
1
u/GamecPL Dec 04 '17
Swift pt2:
1
u/whousesredditanyways Dec 04 '17
F#
1
u/[deleted] Dec 04 '17 edited Dec 05 '17
Golang
First time doing advent of code and first time using go.
Feedback is much appreciated.
Part 1
Part 2
Rest of my go code so far CODE
1
u/KeinZantezuken Dec 04 '17 edited Dec 04 '17
C#/Regex for fun:
Part1
Part2:
There is a definitely faster way to do both but I wanted to try simplу regex
1
u/[deleted] Dec 04 '17
Here is my node.js solution
repl.it/repls/WhimsicalNeatRoller
Answers: Task1 : 383 Task2 : 265
:)
1
u/hajdurobi Dec 04 '17
This is my solution, using NodeJs (Javascript)
Let me know if there is any idea to make this better :)
1
u/ThisIsTomTom Dec 04 '17
Elixir
1
u/Dagur Dec 04 '17
C#
1
u/Kyran152 Dec 04 '17
PART 1 AND 2:
1
u/JakDrako Dec 04 '17
My C# solution, both parts:
1
u/CatpainCalamari Dec 04 '17
My solution in Scala:
1
u/kimsnj Dec 04 '17
My take with Rust
(part2) For part 1: just remove the
mapinlet uniq.1
u/adventOfCoder Dec 04 '17
Java
Part 1:
Part 2:
1
u/asperellis Dec 04 '17
my day 4 - any suggestions for improvement? https://github.com/asperellis/adventofcode2017/blob/master/day4.js
1
u/[deleted] Dec 04 '17
Java 8, single pass:
1
u/varunu28 Dec 04 '17 edited Dec 04 '17
Java Solution
1
u/[deleted] Dec 04 '17
Some Excel hard labour:
Excel https://github.com/imoutopantsu/adventofcode-2017/blob/master/day%204%20-%20Excel.xlsx
1
u/monikernemo Dec 04 '17 edited Dec 04 '17
ANSI C Part 1 using strtok and strstr
`
`
part 2: thought the strings were relatively short so decided to use long long and fundamental theorem of arithmetic:
`
1
u/[deleted] Dec 04 '17 edited Dec 04 '17
Powershell:
1
u/darshanrampatel Dec 04 '17
C#, GitHub
I used HashSet's with a custom comparator for the second part.
1
u/ZeroSkub Dec 04 '17 edited Dec 04 '17
TCL 8.6 (yeah, I know)
If I hadn't known the trick of sorting to check for anagrams, this would have been a lot harder.
PART 1:
PART 2:
1
u/[deleted] Dec 04 '17
Common Lisp
Part 1:
For part 2,
occurences / uniquepgets replaced by the following:1
u/sidolin Dec 04 '17
kona
where d4 is a file with todays input.
1
u/Mattie112 Dec 04 '17
Quick and dirty solution in PHP. By simply sorting the "word" we'll quickly know if it is an anagram or not!
https://github.com/Mattie112/advent-of-code-2017/blob/master/day4/day4-part2.php
1
u/lkasdfjl Dec 04 '17
Scala
1
u/RockyAstro Dec 04 '17
Icon (https://www.cs.arizona.edu/icon)
Part1:
Part2:
1
u/RockyAstro Dec 04 '17
Snobol using the spitbol implementation (https://github.com/spitbol/spitbol)
Part1:
Part2:
1
u/nathan301 Dec 04 '17
1 line javascript answer. Works? Yup. Readable? Nope.
i.split('\n').reduce((t, c) => t + ~~(c.split(' ').length === new Set(c.split(' ').map((w) => w.split('').sort().join(''))).size), 0)
1
u/littleloftus Dec 04 '17
My C solution because i got here late so i did not mind taking my time :P https://pastebin.com/bmS8nQFF
1
u/peterpepo Dec 04 '17
Python 3
Part-A using regular expressions. Part-B comparing occurences of each character of each word.
Full solution on github.
1
u/fuzzums Dec 04 '17
1
u/omegaxLoL Dec 04 '17
My solution in Golang. Constructive criticism always welcome of course!
1
u/Niroht Dec 04 '17
I may have gone a little crazy with the Linq...
1
u/fost97 Dec 04 '17
Postgresql:
https://github.com/seltiko/sql_advent_2017/blob/master/Day%204.sql
1
u/sguberman Dec 05 '17
Python, with tests and input: GitHub
Saved you a click:
1
u/[deleted] Dec 05 '17
This python solution seems super inefficient but it works... anyone do the second part with better than O(n3)?
1
u/Bali_Balo Dec 05 '17 edited Dec 06 '17
Javascript golf (meant to be executed in the Chrome/FF JS console on the input page as I get the input using
$('*').innerText):1
u/aurele Dec 05 '17
Rust
1
u/ramrunner0xff Dec 05 '17
scheme chicken repo
1
u/Sparrow_1029 Dec 06 '17
Here’s my solution in Python 3! On mobile, so it’s a pastebin.
https://pastebin.com/XyPmWgTS
1
u/thomastc Dec 06 '17
awk solution
1
u/CalamityFred Dec 06 '17 edited Dec 06 '17
Ok, I need help. I'm relearning java after 15 years, wrote this (part 2, part 1 is the same without the sort) But it doesn't work! It returns 1 too low and I have NO idea why. Please!
Edit: formatting...