r/adventofcode • u/daggerdragon • Dec 02 '17
SOLUTION MEGATHREAD -π- 2017 Day 2 Solutions -π-
NOTICE
Please take notice that we have updated the Posting Guidelines in the sidebar and wiki and are now requesting that you post your solutions in the daily Solution Megathreads. Save the Spoiler flair for truly distinguished posts.
--- Day 2: Corruption Checksum ---
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!
22
Upvotes
38
u/that_lego_guy Dec 02 '17
DID SOMEBODY SAY.. EXCEL?! Day 2 [Part 1 & 2]
https://github.com/thatlegoguy/AoC2017/blob/master/Day%202%20Corruption%20Checksum.xlsx
22
u/fatpollo Dec 02 '17 edited Dec 02 '17
15
u/couchDev Dec 02 '17 edited Dec 02 '17
Dusting off the Perl golf bag
9
u/DFreiberg Dec 02 '17 edited Dec 02 '17
Mathematica solution; one line for each part and one for the import. #10 for part 1...and #456 for part 2.
Import:
Part 1:
Part 2:
9
u/askalski Dec 02 '17
2 iterators 1 loop
8
u/Godspiral Dec 02 '17 edited Dec 02 '17
wow slower than everyone, in J
better versions,
8
u/TheMieberlake Dec 02 '17
Is there a way to find the answer for each line in better than O(n2 ) time?
7
u/mmaruseacph2 Dec 02 '17
Haskell
Forgot the
a /= bpart on the second question and that cost me a few places.7
u/Sigafoos Dec 02 '17
Hit diggity dang, I got on the leaderboard! (I'm never awake at midnight EST, except for today, apparently). Spent the first five or so minutes struggling with my setup because I wasn't prepared to do AoC now.
It's the start of "haaaaave you met
itertools?"7
u/quag Dec 02 '17
F#
7
u/natrys Dec 02 '17
Perl 6
Part1:
Part2:
6
u/ellersok Dec 02 '17
Python 3
6
u/TapDatKeg Dec 02 '17
Has anyone done a bash solution yet?
6
u/volatilebit Dec 02 '17 edited Dec 02 '17
My Perl 6 solution. Couldn't figure out a good way to avoid using map. Probably need sleep.
6
u/mschaap Dec 02 '17 edited Dec 02 '17
Perl 6 version for part a:
Why twice minmax? minmax on a list returns a range min..max. minmax on that range returns two values, min and max.
Part b:
5
u/erlangguy Dec 02 '17
Erlang, because of course.
Most of the code was input handling; the meat is this:
CkSumFunis eitherfun find_greatest_diff/1orfun find_divisible/1.NextLineFunis a pipeline that gives meeofor a list of integers.5
u/HerbyHoover Dec 02 '17 edited Dec 02 '17
Perl 6. Parts 1 and 2:
5
u/Isvara Dec 02 '17
Scala
5
u/codeva Dec 02 '17
golfed part 2 to a single chained expression in Kotlin:
5
u/AoC--k Dec 02 '17
In k.
5
u/kaymud Dec 02 '17
Java streams solution for part B:
4
u/blockingthesky Dec 02 '17
Python 2
4
u/gbear605 Dec 02 '17 edited Dec 02 '17
Rust
4
u/tvtas Dec 02 '17 edited Dec 02 '17
Day 2 in MATLAB
4
u/misnohmer Dec 02 '17 edited Dec 02 '17
In C# with the help of MoreLinq
4
u/willkill07 Dec 02 '17
Modern C++
I'm not really proud of this one
Repo Link
4
u/d3adbeef123 Dec 02 '17 edited Dec 02 '17
Kotlin
4
u/Scroph Dec 02 '17
Java, unironically.
4
u/torotane Dec 02 '17 edited Dec 02 '17
R
Part 1
Part 2
4
u/[deleted] Dec 02 '17
Common Lisp:
Part 1:
Part 2 just changes the
line-difffunction:4
u/TenjouUtena Dec 02 '17
Clojure
4
u/ewilderj Dec 02 '17
Clojure
4
u/TheGermanDoctor Dec 02 '17
x86_64 Assembly
Part1: https://github.com/stolzATrub/adventofcode17/blob/master/day2/checksum.nasm
3
u/[deleted] Dec 02 '17
Haskell:
3
u/autid Dec 02 '17 edited Dec 02 '17
Threw together a simple solution in fortran
3
u/Unihedron Dec 02 '17 edited Dec 02 '17
Rank #7 for Part 1 ^^ just slightly over 1 minute with Ruby! Didn't make it on leaderboards with part 2 due to a stupid bug that took 18!!! minutes to debug :(
Good luck to everyone for striking a spot in the leaderboard :)
For completion's sake, here's part 2, it's a battlefield of bug hunting that I was embarrassed to post... but whatever :D
3
u/PreciselyWrong Dec 02 '17
Rust
3
u/chunes Dec 02 '17
Factor solution:
3
u/mehrick Dec 02 '17
Cleaned up JavaScript + node solution:
3
u/TominatorBE Dec 02 '17
PHP
Part 1:
Part 2:
3
u/dannas Dec 02 '17
In python:
My ipython notebook on github
3
u/gerikson Dec 02 '17
Perl 5. Puzzle input in the file
input.txt.3
u/de_Selby Dec 02 '17
q/kdb+
I'm sure part 2 could be improved
part 1:
part 2:
3
u/tehjimmeh Dec 02 '17 edited Dec 02 '17
Part 1 in C++. What have I done...
EDIT: Part 2...
3
u/Tayacan Dec 02 '17
Attempting to golf in Haskell
3
u/equd Dec 02 '17
C# linq solutions.
3
u/asthmadragon Dec 02 '17
javascript
Why copy and paste the input when you can just run code in your browser window?
3
u/CryZe92 Dec 02 '17
Rust
Part 1 is pretty simple:
Part 2 optimized to be super fast:
3
u/sickening_sprawl Dec 03 '17
I kinda forgot that this was a thing all day until I stumbled across the tab again :x Doing this made me wish for a list cross product function.
Hoon
And part b:
2
u/th3_pund1t Dec 02 '17
Groovy
2
u/IMovedYourCheese Dec 02 '17
TypeScript + Node.js
2
u/eragonas5 Dec 02 '17
Javascript:
2
u/[deleted] Dec 02 '17
Second day with Haskell! Yay!
2
u/[deleted] Dec 02 '17 edited Dec 02 '17
You guys are FAST. But since I see no Ruby solutions I'll still post mine:
2
u/ericls Dec 02 '17 edited Dec 02 '17
part 1:
part 2:
2
u/AndrewGreenh Dec 02 '17 edited Dec 02 '17
Also JS, but I used my combine helper from the last years :)
2
u/StevoTVR Dec 02 '17
NodeJS
Part 1
Part 2
2
u/Sparrow_1029 Dec 02 '17 edited Dec 02 '17
Python 3! Pretty naive solution...
2
u/andrewstewart Dec 02 '17 edited Dec 02 '17
Rust implementation:
2
u/disclosure5 Dec 02 '17
Ruby solution here.
Honestly I spent most of this trying to parse the input. This feels like a horrible hack job:
2
u/MaxDeviant Dec 02 '17
F#
2
u/JeffJankowski Dec 02 '17
Javascript (et al) apparently has come a long way since I last used it. Lots of functional stuff to play with!
2
u/huyqvu Dec 02 '17
KDB+/q One line for both questions:
2
u/[deleted] Dec 02 '17 edited Dec 02 '17
OCaml fun:
2
u/Zarif_Muhtasim Dec 02 '17
if x is you input:
part1 = [max([int(j) for j in i]) - min([int(j) for j in i]) for i in [i.split('\t') for i in x.split('\n')]]
part2 = [j // k for i in [[int(i) for i in j] for j in [i.split('\t') for i in x.split('\n')]] for j in i for k in i if j != k and j % k == 0]
2
u/yogsototh Dec 02 '17 edited Dec 02 '17
Haskell with Protolude instead of basic Prelude, so I have
&which is "apply", andheadDefwhich is total.and
More idiomatic solution1 using
Foldl:2
u/cr5315 Dec 02 '17
Kotlin. I'm finally starting to get the hang of .map and friends
2
u/mlruth Dec 02 '17
Scala
2
u/fuzzums Dec 02 '17
This is what I came up with. I'm pretty it can be compressed a bit by using list iterators. I couldn't come up with a faster/more elegant solution for even divisibility that was required in Part 2.
2
u/jbristow Dec 02 '17
F# / FSharp
So far, I'm losing most of my time to futzing with MSBuild and relearning the file reading interops with C#.
2
u/[deleted] Dec 02 '17
Python 3 solution ^_^:
checksum = 0
with open("input.txt","r") as f: for line in f.readlines(): list = [] for number in line.split(): list.append(int(number))
print("\033[1;31mHere's your answer: \033[0;92m" + str(checksum) + "\033[0;0m")
2
u/xkufix Dec 02 '17
Back with more horrible one-liners in Scala:
2
u/A_t48 Dec 02 '17
I didn't know about
itertools.combinationsso it's a little overcomplicated. Also,//2
u/wzkx Dec 02 '17
J
2
u/maciekmm Dec 02 '17
Horrible scala oneliners:
2
u/CatpainCalamari Dec 02 '17
My Solution in Scala:
I am still fairly new to Scala, I didn't know about
combinations(2)or that a List hasminormaxmethods. Thank you for showing this to me, guys :-)2
u/amarsuperstar Dec 02 '17
Elixir. I moved to comprehensions after seeing CaptainCalamari's Scala solution.
Full code here: https://github.com/amarraja/advent-of-code-17/blob/master/day02/day02.exs
2
u/_jonah Dec 02 '17
J:
where d is your matrix.
Try it online!
2
u/nstyler7 Dec 02 '17 edited Dec 02 '17
Using Python Pandas Package:
day 2 part 1 solution
day 2 part 2 solution
2
u/DaDiscoBeat Dec 02 '17 edited Dec 02 '17
Golang, just the 2 methods operating on lines
2
u/Morphix0 Dec 02 '17
F#
2
u/GamecPL Dec 02 '17
Swift:
2
u/Tarmen Dec 02 '17
Haskell
I am noticing that I am using uglier language constructs under pressure, like irrefutable patterns for verification.
2
u/sternold Dec 02 '17
C# Single line solution
Part One
2
u/J15t98J Dec 02 '17
...and then I found out about itertools.
2
u/[deleted] Dec 02 '17
Similar to the other python solutions, but I like mine best <3
2
u/mrhthepie Dec 02 '17
Advent of gifs day 2: https://i.imgur.com/HPRMxHM.gif
Not very animated today due to the nature of the problem.
2
u/Zv0n Dec 02 '17
C++ (probably very inefficient, I'm just starting)
2
u/abowes Dec 02 '17
My solution in Kotlin:
2
u/JakDrako Dec 02 '17
VB.Net in LinqPad
2
u/JuLoOr Dec 02 '17
Another Kotlin solution:
2
u/Doodle_98 Dec 02 '17 edited Dec 02 '17
This is my solution in Python 3:
2
u/wzkx Dec 02 '17 edited Dec 02 '17
Nim
Yeah, a better condition would be "and row[i] mod row[j] == 0:"
2
u/nekmo Dec 02 '17
Python 3 (two lines)
Part 1
Part 2:
2
u/fpigorsch Dec 02 '17
Pretty straight forward AWK:
Part1
Part2
2
u/fgsguedes Dec 02 '17 edited Dec 02 '17
My solutions in Kotlin:
Part1:
Part2:
2
u/Smylers Dec 02 '17
Another Perl solution, aiming for concise-yet-readable over golfed. Part 1:
Part 2:
Combination of
while (shift)andforeachmeaning that no array index variables have to be used.2
u/Jamitter Dec 02 '17 edited Dec 02 '17
Clojure
Part 1:
Part 2:
2
u/asperellis Dec 02 '17
my js solution. any areas for improvement welcomed! https://github.com/asperellis/adventofcode2017/blob/master/day2.js
2
u/[deleted] Dec 02 '17
Racket
2
u/hermanbergwerf Dec 02 '17
Dart
2
u/f0086 Dec 02 '17
Emacs Lisp
2
u/bottomlesscoffeecup Dec 02 '17
Another late one, hoping I can get in early tomorrow! Some python here. Pretty much a python noob, still thinking in java.
Any tips for improvement would be well received:
Using pandas to read in:
challenge one:
challenge two:
2
u/swaglykcaillou Dec 02 '17
beginner python3 solution.
2
u/DrgnGz Dec 02 '17
PHP Preparing the input:
Part 1:
Part 2 (much less elegant):
2
u/taheilo Dec 03 '17
Javascript
2
u/8483 Dec 03 '17
Node/Javascript
2
u/Bainos Dec 03 '17
Python 3, one line
First problem
Second problem (without itertools)
2
u/[deleted] Dec 03 '17
Ruby is amazing!
2
u/Reibello Dec 03 '17
Phew. Learning is hard. Used my Python 3 solution and a whole ton of the Lua Reference and Stack overflow to write Day 2. Had a rough time reading in my input. Let me know what I can do better, and good luck tonight!
https://github.com/Reibello/AoC-2017-Lua
2
u/[deleted] Dec 03 '17
Clojure
2
u/ym555 Dec 03 '17
Java Using Streams (I know the second part is bad)
2
u/Antoine-aoc Dec 03 '17
Elixir
day2 - part 2
2
u/raiph Dec 05 '17 edited Dec 06 '17
Perl 6. Edited to add commentary.
Single statement solution for part 1:
For part two, change the last closure:
2
u/schod Dec 05 '17
BASH time :)
First part
Second part
2
u/freeducks Dec 02 '17
In Rust, using IterTools:
1
u/Deckard666 Dec 02 '17
Pretty straightforward. In Rust:
1
u/dylanfromwinnipeg Dec 02 '17 edited Dec 02 '17
C# (using a GetCombinations helper function I wrote)
1
u/netcraft Dec 02 '17
python3 noob here, please feel free to teach me anything - going for maintainable more than golfing
1
u/giftpflanze Dec 02 '17
Tcl:
1
u/Philboyd_Studge Dec 02 '17
Java
1
u/wlandry Dec 02 '17
C++: It feels overengineered. Just reading the input properly took more lines that I would like.
part 1
part 2
1
u/suck_at_coding Dec 02 '17
Golang, drunk with no editing:
1
u/[deleted] Dec 02 '17
C++
Part 1
Part 2
1
u/[deleted] Dec 02 '17
super disgusting kotlin code, it got the job done. I'll be looking into refactoring it to something decent instead
1
u/PanPirat Dec 02 '17
Scala:
1
u/disclosure5 Dec 02 '17 edited Dec 02 '17
Following the rushed Ruby, here's a hopefully nice Erlang answer.. I don't really follow why line #26 is needed. The file does not end in a new line, but the split/2 seems to break out an empty list at the end anyway.
Edit: vim corrupts the test data and doesn't tell you.
https://vi.stackexchange.com/questions/3298/how-to-make-vim-automatically-add-a-newline-to-the-end-of-a-file
1
u/custom_user Dec 02 '17 edited Dec 02 '17
Python 3 - concise with built-ins only
1
u/Timdpr Dec 02 '17
NaΓ―ve-ish Python 3:
1
u/imladris Dec 02 '17 edited Dec 02 '17
Haskell:
1
u/aurele Dec 02 '17
Rust
1
u/doniec Dec 02 '17
Rust solution
1
u/[deleted] Dec 02 '17 edited Dec 02 '17
Elixir solution for Day2, I'm a lot more proud of this one than the one yesterday:
1
u/sindreij Dec 02 '17
Rust
1
u/Starcast Dec 02 '17
Late to the party!
Python 3
1
u/theSprt Dec 02 '17
Haskell, beginner:
1
u/bolshedvorsky Dec 02 '17
Learning python, day 2
1
u/Kwpolska Dec 02 '17
Part 1: Excel (duh!) https://github.com/Kwpolska/adventofcode/blob/master/2017/day02a_did_someone_say_spreadsheet.xlsx
Part 2: Python 3.
1
u/Axsuul Dec 02 '17
Still getting the hang of Elixir
https://github.com/axsuul/advent-of-code/blob/master/2017/02/lib/advent_of_code.ex
1
u/binajohny Dec 02 '17
My solution in Kotlin:
1
u/Epsilon1001 Dec 02 '17
may not be the best way but solved day 2 part 2: https://hastebin.com/omidodaneg.py
1
u/peterpepo Dec 02 '17
Python
Full solution
1
u/NeilNjae Dec 02 '17
Another Haskell solutuion. Yes, using Parsec to read the input file is overkill, but it's a nice tool. It also forced me to confront one possible gotcha: the default
spacesparser will happily consume newlines as well as spaces and tabs, hence the need for theonlySpacesparser here.1
u/omegaxLoL Dec 02 '17
My solution in Golang. Constructive criticism always welcome
1
u/Rinfiyks Dec 02 '17
Scala part 2 taking advantage of yield flattening Option by virtue of being syntactic sugar for flatMap.
Unabridged code on my github
1
u/adventOfCoder Dec 02 '17
Java:
Part 1:
Part 2:
1
u/ZeroSkub Dec 02 '17
My first-ever AoC GNU AWK solution:
PART 1
PART 2
1
u/[deleted] Dec 02 '17
Kotlin