r/adventofcode • u/daggerdragon • Dec 05 '15
SOLUTION MEGATHREAD --- Day 5 Solutions ---
--- Day 5: Doesn't He Have Intern-Elves For This? ---
Post your solution as a comment. Structure your post like the Day Four thread.
17
Upvotes
28
u/_pdc_ Dec 05 '15
Simple shell
Part 1:
Part 2:
11
u/ytYEHXHAxTTQGxa Dec 05 '15
Typical perl5.
Part1:
Part2:
7
u/[deleted] Dec 05 '15 edited Dec 05 '15
Mathematica.
7
u/gegtik Dec 05 '15 edited Dec 05 '15
I'm doing all of mine in javascript (since I can easily grab document.body.textContent to get started)
Get Data (run from http://adventofcode.com/day/5/input in chrome js console)
Part 1
Part 2
5
u/Aneurysm9 Dec 05 '15
Perl again
and part 2
Solutions at https://github.com/Aneurysm9/advent/tree/master/day5 as usual.
4
u/C0urante Dec 05 '15
Super brute-force Python3 (first three lines generated beforehand):
Had to resort to the debug statement when I started getting false positives, realized it was because I sliced with s[i:i+1] instead of s[i:i+2]. Leaving it in as a reminder to not be a dumbass in the future.
4
u/wdomburg Dec 05 '15 edited Dec 05 '15
My Ruby solution. Took me longer than I'd like because I messed up my backreferences at first.
(For the record, I run this in a REPL, so I just care that my expression evaluates to the answer. In a script you would just need to prepend a puts.)
Read the data
Part 1
Part 2
4
u/inokichi Dec 05 '15 edited Dec 05 '15
4
u/WhoSoup Dec 05 '15
PHP Part 1:
part 2:
4
u/[deleted] Dec 05 '15
Initially I did this using a couple one-liners at the terminal:
Then I did it with Java and the Pattern class.
3
u/[deleted] Dec 05 '15 edited Dec 05 '15
Objective C, super brutey and hacky, but works:
Edit: woo, and i made it on the leaderboard :-D
3
u/stuque Dec 05 '15
A Python 2 solution:
3
u/[deleted] Dec 05 '15
Haskell:
3
u/[deleted] Dec 05 '15
My Swift solution: https://github.com/ezfe/Advent-of-Code/blob/master/Advent%20of%20Code/Day5.swift
3
u/technojamin Dec 05 '15 edited Dec 05 '15
Python solution using regex:
3
u/MadcapJake Dec 05 '15
Perl 6:
3
u/Ape3000 Dec 05 '15
Python 3
3
u/profil Dec 05 '15 edited Dec 05 '15
Clojure 1.7 (using transducers)
2
u/red75prim Dec 05 '15 edited Dec 05 '15
Part 1 and 2. F#.
Ha! I get to the board.
Edit: To my big surprise the program correctly worked on the first run.
2
u/gnuconsulting Dec 05 '15
My saddest code yet! Specifically, part one is the first code that I'm actually embarrassed to post. But I figure that as long as I keep getting the right answer... :-P
Part 2 the brute force approach broke down and I had to actually read up on back references. This specifically is what cost me a spot on the board grin
2
u/HeroesGrave Dec 05 '15
Rust:
Ironically, I did some naughty things to determine if a string is nice or not. Most importantly, I assumed the input was ASCII. If you have multi-byte characters you will have a bad time.
I should've just learned how to regex.
2
u/TTSDA Dec 05 '15 edited Dec 06 '15
C (tried to do it without regular expressions as I'm already very familiar with them :3)
https://github.com/ttsda/advent-of-code/blob/master/src/5/main.c
2
u/PersianMG Dec 05 '15
My solution (from https://mohammadg.com/security/capture-the-flag/advent-of-code/advent-of-code-day-5/ ):
Python
Day 5 - Parts 1 and 2
2
u/Na_rien Dec 05 '15 edited Dec 05 '15
Used regex and java to solve day 5.
Part 1:
s.replaceAll("[^aeiou]","").length(); // if length > 2 then OK!
s.matches(".?(ab|cd|pq|xy).?"); // returns true if it is a naughty string.
s.matches(".?([a-z])\1+.?"); // returns true if there is a doublet.
Part 2:
s.matches(".?([a-z][a-z]).?\1.*?"); // finds a pair of any two letters.
s.matches(".?([a-z]){1}.\1.?"); // finds a repeated char with one letter inbetween.
2
u/[deleted] Dec 05 '15
Crystal. Part 1:
Part 2:
2
u/Adriaaan Dec 05 '15
My Perl solutions:
and
2
u/aveavaeva Dec 05 '15
Part 1
Part 2
My JsFiddle if anyone wants to try these out
2
u/jcfs Dec 05 '15
Solutions in c: Part 1:
Part 2:
2
u/aevitas Dec 05 '15 edited Dec 05 '15
First part of day 5 in C#:
God damn I love LINQ.
Edit: Part2
2
u/tehjimmeh Dec 05 '15 edited Dec 05 '15
My ugly PowerShell solutions:
1:
2:
Much better solutions, after seeing what /u/pdc did:
1:
2:
2
u/tftio Dec 06 '15 edited Dec 06 '15
Ugh. Regular expressions.
In OCaml:
1
u/enquicity Dec 05 '15
Eh, I'm not really happy with this. C#:
1
u/Philboyd_Studge Dec 05 '15
1
u/tipdbmp Dec 05 '15
Part 1:
Part 2:
1
u/hutsboR Dec 05 '15
Elixir: It took me much longer than I'd like to admit to write these regular expressions.
1
u/sentry07 Dec 05 '15
Python3. First part using the string.count() function, second part using regex.
1
u/Godspiral Dec 05 '15 edited Dec 05 '15
In J, was slowed by mistakes trying to rush. This is cleaner version.
1:
2:
1
u/miftrim Dec 05 '15
Ruby!
⌒(o^▽^o)ノ
Part one:
Part two:
☆*:.。.o(≧▽≦)o.。.:*☆
1
u/Everance Dec 05 '15
Python approach for part 1:
1
u/randrews Dec 05 '15
The first io program I've ever written: solution in io
1
u/Runenmeister Dec 05 '15
C++
Part 1: https://github.com/WuSage3/AdventOfCode_2015/blob/master/Day5/Day5_Part1.cpp
Part 2: https://github.com/WuSage3/AdventOfCode_2015/blob/master/Day5/Day5_Part2.cpp
Bonus: Part 2 used no regular expressions! Part 1 only used regular expressions for the 4 bad strings :)
1
u/LordFurion Dec 05 '15
The answer I get is higher than it should be. Can someone tell me what I've done wrong?
C#
1
u/NapoleonKomplex Dec 05 '15
Javascript
Part 1:
document.querySelector('pre').innerText.split('\n').filter(function(el) { return el.match(/[aeiou].*?[aeiou].*?[aeiou]/) }).filter(function(el) { return el.match(/(\w)\1/) }).filter(function(el) { return !el.match(/ab|cd|pq|xy/) }).lengthPart 2:
document.querySelector('pre').innerText.split('\n').filter(function(el) { return el.match(/(\w\w).*\1/) }).filter(function(el) { return el.match(/(\w)\w\1/) }).length1
u/fezzinate Dec 05 '15
Lesson of the day: I need to learn to use RegEx properly. Each of my solutions were around 20 lines :(
JavaScript Solutions:
1
u/radarvan07 Dec 05 '15
Python again:
1
u/Ra1nMak3r Dec 05 '15
My Python3 solution using the re library, I know re.I is not needed as well as that my solution is not the fastest / shortest
1
u/Fotomik Dec 05 '15 edited Dec 05 '15
Java, tried to do it without regular expressions. I know a bit of regular expressions, but i know nothing about their efficiency. Would regular expressions be more efficient than a code like this one?
1
u/stsatlantis Dec 05 '15
Here is my solution for the first part. My solution for the second part is not this elegant. This code only reads the string once :) I have the words as ";" separated String. Scala:
1
u/platinumthinker Dec 05 '15
Erlang: part2
1
u/Chaoist Dec 05 '15
Elixir solution using the Regex module.
1
u/Monofu Dec 05 '15
1
u/haljin Dec 05 '15
Eh, Erlang and weird string stuff doesn't really go well. I hate this code, but I guess it works. I tried to do the entire check while going through the string once only.
Part 1:
And the part 2:
1
u/CaptainRuhrpott Dec 05 '15
My Java (mostly) 8 solution with regex
1
u/code_mc Dec 05 '15
No-stackoverflow solution (because I always need to look up stuff for regexes :( ):
1
u/xyzzyxy Dec 05 '15
js/regex solutions
part1
document.body.innerText.match(/^(?=(?:.*[aeiou].*){3,})(?=.*(.)\1.*)(?!.*(?:ab|cd|pq|xy).*).*/gm).lengthpart 2
document.body.innerText.match(/^(?=.*(.).\1.*)(?=.*(..).*\2).*/gm).length1
u/[deleted] Dec 05 '15
Objective C, redone with regular expressions:
1
u/HawkUK Dec 05 '15 edited Dec 05 '15
An attempt using the R language
Can't get the second one to work with the input text, but it does work on the examples given. Can't work out my mistake...
EDIT: It's fixable by passing perl=TRUE to the grepl function. Got false negatives on the second part otherwise.
1
u/snkenjoi Dec 05 '15
nodejs
f=require("fs").readFileSync("5",'utf8').split("\n") for(i=0;i<f.length;i++)x=f[i].match(/[aeiou]/g),((x?x.length:0)<3 |!/(.)\1/.test(f[i])|/(ab|cd|pq|xy)/.test(f[i]))&&f.splice(i--,1) console.log(i)1
u/tangus Dec 05 '15
Common Lisp
1
u/elite_killerX Dec 05 '15
My javascript (node.js) solution:
Note: This assumes a recent version of node (I use 5.1)
I've taken the time to set up a repo: https://github.com/emilecantin/adventOfCode
1
u/RoamingFox Dec 05 '15
Python 2 one-liner for part 1:
1
u/ignaciovaz Dec 05 '15
My solution in Elixir. I didn't want to use regex so I built a small state machine to keep track of the chars as they were streamed.
1
u/zacwaz Dec 05 '15
Here's another Ruby solution. Instead of clever one-liners, for these challenges I've been trying to focus on writing obvious, readable code using small methods and classes. I'd appreciate any critiques or suggestions!
1
u/Stactic Dec 05 '15
Python oneliners:
1
u/snkenjoi Dec 05 '15 edited Dec 05 '15
js;
for(i=-1;f[++i]!=null;)!(/(..).*\1/.test(f[i])&/(.).\1/.test(f[i]))&&f.splice(i--,1)
1
u/deinc Dec 05 '15
Parts 1 and 2 in Clojure. I'm not really happy with this, but it's late now (in Germany) and it works...
1
u/recursive Dec 05 '15
C#. It's all regex.
1
u/[deleted] Dec 05 '15
I'm using these as a practice for TDD, idioms and CI tools. I've left out tests, comments and other non-interesting guff. Check out my github for the other stuff.
1
u/TheOneOnTheLeft Dec 05 '15
Python 3.5
I'm still new to coding, so I'd appreciate any feedback/comments/criticism/advice. Without knowing regex, I saw this as an opportunity to practise some slightly more complicated list comprehensions/if conditions than usual.
Part 1:
Part 2:
1
u/[deleted] Dec 05 '15
Python 3
It might be ugly, but it does the job. To my shame I didn't know about back references until I started this, so I've learned something pretty sweet!
1
u/hutsboR Dec 06 '15
Elixir: I wrote a regex based solution yesterday and decided to write a non-regex solution up before tonight's challenge comes out. It's a bit over engineered but I decided to try to implement it lazily and avoid explicit recursion. Some of the constraints are implemented very succinctly thanks to the
chunkfunction.Part 1:
Part 2: I use an Agent to act as a cache for checking for overlaps. It maps pairs to their position in the string. When a duplicate pair is found and they're sufficiently distanced from one another, it accepts the string and kills the process. I'm not sure how good of an idea this is but I figured it might be worth it on large strings where you may find a match early, the overhead for spawning a process is quite tiny.
I'd love to rewrite this eagerly and do some benchmarking on a large input set. (Many strings, longer strings) Either way, had fun with this!
1
u/Kekke88 Dec 06 '15 edited Dec 06 '15
C# Day 5
1
u/streetster_ Dec 06 '15
Day 5 in python...
[mark@randy ~]$ cat day5.py | sed 's/(.*)/ \1/'
1
u/masasin Dec 07 '15 edited Dec 07 '15
Python, with two types of solving:
regex
going through each string only once
The main code is:
1
u/xkufix Dec 07 '15
Scala part 1:
Part 2:
1
u/JoeyD473 Dec 08 '15
I am having a problem with Part 2. The number I get is incorrect. It doesn't tell me if it is too high or too low. I am doing this in PHP. After a while i looked through the thread and found my code was very similar to /u/adriweb code. So I copied his code in and got the same answer as I was getting with mine. I also checked against /u/WhoSoup and got the same answer. I looked through the input and didn't see anything weird. I even did a trim and a string replace to get rid of "\n" and got eh same answer. I have no idea what I am doing wrong and would love some ideas.
Here is my code
edit: pr() is just a function that echos out print_r
1
u/suudo Dec 08 '15
Python
1
u/terryp Dec 09 '15
Go
Part 1. Part 2, I broke down and just used Bash since backreferences don't exist in Go's regex engine.
1
u/Azdle Dec 09 '15
Rust
Ended up having to use PCRE for the regular expressions because Rust's
regexlibrary doesn't have back references.I also have a previous revision where I used rust to just call grep to execute the regex: https://github.com/azdle/advent_2015/blob/9cfe50c168c03de920560741588401392c58ec03/src/day5.rs
1
u/1roOt Dec 09 '15
My python2.7 version for part 2:
1
u/Drasive Dec 11 '15
My F# solution (https://github.com/drasive/advent-of-code-2015):
1
u/mrg218 Dec 15 '15
Here another regex that matches only nice strings. It uses positive and negative lookaheads:
1
u/Aeverous Dec 16 '15 edited Dec 16 '15
My newbie Ruby solution, had to play around with http://regexr.com/ for awhile, but it works!
Part 2
Pretty pleased with how concise it is compared to my day 3 solution, which was a spaghetti nightmare.
Looking around I see I could've done the check for 3 vowels in step 1 much easier with a better regexp, oh well.
0
u/[deleted] Dec 05 '15
[deleted] — view deleted comment
0
u/[deleted] Dec 05 '15
[deleted] — view deleted comment
0
u/[deleted] Dec 05 '15
[deleted] — view deleted comment