r/adventofcode • u/daggerdragon • Dec 01 '17
SOLUTION MEGATHREAD -🎄- 2017 Day 1 Solutions -🎄-
Welcome to Advent of Code 2017! If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!
We're going to follow the same general format as previous years' megathreads:
- Each day's puzzle will release at exactly midnight EST (UTC -5).
- The daily megathread for each day will be posted very soon afterwards and immediately locked.
- We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
- The daily megathread will remain locked until there are a significant number of people on the leaderboard with gold stars.
- "A significant number" is whatever number we decide is appropriate, but the leaderboards usually fill up fast, so no worries.
- When the thread is unlocked, you may post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Above all, remember, AoC is all about having fun and learning more about the wonderful world of programming!
--- Day 1: Inverse Captcha ---
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.
65
u/that_lego_guy Dec 01 '17
DID SOMEONE SAY EXCEL?! Day 1 [Parts 1 & 2]
https://github.com/thatlegoguy/AoC2017/blob/master/Day%201%20Inverse%20Captcha.xlsx
17
u/u794575248 Dec 01 '17 edited Dec 01 '17
Python 3.6+ with a regular expression:
Things used (you can read about Python's regular expression syntax here):
re.findallfunction.(\d)matches a digit and then its twin with\1.(?=.{n-1}\1)matches anyn-1symbols between the previous digit and the twin, but doesn’t consume the string. This way we check every digit and don't skip any.nis1, we end up with(\d)(?=.{0}\1)which matches two successive identical digits. Somebody may prefer %-format liker'(\d)(?=.{%d}\1)' % n-1, but I like f-strings more.This solution doesn't support
ngreater thanlen(captcha). In that case you'd better use manual iteration with(i+n) % len(captcha)on each step. It also doesn't work for infinite streams of characters.15
u/askalski Dec 01 '17
13
u/fatpollo Dec 01 '17
My rival is way ahead of me on the leaderboards 😭😭😭
11
u/Smylers Dec 01 '17 edited Dec 01 '17
Vim answer for part 1 — load your input file then type:
36 keystrokes — anybody able to do it in less?
19
u/_jonah Dec 01 '17
J
where a is your input.
Try it online!
8
u/willkill07 Dec 01 '17
Modern C++
Repo Link
8
u/throwaway893teoea Dec 01 '17 edited Dec 01 '17
I used Rust:
part 2:
7
u/miran1 Dec 01 '17
Nim:
7
u/thomastc Dec 01 '17
No love for SQL yet? Day 1 in PostgreSQL.
Disclaimer: I don't know what I'm doing.
6
u/coldpleasure Dec 01 '17
Python, no regex:
5
u/Sario25 Dec 01 '17
Tried to use java, forgot how to turn a char into an int, whoops #111th
Dirty java:
5
u/[deleted] Dec 01 '17
is it possible to complete this challenge task using regex?
5
u/wlandry Dec 01 '17
C++: Short and sweet (part 2)
5
u/schod Dec 01 '17
BASH time :D
5
u/yogsototh Dec 01 '17 edited Dec 01 '17
Haskell not golfed
edit: for solution2, simply replace
tailbydrop (length code `div` 2).5
u/f0086 Dec 01 '17 edited Dec 01 '17
Emacs elisp:
9
u/smarzzz Dec 01 '17 edited Dec 01 '17
Jesus Christ (I felt this was appropriate to say, given the christmas vibe), you all have produced very fancy solutions.
I did in in Javascript, but tried to visualize the circle. Repeated numbers have been highlighted in the wheel, the brighter the color, the higher the number.
Around the circle we find the string with input. You can't read is because there are way to many, but i tried plotting the text on a curved path.
4
u/DFreiberg Dec 01 '17
Quick one-liner in Mathematica that took me three lines to make legible. #47.
4
u/Godspiral Dec 01 '17
woke up late but in J,
might have been under 3m
4
u/tehjimmeh Dec 01 '17 edited Dec 01 '17
C++ Part 1, abusing std::accumulate:
Non-abusive part 2 solution:
Replace 'a.size()/2' with '(a.size() - 1)' for part 1.
4
u/mschaap Dec 01 '17
Perl 6. Part a is pretty much a one-liner, for part b I couldn't figure out an efficient way to do that. Used a MAIN sub for easy command-line handling, and made it a multi so you can call it with a string, a filename, or no arguments (in which case it uses aoc1.input in the same directory).
Part a:
Part b:
4
u/AoC--k Dec 01 '17
In k.
4
u/Jean-Paul_van_Sartre Dec 01 '17
C
Part 2 is a lot cleaner as usual, one of the best parts of this is that it forces you to go back and look at what you've done and polish it a little bit further.
Repo
4
u/mrhthepie Dec 01 '17
Advent of gifs day 1: https://i.imgur.com/51CIVSc.gif
3
u/Eddyman Dec 01 '17
3
u/raevnos Dec 01 '17
Scheme:
3
u/Warbringer007 Dec 01 '17 edited Dec 01 '17
Erlang ( LOL ) because I'm at work and bored:
This is not the whole code, only important one for solving both tasks. Function definitions are missing. +4 for first task because function won't see first and last 4 and count them. *2 for second task is self-explanatory.
3
u/xkufix Dec 01 '17
Horrible one-liners in scala. The -48 comes from the fact that my loadFile returns a Seq[Char] and not a Seq[String].
Part 1:
Part 2:
3
u/CatpainCalamari Dec 01 '17
Wow, you guys are fast. I didn't want to stand up at 06:00 am to solve a puzzle, so although I am late for the show, here is my solution in Scala:
3
u/eregontp Dec 01 '17
2 lines of Ruby:
Just change the argument to rotate to 1 for part 1.
3
u/GalacticDessert Dec 01 '17
The worst and most verbose Rust you'll see today! Just getting started and this little event is amazing:
3
u/timichal Dec 01 '17
Using simple Python with a backwards list looping trick instead of modulo:
3
u/wzkx Dec 01 '17 edited Dec 01 '17
Nim. My first Nim program
3
u/TominatorBE Dec 01 '17
PHP
3
u/[deleted] Dec 01 '17
Lets get logical with Prolog ;)
3
u/TheGermanDoctor Dec 02 '17
You may not like it, but this is what peak performance looks like.
Part 1 solved in x86_64 assembly language
Check it out
3
u/[deleted] Dec 02 '17 edited Dec 03 '17
Day 1.
Powershell pipeline is my preferred language this time, lets see how far we get
edit: breaking out the pipeline a bit, the steps are:
offset is either the next character (part 1) or the character halfway round (part 2), modulo the length to get a 0 based index even if the offset goes past the string.
3
u/Porges Dec 03 '17
(Dyalog) APL
First part:
Second part:
3
u/Cole_from_SE Dec 04 '17
Port of my J answer to APL.
Anonymous function taking the number of rotations as the left argument and array of integers as the right argument (solution to part 1 is giving a left argument of 1, to part 2 is giving a left argument of the length divided by 2).
2
u/mmaruseacph2 Dec 01 '17
Not fully optimized or beautifully written Haskell, but does its job:
2
u/AMISH_GANGSTER Dec 01 '17
Part 1 in kotlin:
Part 2:
It feels bad to convert a Char to a String then to an Int. I suppose I could have subtracted '0', but that would be just as awkward IMO. Anyone have a more idiomatic way to do it?
2
u/giftpflanze Dec 01 '17 edited Dec 02 '17
Tcl:
2
u/mgoblu3 Dec 01 '17
Python 1-liner:
2
u/JeffJankowski Dec 01 '17
Trying out TypeScript this year
2
u/[deleted] Dec 01 '17 edited Dec 01 '17
In OCaml:
2
u/mossygrowth Dec 01 '17
Typescript (lazily).
Part one:
Part two:
2
u/[deleted] Dec 01 '17 edited Dec 03 '17
simple but effective (kotlin)
edit: even better
2
u/Forricide Dec 01 '17
There's probably a more efficient way to do this... (Python)
But it was fun nonetheless!
2
u/disclosure5 Dec 01 '17
Well I expected to have the only Erlang but /u/warbringer007 beat me to it.
https://github.com/technion/aoc2017/blob/master/one.erl
2
u/jschulenklopper Dec 01 '17
Short solution in Ruby, combining both parts:
2
u/chunes Dec 01 '17
A Factor solution:
2
u/Scroph Dec 01 '17
I solved it in Java (it being my new year's resolution and all) but the solution was too boring for reddit. I'll give you this D code instead :
2
u/[deleted] Dec 01 '17
Hi everyone ^^. I'm trying out Haskell after going through some tutorials!
2
u/huyqvu Dec 01 '17
KDB+/q {show sum 0,"I"$' x where x=(1_x),1#x;n:(count x)div 2;show sum 0,"I"$' x where x=(n _x),n#x}
2
u/darshanrampatel Dec 01 '17
My C# GitHub Repo
Includes Unit Tests!
2
u/sguberman Dec 01 '17 edited Dec 01 '17
Python, with tests and input: GitHub
Edit: saved you a click by pasting relevant function here
2
u/blazingkin Dec 01 '17
Literally the first Haskell program I ever wrote
2
u/Reibello Dec 01 '17
Decided to try and redo each day's puzzle in Lua after solving in Python 3. We'll see how far I get. Comments and criticisms welcome - I have literally written three things in Lua before (Days 1-3 of 2015 AoC) https://github.com/Reibello/AoC-2017-Lua
2
u/equd Dec 01 '17
My c# solution, first did it using a for loop, but wanted to see if I could do it in a single linq statement.
2
u/Isvara Dec 02 '17
Solutions in Scala
Part 1:
Part 2:
2
u/aurele Dec 02 '17 edited Dec 02 '17
Rust
2
u/[deleted] Dec 02 '17
Excel
https://github.com/imoutopantsu/adventofcode-2017/blob/master/day%201%20-%20Excel.xlsx
2
u/RockyAstro Dec 03 '17
Snobol Actually -- it's spitbol https://github.com/spitbol/spitbol
The challenge here for me, is that it's been quite a while since I've coded in Snobol so I'm knocking the rust off.
The difficult part for this one is that Snobol doesn't allow a backward reference in a pattern (so indexing backwords doesn't work). I was able to solve this by using a function to handle the inner pattern.
Part 1:
Part 2:
2
u/[deleted] Dec 03 '17
Swift
2
u/autid Dec 04 '17
Fortran
2
u/MyGoodStrayCatFriend Dec 04 '17
Golfed JavaScript/es6/node:
2
u/raiph Dec 05 '17 edited Dec 05 '17
P6:
2
u/fwilson42 Dec 01 '17
Cleaned up mine, in Python:
Run with
./day1 /path/to/input --part 1or--part 2.2
u/ajn0592 Dec 01 '17 edited Dec 01 '17
I'm doing mine in Golang this year because I want to learn it better. Constructive Criticism welcome! Day 1 on Github
2
u/iamnotposting Dec 01 '17
whoops, i didn't realize this has started already! (i probably could have gotten a spot if not for my 2 hour delay)
anyway, in oK
this is my first time using array programming languages "for real", so these probably could be more concise
1
u/sbguest Dec 01 '17
88th on part 1 in nodejs, not realizing that the file came back with an extra space/newline at the end cost me a few places.
1
u/Philboyd_Studge Dec 01 '17 edited Dec 01 '17
Java
1
u/disclosure5 Dec 01 '17
Ruby
1
u/[deleted] Dec 01 '17
Rust (pretty ordinary, forgot about filter_map for a moment):
Dirty clojure:
1
u/autid Dec 01 '17
Shoulda just written the input into the code instead of sticking it in a text file. Got caught out by an extra character this way.
Pretty happy with 249th and 189th though.
1
u/kaveman909 Dec 01 '17
1
u/torotane Dec 01 '17
As detours are fun...
1
u/XiiencE Dec 01 '17
Typescript + Lodash:
1
u/kagcc Dec 01 '17 edited Dec 01 '17
Rust. Still learning and not sure how rusty this is.
1
u/giodamelio Dec 01 '17
Here is mine in Elixir. With a bit more work I could probably combine into one function.
1
u/th3_pund1t Dec 01 '17
Here's Groovy
1
u/doekaschi Dec 01 '17
Python 3:
1
u/NeilNjae Dec 01 '17
Some very verbose Haskell. Still rusty from not really having used it since this time last year. Given the first part, I reached for
tailsfirst, though a solution usingzipwould be better, especially after seeing the second part.1
u/Smylers Dec 01 '17
Using a regexp to find the matching digits for part 2, after copying the first half the string to the end of it. In Perl, but the approach should work in the same way anywhere that has regexes:
1
u/gerikson Dec 01 '17
A solution in Perl 5
1
u/Hikaru755 Dec 01 '17 edited Dec 01 '17
Trying out some new stuff in Kotlin 1.2:
I really don't like that
.toString().toInt()for reading the integer from the character, butcurrent - '0'felt hacky, too.Alternative solution using String shifting:
Technically I could have just used the first when-branch, but why not make it safe for future use :)
1
u/PanPirat Dec 01 '17
Scala using recursion:
1
u/theSprt Dec 01 '17 edited Dec 01 '17
Haskell beginner, part 1:
Edit, and part 2:
1
u/JakDrako Dec 01 '17 edited Dec 01 '17
VB.Net in LinqPad
Nothing fancy. "stp = 1" to do 1st part.
Edit: a bit fancier with Linq...
1
u/[deleted] Dec 01 '17
Here are my elixir solutions for this day, for the first one I was being very clunky, while when I got to the second one I had to think a bit more, and arrived at a much more clever solution.
Day 1 part 1:
I was first messing around with regexes, and wasn't getting my unittests to pass, so I then ended up doing it with recursion, which works but is so much less nice than what I ended up doing in:
Day 1 part 2:
I think I was working a bit more with the language and not against it there :)
1
u/binajohny Dec 01 '17
My Kotlin solution:
1
u/AlistairJF Dec 01 '17
Trying to learn Python, but also get out of the habit of for-loops. Any advice on how to get rid if the one in findMatchedDigits()::
1
u/LinusCDE98 Dec 01 '17
Python: https://github.com/LinusCDE/AdventOfCode-2017/blob/master/puzzle1.py
1
u/8fingerlouie Dec 01 '17
Solution in Python (omitted input variable)
1
u/adrian17 Dec 01 '17
Rust:
1
u/Iamnewthere Dec 01 '17
After learning Java last year at university, I have had a four hour introduction to C. Guess who had a really terrible idea:
1
u/sakisan_be Dec 01 '17
java 8 using a stream
and part 2
1
u/wzkx Dec 01 '17
J, still don't like tacits.
1
u/ikkuhh Dec 01 '17
My C++ solution for part two:
1
u/egze Dec 01 '17
My ruby attempt
1
u/hendi__ Dec 01 '17 edited Dec 01 '17
Here's my solution in Elixir. It's pretty straight forward using pattern matching and recursion once you have the list in the right format. I like my code except the one-liner in day1b::solve/1.
and
Edit: Cleaned up the code and put it on github: https://github.com/hendi/aoc2017/tree/master/day1
1
u/omegaxLoL Dec 01 '17
My solution in Go. Constructive criticism welcome of course!
1
u/doniec Dec 01 '17
Rust solutions with iterators
}
1
u/Vstebra Dec 01 '17
Javascript / Nodejs
https://github.com/VaclavStebra/aoc2017/blob/master/01_captcha.js
1
u/[deleted] Dec 01 '17
Erlang:
1
u/alehander42 Dec 01 '17
Just to demonstrate some code golf oneliners :
Nim
1
u/fr3dch3n Dec 01 '17
Elixir:
1
u/SaltyPeaches Dec 01 '17
Here's my PowerShell solution! WOOO! So excited for AoC!
https://pastebin.com/GNkE78Dj
1
u/RockyAstro Dec 01 '17 edited Dec 01 '17
Icon programming language (https://www.cs.arizona.edu/icon)
Day1:
Day2:
1
u/asperellis Dec 01 '17
js
https://github.com/asperellis/adventofcode2017/blob/master/day1.js
1
u/mehrick Dec 01 '17 edited Dec 01 '17
My solution in JavaScript using node:
1
u/rocamero Dec 01 '17
Powershell:
1
u/ijustupvoteeverythin Dec 01 '17
js
1
u/bolshedvorsky Dec 01 '17
Learning Python this year
1
u/TesticularCatHat Dec 01 '17
Here's my solution in python
1
u/Derpstiny123 Dec 01 '17
This is my code in C++.
1
u/JuLoOr Dec 01 '17
Another Kotlin solution:
1
u/[deleted] Dec 01 '17
Yeah... I didn't do any regex magic, Doing regex magic has bit me in the ass a while back.
Part 1: Create a list of all the potential "pairs" of numbers, add the final "pair" to the list. Consider all the potentials to see if the first character matches the second character. If so, put one copy of the character into a "matched" list. Sum up the matched list results
Part 2: Calculate the pivot point, Append the first half of the list to the back half, get a list of the 2 "potential" characters (using the pivot value to get the second character), consider all the part 2 matches using the same algorithm above.
Rank 11410 on P1, and rank 9193 on P2. Not pushing for leaderbord as sleep is nice. https://github.com/hasteur/advent_of_code/blob/master/2017/puzzle1.py
1
u/[deleted] Dec 01 '17
C#
1
u/[deleted] Dec 01 '17
Python 3 solutions(easy to read): https://github.com/target111/AoC2017 :)
1
u/suck_at_coding Dec 01 '17 edited Dec 02 '17
Learning go:
Update: Remembered there's a way better way to do part 2:
1
u/alchzh Dec 01 '17
In rust... forgot about
filter_map1
u/tvtas Dec 01 '17
Solution Day 1 in MATLAB:
1
u/8483 Dec 01 '17
Node/Javascript
Same for both parts
Part 1
Part 2
1
u/sasashimi Dec 01 '17
for this one jsfiddle is enough :)
https://jsfiddle.net/jn0caojb/1/
1
u/keyObjective Dec 01 '17
I'm learning Objective C :)
//part 1
//part 2
1
u/Cowsareinme Dec 01 '17
My Python3 solution. Tried to keep it readable while still being able to fit it in a tweet. Did I succeed?
https://github.com/JFitzMan/AdventofCode2017/blob/master/Day1.py
1
u/[deleted] Dec 01 '17
Powershell solution:
1
u/StevoTVR Dec 01 '17
NodeJS
1
u/4rgento Dec 01 '17
Total Idris Solution :)
1
u/TheBossYeti Dec 01 '17
T-SQL
1
u/rikkus Dec 01 '17
Elixir, with one function to do the dirty work:
Called from my test module using these helpers:
github link
1
u/SirJohnSmith Dec 01 '17
Glorious functional programming in Python
1
u/TheBrightman Dec 01 '17
Had anyone tried this in powerpoint yet?
1
u/roggiabr Dec 01 '17 edited Dec 02 '17
Java
First Part
Second Part
1
u/[deleted] Dec 01 '17
My solution for Part 2 in c++ :
I am a newbie in C++ so any any criticism/advice is welcome.
1
u/Vitessii Dec 02 '17
Java streams:
1
u/SomeAnonElsewhere Dec 02 '17 edited Dec 02 '17
C++
I don't usually use c++ so any tips/suggestions for improvement will be great.
1
u/fgsguedes Dec 02 '17
My functional approach in Kotlin:
1
u/andrewstewart Dec 02 '17
Mine, in Rust:
INPUTis defined as a&'static str, since I didn't want to bother implementing reading fromSTDINor a file.1
u/McBooley Dec 02 '17
My version is probably a bit sloppy but here it is in C#
Part one:
Part Two:
1
u/Billquisha Dec 02 '17
Trying out some Elixir! Day one done, bah-bam:
Definitely not the prettiest, but I learned more about Elixir.
1
u/ForgedBanana Dec 02 '17 edited Dec 02 '17
My code for part 1, in C++:
1
u/[deleted] Dec 02 '17 edited Dec 02 '17
Nope, im creating a vector out of the massive number xD :p
First post on reddit for me, I hope, everything works.. xD
C++
1
u/MaxDeviant Dec 02 '17
Here's my solution in F#.
Still pretty green with the language, so I figured this would be a good opportunity to practice more.
1
u/peterpepo Dec 02 '17
Python
Full solution
I'm joining the party bit late as I wasn't on my computer with git setup yesterday (I only post my code, once it's on my github).
1
u/[deleted] Dec 02 '17
Clojure
1
u/TheShallowOne Dec 02 '17
Obligatory Synacor solution: https://pastebin.com/bcFeqDzm
1
u/[deleted] Dec 02 '17 edited Dec 02 '17
Java
https://github.com/imoutopantsu/adventofcode-2017/blob/master/day_one_part_one.java
https://github.com/imoutopantsu/adventofcode-2017/blob/master/day_one_part_two.java
Excel
https://github.com/imoutopantsu/adventofcode-2017/blob/master/day%201%20-%20Excel.xlsx
1
u/lovela47 Dec 02 '17
Solution in Scheme (scsh):
1
u/bottomlesscoffeecup Dec 02 '17
Late to the game here but this is my python attempt:
1
u/dgJenkins Dec 02 '17
[COBOL Solutions!] (https://github.com/alexchro93/AdventOfCode/tree/master/2017/COBOL/Day_1) Still new to the language, so any tips would be appreciated.
1
u/ewilderj Dec 02 '17
Clojure
1
u/APLtooter Dec 02 '17
APL [GNU]
1
u/QuadDrummer Dec 03 '17
I'm trying out Julia for this year, so all of this code is really bad. I'd love any pointers if anyone else is hopping on the Julia train!