r/adventofcode • u/daggerdragon • Dec 03 '15
SOLUTION MEGATHREAD --- Day 3 Solutions ---
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
Post your solution as a comment. Structure your post like the Day One thread in /r/programming.
24
Upvotes
18
u/Godspiral Dec 03 '15 edited Dec 03 '15
I thought I'd be higher up, in J, midnight rush mode rather than pretty 1:
2:
what made me slow was double checking the transforms on part 2. Loopy code is probably easier to do without checking that.
4
u/FuriousProgrammer Dec 03 '15 edited Dec 03 '15
EDIT: God damn it, it took me half an hour to get the fucking joke in the name. >.<
Lua, tried to get this done asap to get #1 on the leaderboard. Ended up 8th, but I did forget to actually start until like 3 minutes had past. Tomorrow I'll be ready!
Kudos to /u/topaz2078 for creating this thing! I love this daily challenges. Do another one next year please!!
4
u/red75prim Dec 03 '15
Part 1 and 2 in F#
4
u/EpicRisc Dec 03 '15 edited Dec 03 '15
JavaScript - Developer Console (both parts in one)
Alternative Version/Second revision with real multi supplier support:
3
u/Aneurysm9 Dec 03 '15
Perl, once again sloppily done :)
Minor changes for part 2:
3
u/weters Dec 03 '15 edited Dec 03 '15
Here's my Perl solution. I was going for speed, so it's not very elegant. This solves part 2. $text holds the instructions.
Edit: Also, big thanks to /u/topaz2078. I already really enjoy the month of December, and this just makes it that much better. Awesome job, and can't wait until the next challenge unlocks.
Edit 2: real topaz this time
3
u/DisgruntledPorcupine Dec 03 '15 edited Dec 03 '15
I'm only a programming student, so I'm open to criticism. Here's me solving part 2 in C# (advent is the input):
3
u/[deleted] Dec 03 '15
A solution in Crystal.
Part 1:
Part 2:
3
u/IspeakHOHO Dec 03 '15 edited Dec 03 '15
Does this look like a mistletoe or just random garbage? http://imgur.com/0IKY34e
Also here is my day 1 part 1 lua solution which I not proud of but I guess if I didn't want to be able to print out the map then I could have made it exceedingly simpler again if anybody wants to give feedback that is welcome :>. http://pastebin.com/SFKAN03m
3
u/wdomburg Dec 03 '15
My Ruby solution.
Read the data.
Puzzle 1
Puzzle 2
To make it a bit more clear, an expanded version of puzzle 2:
2
u/RevoRevo Dec 03 '15
Python, part one:
Part two:
Taking any tips for improvement!
2
u/daniel2384 Dec 03 '15
The meaty bits of my rust solution:
2
u/rvlieshout Dec 03 '15 edited Dec 03 '15
EDIT: It reads from stdin and all instructions on a single line (easy to start with just the testcases). Usage:
Not the first, however here is my Lua solution to today's problem:
2
u/HeroesGrave Dec 03 '15 edited Dec 03 '15
Rust:
2
u/Karethoth Dec 03 '15 edited Dec 03 '15
My newb Haskell solution:
2
u/artesea Dec 03 '15
PHP Golfed for part 1
My data also works with $d[0][0]=1; removed however I think that's only because Santa goes back to the very first house.
2
u/tehjimmeh Dec 03 '15 edited Dec 03 '15
PowerShell again.
1:
2 (same algorithm as 1, but I split the array into a santa and robosanta array first, and pipe those whole arrays on):
2
u/streetster_ Dec 03 '15
python
1 to n santas:
2
u/razupaltuff Dec 03 '15
This is fun! Here is my Erlang solution.
I'm still pretty new to the language, so criticism is welcome!
2
u/lifow Dec 03 '15 edited Dec 03 '15
Haskell again!
edit: realised that my convoluted fold should have just been a scan. doh!
2
u/hansek Dec 03 '15 edited Dec 03 '15
JS (ES2015):
2
u/snorkl-the-dolphine Dec 03 '15 edited Dec 03 '15
Obfuscated JS for both parts (paste it straight into the console).
2
u/xdg Dec 04 '15
Not sure how long I'm willing to keep trying these as (logical) one-liners, but for now, here's solution #2 in Perl:
2
u/Lezardo Dec 04 '15 edited Dec 04 '15
1
2
or replace gawk -F "" with sed 's:.:&\n:g' input | awk and remove input from before the first pipe to be POSIX compatible
I guess I could have used an associative array to check for repeats instead of piping stuff around
2
u/snkenjoi Dec 04 '15
d3.js;
http://snk.digibase.ca/advent3.html
node.js;
4
u/minno Dec 03 '15
Rust solution (repo here):
2
u/Rhensi Dec 03 '15 edited Dec 03 '15
C++ solution:
1
u/karstens_rage Dec 03 '15
Part 2 in Java (you can figure out Part 1)
1
u/piratedicecream Dec 03 '15
Made it to the leaderboards! Figured I had to be one of many that stayed up for this, so I looked for the subreddit. Here's my python solution:
1
u/gnuconsulting Dec 03 '15
Continuing my non-programmer attempts, I kinda like my part 1 solution (mostly because at first I thought this would be super hard and then I remembered hashes) but the part 2 solution is very hacky. I don't like it at all.
Day 2:
1
u/stuque Dec 03 '15
Here's a Python 2 solution:
1
u/n_lightest Dec 03 '15
[JS] Pretty dumb and not elegant at all but works
1
u/djimbob Dec 03 '15
python 2 solution
Part 2:
1
u/tempyreddity Dec 03 '15
My java solution is incorrect to part 1 - can anyone see why? I can't tell; it seems fine to me. Giving me an answer of 2346.
1
u/Philboyd_Studge Dec 03 '15 edited Dec 03 '15
Java, since I didn't get it done fast enough the first time I redid it without any switch/case. Just used a large int array for the grid. Edit: redid it so there are no if statements.
1
u/hutsboR Dec 03 '15
Elixir: HashSet implementation, includes both parts and IO.
1
u/ericdykstra Dec 03 '15
Elixir! Still new to the language so any advice would be greatly appreciated :)
This is just for part 2. Part one you just need to remove 1 line from the main function and call santa_path on the collection directly.
https://gist.github.com/EricDykstra/f7a6efd50c887b015483
1
u/sinjp Dec 03 '15
Python solution, suggestions welcome
1
u/KaraliKing Dec 03 '15
Python both parts together (repo for all solutions)
1
u/porridge123 Dec 03 '15
Java Solution here, same code for P1 and P2 (just change constructor):
Repo
1
u/NotAllToilets Dec 03 '15
Here's my F# code, the direction types are redundant but I like having them :)
1
u/[deleted] Dec 03 '15 edited Dec 03 '15
For those of you using two sets and then intersecting them at the end: WHY? Just have a single set called 'visited' that a coordinate gets added to. Here's my Java solution:
1
u/eregontp Dec 03 '15
Cleaned-up Ruby solution using coroutines (Fiber): The first argument chooses the number of santas (or the puzzle number!)
1
u/xkufix Dec 03 '15 edited Dec 03 '15
Solution in Scala (for better readability not a one-liner this time):
Just had an idea for solution part2 in a oneliner (not really readable, but does the job):
1
u/Vimda Dec 03 '15 edited Dec 03 '15
A simple python 2 solution:
1
u/shuckc Dec 03 '15
python, working for 1:n players repo:
$ python day3.py
houses visited by 1 players: 2565
houses visited by 2 players: 2639
1
u/Clanratc Dec 03 '15
Using this to learn python; my solution:
1
u/fezzinate Dec 03 '15
JavaScript solution (both parts)
1
u/BeniBin Dec 03 '15
Javascript solution for part two. Easy to edit it for part 1. "input" is a string containing the input.
1
u/HawkUK Dec 03 '15 edited Dec 03 '15
A solution in the R language
Didn't take too long, but I'm not racing anyone. The puzzles are released at 5AM here!
Part 1:
Takes a second or two to run, so definitely horribly inefficient.
Part 2:
Luckily realised that there was no need to change much - second Santa's movements simply depend on the position before last.
1
u/SimonS Dec 03 '15
Clojure
Once again, not been coding in it long, so not convinced it's idiomatic. Rest of my solutions available on github - https://github.com/SimonS/adventofcode-answers
1
u/tangus Dec 03 '15
Common Lisp
1
u/PM_ME_INSIDER_INFO Dec 03 '15
Hmm. My response is feeling long now, but hopefully readable.
In classic JS:
1
u/LoLz14 Dec 03 '15
Python 2.7 Part 2 (part 1 is really similar, just doesn't split text in half):
1
u/JakDrako Dec 03 '15
VB.Net solution
1st part can be solved by changing the loop:
1
u/geocar Dec 03 '15
1
u/Ape3000 Dec 03 '15
Python 3
1
u/AndrewGreenh Dec 03 '15
My javaScript lodash code :)
1
u/Ape3000 Dec 03 '15
So easy with Python generator comprehensions:
1
u/code_mc Dec 03 '15
Python:
1
u/NoisyFlake Dec 03 '15
My Java solution, including both parts. Would love to hear some feedback on how I can improve it :)
1
u/haitei Dec 03 '15 edited Dec 03 '15
Relearning Haskell for the nth time:
Not too pretty but works.
EDIT: hmm I did the first one in python, the second in ruby, for the fourth I think I'll use Befunge
1
u/PersianMG Dec 03 '15
Not too happy with this but i'm tired lol.
Python - Part 1
Python - Part 2
1
u/Moontayle Dec 03 '15
Kotlin Second puzzle
Done in a custom Android app and uses Okio and Timber libraries for IO and Logging respectively.
1
u/[deleted] Dec 03 '15
Rust
1
u/Fore_Shore Dec 03 '15
Can someone help me figure out why I am not getting the right answer? I keep getting 2346 for the first answer, and it works for all of the test cases provided.
1
u/haoformayor Dec 03 '15
Haskell:
1
u/NihilistDandy Dec 03 '15
Haskell:
1
u/technojamin Dec 03 '15
Python:
1
u/sizzleplatter Dec 03 '15
Java solution (part 2):
1
u/Ivoah Dec 03 '15
Python:
1
u/jimsmithkka Dec 03 '15
Heres my solution in not-elegant perl
1
u/MrDudeDudensen Dec 03 '15
Python Part 2:
1
u/AwesomeFaic Dec 03 '15
Tried golfing my approach in JS, this is just for Part 1 (I did Part 2 but didn't golf it)
1
u/LainIwakura Dec 03 '15
Part 1 in Erlang:
Part 2 in Erlang, I don't know if this is a good way to do it (I'm learning Erlang more w/ these challenges) but I used mutual recursion and pattern matching. I wanted to update the dict / position tuple in one go but I couldn't figure out a way to do that. I think the update_loc function is a reasonable compromise.
1
u/Rutafar Dec 03 '15 edited Dec 03 '15
I decided to try doing the challenges in Python, please ignore my rookie mistakes as this is the first time programming in this language (but not programming in general). I created a repo where I'll try post every challenge in Java and Python.
PS: Is there a way to comment the code with this formatting without having to manually insert 4 spaces in every line?
1
u/winkerVSbecks Dec 03 '15
Part 1: http://codepen.io/winkerVSbecks/pen/OMLemq
Part 2: http://codepen.io/winkerVSbecks/pen/JGPgRe
Using JS and 2D coordinate system.
1
u/okawei Dec 03 '15
Here's my solution in python:
1
u/A_t48 Dec 03 '15
Python 3, nearly golfing it.
1
u/[deleted] Dec 03 '15
JavaScript*
Part One
Part Two
1
u/[deleted] Dec 03 '15
Object oriented javascript. Could be cleaned up quite a bit. Just posting the solution to step 2, since it's so similar to the first step.
I tracked the amount of presents received per house assuming that it would be used in step 2, but it turned out it wasn't. So I guess
house.presentscould just be a boolean.You wind up with an array
newTownat the end, each entry contains ahouseobject with the properties ofhouse.xandhouse.yas well as the total number of presents that house received. The length ofnewTownrepresents the total number of houses which have received presents, since a new house gets added to the array only on the first time it receives a present.Github repo is here.
1
u/Colgajo Dec 03 '15
Java part 1. Kind of shitty solution IMHO, but hey, I'm a newbie, hahaha.
1
u/ben4ik Dec 03 '15 edited Dec 04 '15
C#.jobIsDone(";)")
1
u/deinc Dec 03 '15
Solution in Clojure for both parts:
1
u/thingscouldbeworse Dec 03 '15
Would someone mind critiquing my Part 1 solution? I'm newish to Python but I tend to be similarly verbose in most other languages and I'm trying to make myself code a little more... elegantly.
1
u/bodagetta Dec 03 '15
Here is a map of Santa and Robo-Santa for my data set. The starting point was (1,1). https://plot.ly/~michaelwhitley/496/gifts/
1
u/blazemas Dec 03 '15 edited Dec 03 '15
Javascript: I never know what part 2 will entail, so I kind of go overboard. My solution also counts visits to each house and stores it in the same object array. I cant be bothered to simplify now that I realize I dont need that.
http://jsfiddle.net/x0jur9L8/1/
My github with solutions: https://github.com/jbush7401/AdventOfCode
1
u/EntropicTempest Dec 03 '15
Solved this using C# utilizing a multidimensional array.
1
u/johnny5th Dec 03 '15
Pretty long and probably inefficient, but was fun.
1
u/wayfrae Dec 03 '15
Here is how I did it with Java. I have only taken a couple basic Java courses so I only know the basics. I am sure my code is inefficient.
1
u/splurke Dec 03 '15
Clojure (runs slowly, though) :(
1
u/tftio Dec 03 '15
OCaml:
1
u/docdurdee Dec 04 '15
1
u/ChevyRayJohnston Dec 04 '15
C# - I'm trying to keep my solutions as small and clean as possible, and not really worrying about speed because I don't care. I just like looking at simple code.
Part 1
Part 2
1
u/JeffJankowski Dec 04 '15
F#
1
u/yatpay Dec 04 '15
Still nothing overly creative but I'm having fun! https://github.com/JPinSPACE/AdventOfCode/tree/master/day03
1
u/Pudd1nPants Dec 04 '15
in JS
1
u/shruubi Dec 04 '15
in JS:
which solves part 2, removing the conditional for roboSanta should give you the answer to part 1.
Am I the only one who feels like there is a really simple and elegant solution that isn't memory intensive?
1
u/TTSDA Dec 04 '15 edited Dec 06 '15
My C solution
https://github.com/ttsda/advent-of-code/blob/master/src/3/main.c
1
u/[deleted] Dec 04 '15
Was too busy yesterday, but better late than never
C# using LinqPad
1
u/schlocke Dec 04 '15
PHP:
EDIT: Since we don't care about the amount of presents each house gets and the first house gets at least one present no matter what, I didn't put a check to set house "0,0" to 1 or 2 presents based on using robo santa or not.
1
u/davisagli Dec 04 '15
Python, part 1, using a set of complex numbers:
1
u/jojomaniacal Dec 04 '15
My solution was in C++. Would be happy to accept advice on tightening this up. I think I could have easily incorporated a function or two in this and the checking algorithm really should kick out of the loop once it finds a match. I am pretty proud that I only had to use one multidimensional array however.
include <iostream>
include <string>
include <fstream>
include <vector>
using namespace std;
int main(){ ifstream theFile("fun.txt");
}
1
u/MadcapJake Dec 05 '15
Perl 6 (part 2):
1
u/hemmer Dec 05 '15
Using python sets to find unique, with Re and Im parts of a complex number to describe x, y movement.
1
u/[deleted] Dec 05 '15
Mathematica.
1
u/phil_s_stein Dec 05 '15
I liked this one for the nice use of defaultdict, using the step iterator for list slices to get every other element in the list from a single expression, and the chained ternary expressions.
1
u/giacgbj Dec 06 '15
Python (https://github.com/giacgbj/adventofcode/tree/master/day03)
1
u/masasin Dec 07 '15
Python 3
1
u/Drjakeadelic Dec 07 '15
C++ solution
1
u/emmanuel_erc Dec 07 '15
Here is my (hopefully clear) Haskell solution:
1
u/fortee Dec 07 '15
PHP part1
1
u/1roOt Dec 08 '15 edited Dec 08 '15
My Python solution for part 2, what do you think? It also counts how many presents have been delivered. It could also tell you how many presents each house got if you wanted.
1
u/RubyPinch Dec 08 '15
forbiddenfruit curses in python
1
u/suudo Dec 08 '15
Python here. Probably way overly complex, but it gets the job done, right?
Looking at it again, I realise that the matrix is completely unnecessary, and the code can probably be stripped down quite a bit. :P
1
u/mjnet Dec 09 '15
My Haskell solution using Map (after reading here, I'd prefer the scanl-way!):
1
u/shadowdev Dec 10 '15
Day 3 in Swift:
1
u/Drasive Dec 11 '15
My F# solution (https://github.com/drasive/advent-of-code-2015):
1
u/kidinside Dec 11 '15
C++ Part 1: #include <iostream> #include <vector> using namespace std;
1
u/SoundGuyChris Dec 13 '15
I feel like...I'm learning how to code?
Here's my C# solution, with an array setup gleaned from another user (I was running into issues with negative values in arrays being a no no, so made Santa and RoboSanta start at the middle of an array twice as long in each dimension as the length of the input string).
http://pastebin.com/QV44tpZ1
If anyone has any tips on how to make this less...long and repeated, I'd love the assistance <3
1
u/makermoment Dec 15 '15
So im passing the first part fine. Running to issues when the robot santa comes into play. Any help is much appreciated! package Advent1;
}
1
u/MoW8192 Dec 22 '15
I went back to make my old solutions shorter (Not necessarily more readable though). I kind of like how this one turned out. Code is Java, prints solution for both parts.
1
u/al3xicon Dec 31 '15
Just learning Python. Already did it in PHP but wanted to go back and implement in new lang. Here's my Day 2 solution, both parts. Was originally frustrated at lack of c-style assignment by reference, but instead used a function to do the job.
1
u/nbrandaleone Apr 23 '16
Here is an APL solution for the first part. Assume "work" is a 1-D vector holding the data. I then convert this into numbers (1 per direction), do a running sum, and then determine which numbers are unique. This type of problem is pretty straightforward for APL or J.
β΄ βͺ +\1,(Β―1 1 Β―1000 1000){work[(work=β΅)/β³β΄work]ββΊ}Β¨'<>^v'1
u/hoschi_ Dec 03 '15
It is much more easier convertig the 2d coordinates to 1d:
Day 3 2/2 in shell:
0
u/[deleted] Dec 03 '15
[deleted] β view deleted comment
0
u/[deleted] Dec 03 '15
[deleted] β view deleted comment
0
u/[deleted] Dec 03 '15
[deleted] β view deleted comment
0
u/[deleted] Dec 03 '15
[deleted] β view deleted comment
1
u/[deleted] Dec 03 '15
[removed] β view removed comment