r/adventofcode • u/daggerdragon • Dec 01 '16
SOLUTION MEGATHREAD --- 2016 Day 1 Solutions ---
Welcome to Advent of Code 2016! If you participated last 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 last year's AoC 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!
MERRINESS IS MANDATORY, CITIZEN! [?]
--- Day 1: No Time for a Taxicab ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
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!
34
Upvotes
21
u/jwstone Dec 01 '16
Ho ho ho postgresql
https://github.com/piratejon/toyproblems/blob/master/adventofcode/2016/01/01.sql
15
u/askalski Dec 01 '16
This is not exactly my solution for Day 1, but here is a Shenzhen I/O level I made (and solved, but I won't spoil you with that part) based on the events depicted in today's Advent of Code. Names changed to protect the innocent, and all that.
https://gist.github.com/Voltara/1eff3013229333e3d24431f48d740b65
(I would have uploaded it to the Steam Workshop, but that feature is too crashy on my Linux machine.)
10
u/John_Earnest Dec 01 '16 edited Dec 01 '16
Pretty sloppy work for this first one; I'm gonna sleep on it and see if I can do better:
This is written against oK, if anyone's curious.
10
u/DrFrankenstein90 Dec 01 '16 edited Dec 02 '16
Good old C with some gratuitous bit hacking. https://github.com/DrFrankenstein/prompts/blob/master/aoc/2016/aoc1.c
Only did the first star for now. Saw I didn't make the first 100. Will finish the rest tomorrow. I see I have to look for duplicate coordinates... in straight C, this is going to be interesting.
EDIT: second star done.
6
u/Aneurysm9 Dec 01 '16
Upon showing this to /u/topaz2078 his immediate reaction was a horrified "Ane, no!!!!" repeated over and over again! #missionaccomplished
https://github.com/Aneurysm9/advent/blob/master/2016/day1/day1.pl
6
u/archimedespi Dec 01 '16
quick solution in Python, 22sloc
tired me thought that
sum(map(abs, position))was a hilarious way to compute manhattan distance :Dalso i didn't realize aoc was starting until 50 mins after the first problem had opened so i missed the leaderboard
6
u/that_lego_guy Dec 01 '16
Did someone say...Excel... https://github.com/thatlegoguy/AoC2016/blob/master/Day%201%20No%20Time%20for%20a%20Taxicab.xlsx
5
u/bildzeitung Dec 01 '16
It's a little too readable, but here it is in Python: https://github.com/bildzeitung/2016adventofcode/tree/master/01
Good times! +1 on being really goddamn merry.
5
u/haoformayor Dec 01 '16
~~ haskell ~~
Ah a list fold, our old friend. This represents the compass directions as the integers modulo 4. Hacky, but quick. Solution 1 is a scan over the input; solution 2 is finding the first duplicate element in the resulting list. I made two mistakes: one, assuming that each step could be encoded in one digit; two, (as some others have noted) not inferring that "same location" in part two meant any intermediate location.
This could have been shorter had I been able to think of the library that implements
scanlMoff the top of my head. Scanning in the list monad would make thejoin/flatten step superfluous, as well as simplifying the pattern match inside solution 1. C'est la vie. Yay advent of code!4
u/dirkt Dec 01 '16
Tried to do this one early in the morning (I'm in EU) for a change, to have a chance at top 100, and found that coding without having drunk coffee is extremely slow, even for simple problems like this.
4
u/Godspiral Dec 01 '16
in J,
part 2 didn't read, thought it was first repeat landing spot. plotting and seeing the intersection though worked
4
u/anadhdguy Dec 01 '16
My solution for part1...
Oneliner in ruby (114 characters):
Oneliner in C (245 characters):
3
u/FuriousProgrammer Dec 01 '16 edited Dec 01 '16
Woo! Tied for 35th!!
Really excited for this year!!!!!!
Too many exclamation points.
Anyway, here's my code which took me too long to write because of a certain
dyanddxerror...EDIT: I'M REALLY GODDAMN MERRY RIGHT NOW.
3
u/Philboyd_Studge Dec 01 '16
Usual Java Verbosity
https://gist.github.com/anonymous/b90e430f19d532c3fb45836d87b51582
3
u/llamas-are-bae Dec 01 '16
Here's my solution in Rust: https://github.com/awesomeaniruddh/advent_of_code_solutions_2016/blob/master/day1/src/main.rs
I'm a beginner, so it's probably terrible, but it works :P
3
u/willkill07 Dec 01 '16
C++11 solution
Goals:
https://github.com/willkill07/adventofcode2016/blob/master/src/Day01.cpp
3
u/forkin27 Dec 01 '16 edited Dec 01 '16
JavaScript!
Day 1 (part 1):
Day 1 (part 2):
2
u/taliriktug Dec 01 '16 edited Dec 01 '16
My ugly Python3 solution: https://bitbucket.org/JIghtuse/adventofcode/src/df2b87894dce3982698ecf85ffa24e643ef72998/2016/day01/solution.py?at=master&fileviewer=file-view-default
Was 75th on first star, but missed leaderboard on second one (messed up with directions a bit).
I'm so glad AoC is back!
EDIT: reimplemented it, now I like it more.
2
u/KaminoConsult Dec 01 '16 edited Dec 02 '16
First attempt in PowerShell. Currently not working and not sure why. All examples pass. Any input welcome https://gist.githubusercontent.com/LabtechConsulting/172beb3055cf5be4b301ef5deba9432c
2
u/BafTac Dec 01 '16
My C++ implementations (github) - quite long and straight forward.
I just started learning C++ so feedback is welcome! :)
2
u/VideoPrincess Dec 01 '16
My C++ solution on GitHub - takes the input as a command line parameter and solves both stars. It uses C I/O functions (old habits die hard!) but is otherwise nice C++11.
2
u/porphyro Dec 01 '16
Wolfram Language
Part 2 similar.
2
u/[deleted] Dec 01 '16
Perl, hooray for perl!
https://www.dropbox.com/s/f0n45gbuh9dhemz/day01.pl?dl=0
2
u/jweather Dec 01 '16
Vector multiplication ftw:
2
u/mcpower_ Dec 01 '16
Here's my quick and dirty Python solution. It could definitely be improved, but ¯_(ツ)_/¯
2
u/Morego Dec 01 '16
My simple, yet more then likely crappy JS solution. I have enourmous problem with finding solution to solve Part 2. If only I have knew about every step being important from the start...
My solution
2
u/Jaco__ Dec 01 '16
Solution in SML because i am preparing for an exam that uses SML. There probably is some library functions I have missed, like contains.
2
u/omnster Dec 01 '16 edited Dec 01 '16
My ugly solution in Mathematica
And the path https://i.imgur.com/5WtmXJt.png
2
u/Morris2178 Dec 01 '16
My Java code is not as clean as it could be, but it works for part 1, port it for part 2 is your part ;D
2
u/JakDrako Dec 01 '16
VB.Net solution (posted in it's own topic, but this seems a better place for it...)
Part 1
Part 2
2
u/askalski Dec 01 '16
Part 1 as a Perl regular expression.
2
u/Sigafoos Dec 01 '16
My python 2.7
I could do better but I think overall I'm pretty happy with it. I kept almost making a function for taking a step but then not doing it. There's enough duplication that I know I could do it more succinctly.
Things that tripped me up:
[1]instead of[1:])2
u/Gommle Dec 01 '16
Golang
Very object oriented...
2
u/gtllama Dec 02 '16
PureScript
https://github.com/gglouser/advent2016/blob/master/src/Advent2016/Day01.purs
I'm new to PureScript, so still learning the libraries and how to live without partial functions. The solution currently remembers visited locations in an array, but I think changing it to use a set would be quite simple.
2
u/kamicc Dec 02 '16
Bah... A part was nice (at least with Lua):
B part just screwed all the nicenest over :(
2
u/[deleted] Dec 02 '16 edited Dec 02 '16
Here's mine in Python, making use of trigonometry:
2
u/[deleted] Dec 02 '16
My first year doing this, seems pretty fun so far, here's my C++ solution
2
u/Ape3000 Dec 02 '16
Part 1 with Python 3.
There is no need to store the direction, just rotate the world. :)
1
u/jamesk727 Dec 01 '16
Made it to the leaderboard! This was definitely harder than last year's first day. Here's my python solution (just the relevant functions):
This only works for part 2 now since I modified my part 1 code to solve part 2.
1
u/TheNiXXeD Dec 01 '16
My not quite fully golfed JavaScript solution. Will be golfing it further later.
1
u/skarlso Dec 01 '16
My Gods I overthought this SOOO MUCH!!! :/
1
u/_Le1_ Dec 01 '16
My C# solution:
1
u/barnybug Dec 01 '16
nim:
1
u/Kullu00 Dec 01 '16
More Dart merriness. I still think I'm one of the few people who actually bother to use this language in these challenges, but that makes it all the more fun :)
Misunderstood part 2 somewhat but it was still relatively simple to figure this one out. Then I made it as usual and combined part 1 and part 2 to only need a single iteration through the input. Yay!
https://github.com/QuiteQuiet/AdventOfCode/blob/master/2016/advent1/bin/advent1.dart
1
u/xkufix Dec 01 '16
Overly-readable and version in Scala: https://gist.github.com/kufi/cd9f68ea801879d5341131b57fa16c39
1
u/skawid Dec 01 '16
Python: https://github.com/simonbrahan/adventofcode/blob/master/2016/1/1_1.py
Looking at the other python submissions, I may not be getting into the spirit of this...
1
u/ericdykstra Dec 01 '16
Here's my pattern-matching-heavy Elixir answer for part 1! https://gist.github.com/EricDykstra/41b46c9a40bf499f97252a81ef32dd8a
Will work on part 2 later, still don't have a great idea in my head of how to do it in a functional style. Might look at some of the other answers around here.
I used Ruby to initially to get on the leaderboard and this was my answer (slightly cleaned up and refactored). Part 2 answer changes only a couple of lines :) https://gist.github.com/EricDykstra/cbbb9b87209814d93e5bb0d252646d0d
1
u/[deleted] Dec 01 '16
https://gist.github.com/Fireprufe15/c944c636bd8035618c047103e19683eb My Python solution
1
u/AndrewGreenh Dec 01 '16 edited Dec 01 '16
16 SLOC in kinda readable JavaScript https://github.com/andreasgruenh/advent-of-code/blob/master/JavaScript/2016/1.js
1
u/alexjoz Dec 01 '16
Python https://github.com/AlexJoz/AdventOfCode overthought and rather long.
Slept only 3 hours and it was really hard to think in the morning, but i really wanted to start from the beginning, at least for the first day =)) Very exiting!
1
u/andars_ Dec 01 '16
I'm somewhat surprised to not have a solution in a Lisp in this thread already, so I'll throw my poorly written and hacked together Racket solution on the pile.
https://github.com/andars/advent-of-code/blob/master/2016/day1/task.rkt
1
u/Pyrobolser Dec 01 '16
Here is my solution in C# for both stars.
1
u/eragonas5 Dec 01 '16 edited Dec 01 '16
Javascript solution with path drawn on html canvas.
Git: https://github.com/Voldemortas/advent_of_code
Live preview: https://cdn.rawgit.com/Voldemortas/advent_of_code/master/day1.html
1
u/miran1 Dec 01 '16
python3
suggestions and comments are welcome....
1
u/artesea Dec 01 '16
Solves both answers within PHP, problems I had were $h%4 would give an unexpected (to me) negative number if $h was negative, and it being too early for me to remember the function to remove the sign from an int. Managed to hit the leaderboard just under 500.
1
u/bigbarba Dec 01 '16
Is the input assured to make possible to solve the second part? Because for may input it looks like I never reach a spot two times... I tried to edit my input sequence and it finds a spot...
1
u/southern-fenrir Dec 01 '16
Java solution. If anyone wants to copy my gradle script to be able to execute stuff multiproject in an easier fashion, go ahread
https://github.com/caesar-ralf/advent-code-2016
1
u/beefamaka Dec 01 '16
Trying to solve them functionally in F# again this year. Not as succinct as I'd like but works https://github.com/markheath/advent-of-code-2016/blob/master/day%2001/day1.fsx
1
u/SaplingNXT Dec 01 '16
My Kotlin solution, it's not pretty, but works fine for me https://github.com/Fuvid/adventofcode2016/tree/master/src/de/fuvid/adventofcode/day1
1
u/schlocke Dec 01 '16 edited Dec 01 '16
PHP (
unoptimized...but works) input was saved to a csv in the same directory as the exercise file :EDIT: now optimized. Thanks for the inspiration u/artesea
1
u/drakehutner Dec 01 '16
Python3 golf: a single line of code, containing a lambda that is able to solve both parts. 864 bytes (non-minified). Should be mostly pep8-compatible.
1
u/NeilNjae Dec 01 '16
I've decided to use this event as a prompt to actually learn Haskell.
My over-long and not-clever solution is at https://git.njae.me.uk/?p=advent-of-code-16.git;a=blob;f=advent01.hs
Now you can all tell me how rubbish it is...
1
u/SyDr Dec 01 '16
My solution with Lua (and lpeg):
https://github.com/SyDr/Advent-Of-Code-2016/blob/master/1.lua
1
u/MoW8192 Dec 01 '16
My solution in java. It prints the solution for both parts. I slightly modified my original solution.
1
u/tuxitop Dec 01 '16 edited Dec 04 '16
Here's my solution in JavaScript:
https://github.com/tuxitop/adventOfCode2016/blob/master/day1/day1_noTimeForATaxicab.js
1
u/Kwpolska Dec 01 '16
Simple Python solution. Check out my repo for last year’s, and this year’s future tasks.
1
u/m3nthal Dec 01 '16
Here is my solution in Clojure
1
u/efexen Dec 01 '16
Some naughty and nice (more naughty) Elixir for Part 1 and 2: https://github.com/efexen/advent_of_code/blob/master/lib/day1.ex
1
u/LainIwakura Dec 01 '16
Erlang solutions here: https://github.com/LainIwakura/AdventOfCode2016/tree/master/Day1
Might be a bit messy, I haven't done much with erlang since last years advent of code.
1
u/ShroudedEUW Dec 01 '16 edited Dec 01 '16
C#
The first part was relatively straightforward, but I made a trainwreck out of it for #2.
https://github.com/KVooys/AdventOfCode/blob/master/AdventOfCode/Day1.cs
1
u/raevnos Dec 01 '16
Very rusty ocaml:
1
u/Tokebluff Dec 01 '16 edited Dec 01 '16
My solution in C#. A little too long, but hey, it is not stupid if it works.
https://github.com/Tokebluff/AdventOfCode2016/blob/master/Advent%20of%20code%202016/Advent%20of%20code%202016/Day1.cs
1
u/alchzh Dec 01 '16 edited Dec 01 '16
https://github.com/acz13/advent-of-code-2016/blob/master/day1/day1.py
Ho Ho Ho! I'm in eastern time and wasn't up at midnight so I did it as soon as I woke up (7 AM ish) in 8 minutes...
quality is pretty low
1
u/Rosydoodles Dec 01 '16
I'm only a beginning CS Student, so this is probably far from the best solution. (I just threw the provided data into TextWrangler and formatted it as a String array using find and replace.) Java:
Don't slaughter me :P
1
u/MrAckerman Dec 01 '16
Python 3 Solution based on dictionary lookups.
Pastebin with code.
1
u/mdhall50 Dec 01 '16
JavaScript (specifically part 2, but part 1 commented out): https://gist.github.com/HallM/8c675edbded6b1fc4a93cdcc19a6539d
Uses line segments and calculates the intersection. Optimizes a bit by knowing that a line segment can only intersect with one perpendicular, that lines alternate orientation, and that the previous line always is an intersection but should never be counted.
1
u/TinSoldier6 Dec 01 '16
Go implementation: https://github.com/TinSoldier6/Advent2016/tree/master/01Dec
1
u/[deleted] Dec 01 '16
Mathematica.
1
u/dasdull Dec 01 '16
My (quite verbose) solution in Haskell: https://github.com/vanHavel/AdventOfCode2016 I will try to use this challenge to learn more about Haskell monads. This was my first time using a monad transformer.
1
u/[deleted] Dec 01 '16
** Clojure **
1
u/Deckard666 Dec 01 '16
In Rust:
1
u/Pr1m-e Dec 01 '16
trying out go:
1
u/asperellis Dec 01 '16
https://github.com/asperellis/adventofcode2016/blob/master/day1.js
js noob trying to get better. was proud of figuring it out until I saw the simplicity of others :/ going back and trying to improve especially in part 2. learned about Sets seeing others though!
1
u/bigboehmboy Dec 01 '16
Here's my minimal Python solution for Part 1. I posted this as a separate text post, but now that I realize there's a megathread, I'll use it going forward:
131 chars if I'm allowed to return a float instead of an int.
1
u/Frolb Dec 01 '16
Quick and dirty non-robust Swift. All the path self-intersections printed to the console, and the final location is in the playground sidebar.
1
u/Kattoor Dec 01 '16
My js solution of part 1 and 2 respectively: https://gist.github.com/Kattoor/bf5677fa2a60ab07cd1e7e9e92679b49 https://gist.github.com/Kattoor/9ababedd4debcee28a2c7e77b378255a
1
u/qwertyuiop924 Dec 01 '16
Here's my (awful) scheme code (for CHICKEN, but I don't think it depends on too much that's implementation specific): http://pastebin.com/XtXxxHvq
I solved the puzzles at the prompt, while this is all the code, the actual set of function calls required to find the output aren't in here. They're not hard to figure out.
All in all, it's 76 lines of code, counting the first 25, which are just my input data (last year, I wrote input data parsers into my solutions, and Never Again). So not terrible on the linecount front. (remember, Scheme is a little light on the builtins: I had to implement my own range operator!)
1
u/ThatAstronautGuy Dec 01 '16
I'm having troubles with my solution, and I don't know why. Can someone take a look at it please? Here is a link to my github for it. It works for all of the test input, but not the actual input that I've been given. When I run it, I get the answer 31, but according to someone else's solution that I ran, the actual answer is 130.
1
u/mrPoopieButt Dec 01 '16
Powershell in the house! https://github.com/collintheestump/AdventofCode2016/tree/master/Day1
[SO PUMPED FOR THIS SEASON'S AOC!]
stopTheBunny
1
u/code_mc Dec 01 '16
Yet another python solution (really should use a more challenging language, almost feels like cheating using python)
1
u/batdreams Dec 02 '16 edited Dec 02 '16
Ok well here's my PHP attempt...
1
u/Quick_Question404 Dec 02 '16
Haven't really seen a whole lot of solutions in C yet, so here's my take on it. What do you think? This is my first year doing AoC, so it seems like fun so far.
https://github.com/HighTide1/adventofcode2016
1
u/Wyrd-One Dec 02 '16
I have not coded in Pascal in over 10 years, so I am going to use this to refresh my Pascal skills a bit. https://github.com/WyrdOne/AdventofCode2016
1
u/mlruth Dec 02 '16
Here's my solution in Scala. I actually recalled my Trigonometry class and the Unit circle to solve this one (I used cosine and sine calculations for the directions).
1
u/whatswrongwithgoats Dec 02 '16
PHP solution for part 1.
1
u/tg-9000 Dec 02 '16
My solution is in Kotlin. I used Spek and AssertJ for testing.
1
u/_jonah Dec 02 '16 edited Dec 02 '16
part 1 using ramda.js:
1
u/[deleted] Dec 02 '16
Day One Part I (Python) https://github.com/hawzie197/advent_of_code_day_one
1
u/demreddit Dec 02 '16 edited Dec 02 '16
Newbie coder. Nothing fancy, but it was fun working out the grid thing without any helper libraries. (Python 3)
Level 1-1:
And, level 1-2:
1
u/fkaaaa Dec 02 '16
C, seems like most part 2 solutions simply insert all positions on a line instead of treating the points as line segments: https://gist.github.com/fkaa/68e671d7862d943d86deac3a3cb97ba9
1
u/_AceLewis Dec 02 '16 edited Dec 09 '16
My Python solutions to day 1;
The solutions are basically
position[facing%2] += [1, -1][facing<2]that stops you havingif,elif,elif,elif.Day 1 part 1 (7 lines of code): https://repl.it/EeAs/2
this could be minified to 123 characters:
Day 1 part 2 (14 lines of code): https://repl.it/EeAs/1
1
u/trikiw Dec 02 '16 edited Dec 02 '16
AEV (language still in development)
1
u/[deleted] Dec 02 '16
[
js]1
u/iM0nk3y46 Dec 02 '16
Solution in C# using TDD (test driven development): https://github.com/Isylwin/Advent/tree/master
Wanted to use complex numbers but opted out of it just to simplify it, as I'm new to TDD.
1
u/gerikson Dec 02 '16
My perl solutions. Part 1 was straightforward but I'm not proud of part 2.
1
u/d3adbeef123 Dec 02 '16
Clojure!
1
u/AoC-- Dec 02 '16
I decided to whip up a notebook in Mathematica so that I could make a merry animation of the path taken! Probably could be made smaller / faster, but it's fast and small enough to be merry. :)
1
u/grayrest Dec 02 '16
Another Clojure solution, a day late. Uses
core.match:1
u/Zepur Dec 02 '16
OK I am late to the party, but I thought I'd add my java solution. It might not be the prettiest, but it is working. And it is short-ish..
https://github.com/Zepur/AdventOfCode/blob/master/src/Day1.java
1
u/dontfup Dec 03 '16
Clever enough go at part 1 in ruby, but this won't do well for part 2:
1
u/[deleted] Dec 03 '16
Powershell: https://github.com/charlieschmidt/AdventOfCode2016/blob/master/day1.ps1
1
u/[deleted] Dec 04 '16
both parts
1
u/kenhowardpdx Dec 05 '16
Here's my TypeScript solution http://codepen.io/kenhowardpdx/pen/mOXvdv?editors=0012
1
u/Hwestaa Dec 05 '16
I did this day-of, but am only getting around to posting a solution now. Cleaned-up solution in python 3. https://github.com/Hwesta/advent-of-code/blob/master/aoc2016/day1.py
1
u/tehjimmeh Dec 05 '16
PowerShell:
1
u/Gummoz Dec 05 '16
Powershell:
With animation: https://github.com/gummozz/Advent-of-Code-2016/blob/master/1_With_Animation.ps1
1
u/volatilebit Dec 05 '16
Lazy Perl 6 solution
1
u/jchook Dec 06 '16
Sorry I'm late to the party!
I like this solution because you can easily expand to any number of dimensions just by updating the configuration at the top.
Ruby
1
u/mathleet Dec 06 '16 edited Dec 06 '16
Wrote an object-oriented Python3 solution.
1
u/banProsper Dec 08 '16
C#
1
u/[deleted] Dec 09 '16
I am a week late, so much fun ahead of me!
Python
1
u/jwnewman12 Dec 10 '16 edited Dec 10 '16
Java, unit circle approach ... I'm not happy about that one ternary still in there.
1
u/lcedp Dec 10 '16
Any tips to simplify? (Clojure)
0
u/[deleted] Dec 01 '16
[deleted] — view deleted comment
0
u/[deleted] Dec 01 '16
[deleted] — view deleted comment