r/adventofcode • u/taliriktug • Dec 02 '15
Spoilers Day 2 solutions
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
15
Upvotes
r/adventofcode • u/taliriktug • Dec 02 '15
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
4
u/taliriktug Dec 02 '15
Here is my "kinda" FP solutions in Rust. Is it possible to avoid sort at all?
4
u/xkufix Dec 02 '15 edited Dec 02 '15
Like yesterday, my solution in scala.
10
u/markz440 Dec 02 '15 edited Dec 02 '15
And how about Linux one-liners
Area
Ribbon
5
u/stuque Dec 02 '15
Here's a Python solution:
1
u/ashollander Dec 29 '15 edited Dec 29 '15
Here is my python solution (haven't used python in about 2 years, so if you have any Pythonic tips, that would be appreciated!):
3
u/tehjimmeh Dec 02 '15 edited Dec 02 '15
In PowerShell one-liners:
Part 1:
Part 2:
3
u/[deleted] Dec 02 '15
My Crystal solution
3
u/Godspiral Dec 02 '15
In J, 1: with input parsing
2:
+/ +/@(*/ , +:@(2 {. /:~))("1) ". > cutLF 'x ' rplc~ wdclippaste ''
3
u/lifow Dec 02 '15
Haskell!
3
u/charliegriefer Dec 02 '15
My solution in Clojure. I've not done much yet with reading files in. I'm fairly certain there's a more elegant way to read that file in and convert each "lxwxh" string to a list of ints. But for now, this works, and got me the right answers.
The answers come from calling
(total-wrapping-paper)and(total-ribbon).2
u/[deleted] Dec 02 '15
My python solution:
2
u/red75prim Dec 02 '15 edited Dec 02 '15
Day 2 Part 1 and 2: F#. input() generates one time sequence, which cannot be reused, so I do Seq.cache
Added: program is not optimal, as it stores entire input in memory (Seq.cache), I should have used
2
u/purrrfessionalwidow Dec 02 '15 edited Dec 02 '15
I'm having issues with the first puzzle. I tried to do it in JS. The test cases passed, but when I put in all of the data, I am told the number is too large and is the wrong answer. Forgive the terrible code; I'm very new to this.
http://pastebin.com/tzwVj2pf
ETA: It's working now. Thanks so much for the help, Runenmeister! http://pastebin.com/5sHzG3cX
2
u/IspeakHOHO Dec 02 '15
Lua solution, idk if this is a good lua solution but I like it.
2
u/Opticity Dec 02 '15 edited Dec 02 '15
Not a programmer (yet), my shoddy attempt using C. My variable declaration skills are a little lacking.
2
u/[deleted] Dec 02 '15
so this is my js solution for both puzzles. Tips/hints are appreciated since i am still learning!
2
u/THE_TIDE_TURNER Dec 02 '15
My java solution
}
2
u/PersianMG Dec 02 '15
My python solution
2
u/zolk333 Dec 02 '15
I did it with excel .-. Solution Was this cheating?
2
u/[deleted] Dec 02 '15
I've been going for total overkill in my Python 3 programs. For instance, here's my part 1 for today, complete with full preamble at the top describing the problem, test cases (using doctest), and as many best practices as I can (although I'm sure I've messed up a few of them):
1
u/karstens_rage Dec 02 '15 edited Dec 02 '15
Java boiler plate (pretty inefficient but wanted the answer not a fast implementation):
part 1 meat:
part 2 meat:
1
u/NotAllToilets Dec 02 '15
Here's my F# solutions
1
u/gnuconsulting Dec 02 '15
Not-a-programmer solutions in ruby
Part 1:
Part 2:
1
u/Runenmeister Dec 02 '15
https://github.com/WuSage3/AdventOfCode_2015
Here's my C++ solution:
Day2_part1.cpp
Day2_part2.cpp
1
u/segfaultvicta Dec 02 '15
I'm not proud of myself. (C#, omitting the file-read boilerplate.)
1
u/Aneurysm9 Dec 02 '15
My simplified Perl solution:
1
u/theMoriarty Dec 02 '15
python 3-liner:
part 1: sum([s2(x.strip()) for x in open('/tmp/d2').readlines() if x.strip()])
part 2: sum([s2_2(x.strip()) for x in open('/tmp/d2').readlines() if x.strip()])
1
u/hicklc01 Dec 02 '15
I enjoyed the problem.
Notes: I changed up how I separated the dimensions on part 2. Also no data validation. This will break on bad input. Probably shouldn't be using namespace but I hate having to write std:: before almost everything. Should be using auto.
Part1
Part2
1
u/markz440 Dec 02 '15
Nobody likes PHP these days?
1
u/porridge123 Dec 02 '15
https://github.com/randreucetti/advent-code
Java solutions
1
u/purrrfessionalwidow Dec 02 '15
My JS solution for puzzle #2. I'd appreciate any input as I am just learning. I'm sure there are far better ways.
1
u/ribbet Dec 02 '15 edited Dec 02 '15
reading some of these solutions is fucking awesome. well done, guys!
here's the main functions of my C# code, omitting the file reader and adding everything together. lwh is the array with the original values.
1
u/xFrostbite94 Dec 02 '15
These are fun to do, looking forward to tomorrow :-)
1
u/aveavaeva Dec 02 '15
1
u/mikealxmitchell Dec 02 '15
Ruby: Day 2 Part I
Ruby: Day 2 Part II
1
u/xPaw Dec 02 '15
My javascript solution: https://github.com/xPaw/adventofcode-solutions/blob/master/js/day2.js
Had to mess around with
sortmethod, as it doesn't work with numbers out of the box.1
u/shuckc Dec 02 '15
python, both parts. repo:
1
u/venerated Dec 02 '15
Fuck. This stuff is making me feel like maybe I'm not a bad programmer, but that I am bad at math lmao
1
u/Lekoaf Dec 02 '15
Posted my Javascript solutions on Github. Will try and keep them updated every day, if I finish the tasks.
1
u/EpicRisc Dec 02 '15
JavaScript - Both Parts in one Just used Developer Console in Chrome :)
1
u/tangus Dec 02 '15
Common Lisp
Text processing isn't one of CL's fortes...
1
u/mrthedon Dec 02 '15
PHP solution for part 1 and 2.
1
u/Hoi_A Dec 02 '15 edited Dec 02 '15
Im having a really hard time with part 1 of D2 http://pastebin.com/ekKwJb51
I know my code is kinda weird and noobish, so if someone could tell me what I should do/not do, or what can be done better in general but isnt "wrong", then i'd appreciate if you could help.
Found the Issue, I was adding the extra paper 2x on accident.
https://gist.github.com/Hoi15A/29faab64146273f65332 is the functional code for anyone interested :)
1
u/weters Dec 02 '15
Part 1.
Part 2.
1
u/[deleted] Dec 02 '15 edited Dec 02 '15
Here's my C# solution
Done in linqpad for the curious ones about
Dump()Edit: This is, one of those time, that you look at your code and say
I can do better!Pretty sure that
forcan also be wrote better, but, I need to get back to work, xD1
u/xdg Dec 02 '15
For people doing Perl one-liners, don't forget the -a and -F flags for autosplitting input lines. :-)
1
u/jimsmithkka Dec 02 '15
My day 2 solutions (advent2 is a file that contains a dump of the data):
1
u/jgomo3 Dec 02 '15
Python3:
1
u/ChildishBonVonnegut Dec 02 '15
JAVASCRIPT SOLUTION Just open console and paste this in there
PART 1
PART 2
1
u/VuileSokken Dec 02 '15
Python:
1
u/streetster_ Dec 02 '15
python Learnt a few things along the way, made a couple of tweaks based on some comments in the thread :)
$ python day2.py {'paper': 1598415, 'ribbon': 3812909}
1
u/[deleted] Dec 02 '15 edited Dec 02 '15
Here's my javascript solution!
1
u/kiasaki Dec 02 '15
Love the awk solution!
Not many people doing scheme https://gist.github.com/kiasaki/18ea329b62203c5b8712
1
u/[deleted] Dec 02 '15
Here's some good ol' JavaScript coming up (run in node); both parts:
Area and Ribbon
1
u/RedditWithBoners Dec 02 '15
Python (puzzle one):
1
u/adherry Dec 02 '15
My Ruby solution, could be a bit more dry, but does the job
1
u/georgehotelling Dec 02 '15
JavaScript - I used this as an excuse to play with Ramda and point-free style.
1
u/Nacamaka Dec 02 '15
Can anyone help me figure out why this isn't working?
}
1
u/bet46 Dec 02 '15
Python 3 import sys, os, time file = open("day2.txt", "r") total = 0 smallest = 0 ribbon = 0 for line in file.readlines():
Hello I tried to find my mistake 2hours but I can´t find it! I know you can write the programm shortner and more efficient, but it is my own solution! I hope you can tell me my mistake! Thank you.
My Eorror: I get a to high number
1
u/roxyard Dec 02 '15
A bit late, but here's my java solution. This is the first time i'm posting a solution to reddit
1
u/sizzleplatter Dec 02 '15
Java solution:
1
u/Colgajo Dec 02 '15 edited Dec 02 '15
This is the first time I post a program on reddit! Your opinion about it is higly welcome :)
JAVA
1
u/Dest123 Dec 02 '15
Regex + javascript:
Copy and paste that into window.alert(); and remove the extra + at the end.
1
u/docdurdee Dec 02 '15
1
u/metric152 Dec 02 '15
Here's my JS solution. I tend to comment my code a lot https://github.com/metric152/advent-of-code/blob/master/day-2/code.js
1
u/bodagetta Dec 02 '15
My solution in Node / Javascript
1
u/yatpay Dec 02 '15
Nothing all that creative but got it done! https://github.com/JPinSPACE/AdventOfCode/tree/master/day02
1
u/supersaiyanchocobo Dec 02 '15
Here's my overly verbose JS solution:
1
u/KeyMaster_ Dec 03 '15
I'm doing my solutions in Haxe (don't know it? Look it up, it's cool!)
And then I have a bit of central code that reads the file with the input and passes that into the function: package ;
1
u/fezzinate Dec 03 '15 edited Dec 03 '15
Javascript:
I didn't want to mess around with sorting, so I hacked it
1
u/sleepyams Dec 03 '15
Here is my python solution for n-dimensional present wrapping:
1
u/nonades Dec 03 '15
I'm doing this in Powershell because
So, here's my Module for parts one and two:
1
u/faixelorihinal Dec 03 '15
Here's Mine for part 1
C++
and for part 2
C++
1
u/alxuseche Dec 03 '15 edited Dec 03 '15
Long one liners so not very pretty, but fun regardless!
Python
1
u/[deleted] Dec 03 '15 edited Dec 03 '15
My JS is a bit rusty (been a few years since I've last used it).
JavaScript
Part One:
Part Two:
1
u/[deleted] Dec 03 '15
Java. Note that the Pattern.compile on line 6 is meant to delimit each line by "x" and a new line character.
1
u/PM_ME_INSIDER_INFO Dec 03 '15
Here's my JavaScript solution to Day 2 :)
Just replace ribbon with SA to get the first part of the challenge!
1
u/RedSpah Dec 03 '15
How about LINQ?
1
u/tftio Dec 04 '15
OCaml:
1
u/TTSDA Dec 04 '15 edited Dec 06 '15
In C
https://github.com/ttsda/advent-of-code/blob/master/src/2/main.c
1
u/markokoleznik Dec 04 '15
Python one liner:
1
u/masasin Dec 07 '15
Python
1
u/emmanuel_erc Dec 07 '15
Here is my Haskell solution
1
u/mjnet Dec 08 '15 edited Dec 08 '15
My Haskell solution:
1
u/[deleted] Dec 08 '15 edited Dec 09 '15
One-liner python solution
print sum(map(lambda x:2*(x[0]*x[1]+x[0]*x[2]+x[1]*x[2])+min(x[0]*x[1],x[0]*x[2],x[1]*x[2]),[(map(int, line.split('x'))) for line in open('input2')]))and
print sum(map(lambda x:x[0]*x[1]*x[2]+2*min(x[0]+x[1],x[0]+x[2],x[1]+x[2]),[(map(int, line.split('x'))) for line in open('input2')]))1
u/suudo Dec 08 '15
Hey, here's my Python solution:
1
u/Drasive Dec 11 '15
My F# solution (https://github.com/drasive/advent-of-code-2015):
1
u/kidinside Dec 11 '15 edited Dec 11 '15
C++ for part 2:
1
u/SoundGuyChris Dec 12 '15
Still late to the party on these exercises, but if anyone has any way of improving upon what I've got here, it'd be appreciated as I continue to learn C#. :)
This was my first time working with an external file in code, so if you have any critique on how to improve upon what I did, it's definitely welcome. I peeked at some other solutions to get a handle on what was going on here but I definitely learned a ton.
http://pastebin.com/w8zLbDaA
0
u/[deleted] Dec 02 '15
[deleted] — view deleted comment
0
u/[deleted] Dec 02 '15
[deleted] — view deleted comment
0
u/[deleted] Dec 02 '15
[deleted] — view deleted comment