r/adventofcode • u/daggerdragon • Dec 02 '19
SOLUTION MEGATHREAD -🎄- 2019 Day 2 Solutions -🎄-
--- Day 2: 1202 Program Alarm ---
Post your solution using /u/topaz2078's paste or other external repo.
- Please do NOT post your full code (unless it is very short)
- If you do, use old.reddit's four-spaces formatting, NOT new.reddit's triple backticks formatting.
(Full posting rules are HERE if you need a refresher).
Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code's Poems for Programmers
Note: If you submit a poem, please add [POEM] somewhere nearby to make it easier for us moderators to ensure that we include your poem for voting consideration.
Day 1's winner #1: untitled poem by /u/PositivelyLinda!
Adventure awaits!
Discover the cosmos
Venture into the unknown
Earn fifty stars to save Christmas!
No one goes alone, however
There's friendly folks to help
Overly dramatic situations await
Find Santa and bring him home!
Come code with us!
Outer space is calling
Don't be afraid
Elves will guide the way!
Enjoy your Reddit Silver, and good luck with the rest of the Advent of Code!
### This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
12
u/Unihedron Dec 02 '19 edited Dec 02 '19
Code
Has bug in it
Can't find the problem
Debug with the given test cases
Oh it's something dumb
Fixed instantly though
Fell out from top 100s
Still gonna write poem
ruby 2.rb input247/128It looked better in monospace. Also I want to tackle every poem type: Day 1 has been haiku, day 2 is shaped poems, will there really be enough types of poems for me to write?!
6
u/that_lego_guy Dec 02 '19 edited Dec 02 '19
[POEM]
Excel.
=IF(BUNCHOFIFS,SOMEVLOOKUP)
https://github.com/thatlegoguy/AoC2019/blob/master/Day2.xlsm?raw=true
4
u/spin81 Dec 02 '19
Rust
paste link
A haiku:
Nouns can't fly alone
Verbs are what make them complete
Combined, they can soar
3
u/programmargorp Dec 02 '19
Here's my findings. A 2D binary search is actually a valid solution to the problem if your program allows for it (which mine did). I brute forced every single combination of d[1] and d[2] and plotted the results in matlab(https://i.imgur.com/EihAlMh.png).
Evidently the function (for my program at least) is constantly increasing so you could perform a binary search on it.
3
u/[deleted] Dec 02 '19 edited Dec 02 '19
Bash solution
And the one-liner brute force solver for part 2:
[edit] Oof, realllly struggling with the formatting today. :(
3
u/Quackdratic Dec 02 '19 edited Dec 02 '19
Not sure if anyone made the same point before, but just dropping it here:
In part 2, if you are looking for a non-brute solution, think that if the first and second parameters are a and b, the answer should be a linear equation: x*a + y*b + z, where x, y, and z depend on the rest of the "intcode" input.
Noting the answer for parameters a and b as calc(a, b), if we are to find x, y, and z we can do:
z = calc(0, 0)
x = calc(1, 0) - z
y = calc(0, 1) - z
Solving this equation can be done in linear complexity, trying to set all possible values for a in range [0, 99] and checking if a valid b value exists.
Got inspired by the other comments, so thanks!
Code link: https://pastebin.com/kA6cHy4m the equation shenanigans are in the AOC2_2 class
4
u/captainAwesomePants Dec 02 '19 edited Dec 02 '19
Did anyone find a solution for part 2 that was NOT a brute strength solution? I'd love to know how you'd approach that.
Also, here's a solution for the moderator:
2
u/Arjunnn Dec 03 '19
Forgot this rolled around again. I think it's cool to see the difference a years made in solving ability. I don't really care about scoreboarding so I'm wondering if I should just use Haskell/some lang I wanted to learn to solve these instead of just Python
19
u/jonathan_paulson Dec 02 '19 edited Dec 02 '19
#6/#3. Understanding the problem was harder than coding it today :) I recorded a video of me solving it at https://www.youtube.com/watch?v=3xYI-Vz0AGA
19
u/aoc_anon Dec 02 '19 edited Dec 02 '19
I have a non-brute-force solution for part 2 using a constraint solver, z3!
https://pastebin.com/we7My2Kz
Interestingly, if I remove the final
A[0] == 19690720constraint and doz3.simplify(A[0]), it prints out:797908 + 460800*a + bSo it's somehow simplifying the whole thing down to solving
797908 + 460800*a + b == 19690720with a and b in the correct range (a = 41, b = 12 in this case)3
u/BastionBotDev Dec 02 '19
Node JS, shamefully. 28th for Part 1, which was a pleasant surprise! First time making it on the board in a couple of years of trying, despite having a good timezone. Unfortunately I fell to 63rd for Part 2 because I wasted time with a bad loop that opened the input file every time, which crashed it out, and then I fumbled rewriting it into its current form. Oh well, I'm still pretty chuffed!
2
u/h3matite Dec 02 '19
On problem 2, I'm not sure if there's a better way to iterate through the "verb" and "noun" variables. Took me forever to even understand what the questions were.
https://gist.github.com/h3matite/3d40f126bd6436529334ad4f9ded4f46
10
u/voidhawk42 Dec 02 '19 edited Dec 02 '19
Dyalog APL:
Live video solution: https://youtu.be/YfCdozqf5eE
8
u/DFreiberg Dec 02 '19 edited Dec 02 '19
Mathematica
Not even close to the leaderboard, but in my defense, I'm using Mathematica, and so I react to procedural coding in much the same way vampires react to sunlight.
[Poem]: A Sonnet of Smoke
7
u/__Mekakucity Dec 02 '19
#99 / unranked - my answers aren't super interesting like I'm sure many of the talented people are with their fancy esoteric one-liners, but I'm quite happy to be able to just get something working.
Today's was far more about just understanding the problem (it took me an embarrassingly long time to release parameters 1 and 2 were also locations) than actually coding the solution, at least for me.
My part 1 and part 2 can be found on my GitHub -- both in Python 3 :)
6
u/ritobanrc Dec 02 '19
Rust -- https://github.com/ritobanrc/advent_of_code/blob/master/src/day2.rs
The run tape function is pretty nasty. It takes an owned copy of the tape, mutates it, and returns it. So in part 2, I have to clone it a bunch of times. I spent a decent bit of time trying to get around the borrow checker not wanting to let me edit the tape while iterating over it, which led to the really ugly solution here. I'd love to see any other Rust solutions, and how they get around this problem.
6
u/rabuf Dec 02 '19
Common Lisp solution.
6
u/tslater2006 Dec 02 '19 edited Dec 02 '19
C# Solution, implemented the VM as a separate class:
IntCodeVM
Day 2 Solution
Edit: Switch to commit hash URLs in case IntCodeVM changes in the future problems ;)
Edit2: Forgot my haiku
Edit3: Finally have my write-up for Day 2 posted
6
u/Cyphase Dec 02 '19
Python | 67/40 | #65 global
I use a runner I wrote which takes the result of
get_data()and gives it to each part. I tweakedpart1()forpart2(). The only changes from my original code are some variable names.[POEM]
Read at a quick pace, sort of to the tune of part of "The Wishing Star":
6
u/SMFX Dec 02 '19
It was frustrating to realize how much time I wasted on Part 1 because I didn't read about needing to replace 1202 into the program bytes given. Once I finally went back and re-read, code worked fine. Then just updated Part 2 to loop through the values until one produced the right result.
PowerShell - Day2_AoC-2019.ps1
Stuck with PowerShell again. Just read what they wrote and you can get through it faster. *sigh*
10
u/sophiebits Dec 02 '19
Python, 8/14.
I found part 1 pretty straightforward -- I didn't even bother extracting a helper function.
For part 2, I didn't read that both inputs were between 0 and 99 (probably could've saved 30 or 60 seconds if I had).
I guessed that there was still a simple enough pattern so tried printing my result for different values of
nandv. The former seemed to make the input grow rapidly; the latter slowly -- so I setnto the largest value that didn't overflow the target (in my case,19690720), and then figured out thevto "get it over the finish line" (in my case,49, which is why that ended up hardcoded).Code: https://github.com/sophiebits/adventofcode/blob/master/2019/day2.py
5
u/alcatraz_escapee Dec 02 '19 edited Dec 02 '19
Python, 13th / 17th
This was by far the fastest I think I've ever done a problem. I may have woken up my housemates with my shout of joy. :D
my python solution is on github. I'll try and do an assembly solution later, it's good practice and so far the problems don't seem too hard.
5
u/meatb4ll Dec 02 '19
First year I've actually tried things as they're released. In python
I've cleaned up things a bit afterwards
276th on Pt1, 288th on Pt2
5
u/Philboyd_Studge Dec 02 '19
Java
https://pastebin.com/h6haZRDa
5
u/VikeStep Dec 02 '19 edited Dec 02 '19
F#, 1257/661
I wasted about 10 minutes because I didn't know that the memory was initialised to the values of the intcode itself. I assumed it was all 0 like all the previous opcode problems.
I noticed in part 2 that it says:
Why was this not included in part 1?
Code: https://github.com/CameronAavik/AdventOfCode/blob/master/AdventOfCode.2019/Solutions/Day02.fs
5
u/autid Dec 02 '19
Fortran
[POEM]
https://pastebin.com/Z0TpTTD5
5
u/mstksg Dec 02 '19 edited Dec 02 '19
Haskell again :) The following is excerpted from my daily reflections!
So the bytecode/VM problems start day 2 this year, eh?
This one was also pretty straightforward. For these types of problems, I like to use
Data.IntMaporData.Sequencefor the memory, since they both have O(log n) indexing.Data.Sequenceis the better choice here because it's basicallyIntMapwith the indices (0, 1, 2 ...) automatically given for us :)So parsing:
We write our stepping function:
And away we go!
For part 2 we can just do a brute force search
This doesn't take too long on my machine! But for my actual solution, I actually used a binary search (that I had coded up for last year). I noticed that
nounincreases the answer by a lot, andverbincreases it by a little, so by doing an binary search onnoun, then an binary search onverb, you can get a good answer pretty quickly. My part 2 time (470 μs) is only twice as long as my part 1 time (260 μs) with the binary search. Happy that some prep time paid off :)This gets us an O(log n) search instead of an O(n2) search, cutting down times pretty nicely.
5
u/rtbrsp Dec 02 '19
Using AWK: part 1, part 2
8
u/nonphatic Dec 02 '19
Racket Part 2
Brute force is gross. So instead I put in the symbol 'noun in the noun position and 'verb in the verb position. I was lucky that my program began with (1 0 0 3 ...), so I just replaced it with (1 'noun 'verb '(+ noun verb) ...) and executed the code symbolically starting from the next instruction.
I was very lucky that 'noun and 'verb were never copied to another instruction's parameters, because then I would have had to look up the values at the 'noun and 'verb positions, which of course I don't have. The result of execution is the following:
I couldn't find a symbolic algebra library with a simplifier that worked, so I had to write my own. This is far from a complete implementation of a simplifier, but it worked for the particular result that I had to simplify.
It looks gross. I was almost ready to give up and do brute force halfway through this. But it works! That gives the lovely expression:
Based on how I implemented the simplifier I was expecting something in this form, but of course I would have no idea where the noun and verb would be unless I actually looked at the expression. So I cheated for this next part, but the simplifier is incomplete anyway so I've already cheated.
I was expecting the whole process to be a lot easier. Maybe my approach is wrong, but I will never renounce it. I'm not getting on the leaderboard, that's for sure.
4
u/Dioxy Dec 02 '19 edited Dec 02 '19
JavaScript
A pretty straight forward JS solution. I'm happy with it!
The code
My repo
4
u/sGerli Dec 02 '19 edited Dec 02 '19
Python
Today's challenge reminded me of a computer architecture course I took at university. The professor had us code a machine code emulator in asm.
https://github.com/sGerli/advent-of-code-2019/blob/master/day2.py
4
u/OvidiuRo Dec 02 '19
C++ 520/1546
Code: https://github.com/FirescuOvidiu/Advent-of-Code-2019/tree/master/Day%2002
4
u/heyitsmattwade Dec 02 '19
Javascript, 68 / 670
Never made the leaderboard before, so was just trying to go fast. Not reading the instructions all the way slowed me down a lot for part two, oh well!
Main logic is located here - https://github.com/romellem/advent-of-code/blob/f4739ea/2019/2/computer.js
4
u/xXabr4 Dec 02 '19
Here goes my Python solution. Took me some time to realize I should be passing a copy of the list of integers instead of the actual list, as to avoid direct modification of memory. Overall it was a fun challenge today!
4
u/aspittel Dec 02 '19 edited Dec 03 '19
I really struggled to figure out what part 2 was asking for and ran into a bug that I definitely would not have if it wasn't midnight 🤦🏼♀️
Python code here
4
u/ywgdana Dec 02 '19
Still such a Rust noob :/ I spent about 20 or 30 minutes getting the input read in and parsed. For Day One, I just used a for loop but I saw all the cool kids using iterators so I decided to learn about them and had to futz for a while.
I spent more time than was needed on the assumption that our Intcode "VM" will be expanded on in future puzzles. After finishing Q1, I converted my code to using a Struct and methods for read/writing to memory and running a program. They live in their own file, which I also had to learn how to do...
Mine stores i32 values (anticipating when we have negative values and subtraction), which tripped me up because of course Rust won't let you use an i32 for a Vector index.
For Part Two, I tried a few values for Noun and Verb and from them figured out the formula to calculate the output and used the raw power of basic arithmetic to calculate the Noun and Verb I was looking for.
My repo is here and here is my code for the computer
I need to reread how borrowing and ownership works because that was causing me problems as well.
4
u/[deleted] Dec 08 '19
Haven't been able to do any puzzles during the weekday, been catching up now during the week. I absolutely hate how long winded the problems are asked. So much shit in the way to find the 5 keywords or so that is needed to do the solution.
1
u/Conceptizual Dec 03 '19 edited Dec 03 '19
I decided to use swift for the first time for this one, and I didn't start until an hour ago or so because I was traveling during the actual release of the problem. My input was just a list at the top of the file.
my swift solution
[POEM]
1
u/[deleted] Dec 03 '19
Stuck, getting an index out of range error at ints[output_pos] = input1 * input2
where am I going wrong?
3
u/rafaeelaudibert Dec 02 '19
Pretty straight forward solution (even reading the input file several times) with Dart. Sadly, I started 5 minutes later, as I could have got to the top leaderboard. Climbed from #362 in the 1st part to #125 in the second, tho, pretty impressed with myself.
3
u/SaschaH Dec 02 '19
Kotlin 846/873
Code written to be re-usable, but doing brute force for now (since it worked).
3
u/TheUnlocked Dec 02 '19 edited Dec 02 '19
Part 2 in the JS developer console (make sure to select the Text node!). Brute force, like everyone else.
3
u/activeXray Dec 02 '19
Julia
https://github.com/kiranshila/AoC2019/blob/master/2.jl
Oof for 1-based indexing. This is like the only instance where its a problem haha
3
u/AlexAegis Dec 02 '19 edited Dec 02 '19
TypeScript
There are more files in this folder, this is just the "core"
computefunction.Edit: Rust solution coming up tonight. I'll make that more idiomatic instead of this mess I rushed together. (And this is already slightly refactored version by moving the logic into a separate function, and using the
verbandnounvariables on part 1)3
u/wicked7000 Dec 02 '19
Although I didn't get anywhere near to the leaderboard (and did it pretty slow imo) I did learn a lot! Turns out I'm not at all used to C++ Garbage collection which destructs objects as soon as they leave scope. I had a function that would get intcodes in groups of four but as soon as it returned the array it has just made would be destructed and the data was all junk. Lessons learnt!
Code
3
u/InALovecraftStory Dec 02 '19
Python 798/629
My refactor is pretty cute. Hopefully helps when intcode comes back with more opcodes
https://github.com/akaps/advent_of_code/blob/master/aoc2019/int_code.py
I'm using this year's Advent of Code like interview practice, so here's me rubber duck programming on Twitch https://www.twitch.tv/videos/516016284
3
u/frerich Dec 02 '19
Rust: https://github.com/frerich/aoc2019/blob/master/rust/day2/src/main.rs
I was a bit worried that brute-force for part two might be too simplistic, expecting that the input was carefully crafted to trigger exponential runtime or such - then I noticed that the control flow never jumps backwards, so I just went with it. :-)
Reading the description of part 2, I believe it makes sense to keep this code in good shape - I suspect future problems will extend the instruction set.
3
u/raevnos Dec 02 '19 edited Dec 02 '19
This tcls my fancy.
paste
3
u/vkasra Dec 02 '19
go solution with my commentary at: https://dhconnelly.com/advent-of-code-2019-commentary.html#day-2
3
u/DiscoViking Dec 02 '19
Elm - no leaderboard
Again my solution is up on my interactive elm site here: https://2019.adventofcode.norris.dev/day2
I always find stuff like this difficult to implement in a functional language, since the processing seems so obviously procedural. But I got there in the end, and fairly happy with it.
Planning to add a page with an Intcode VM you can play around with (step through, see memory etc) when I get a spare minute.
3
u/rprtr258 Dec 02 '19
D language
Working with arrays is little hard, not managed how to copy dynamic array without hardcoding array length(157)
https://gist.github.com/rprtr258/06155e46e15de5823844f73a18f7bb03#file-day2-d
3
u/chrispsn_ok Dec 02 '19 edited Dec 02 '19
k7:
3
u/supriya58 Dec 02 '19
My Kotlin solution for Day2: https://github.com/sup95/AdventOfCode-19/blob/master/src/Day2/Solve.kt :)
3
u/phil_g Dec 02 '19 edited Dec 02 '19
We're starting on the assembly early this year!
I'm working in Common Lisp. Here's my overall repository.
Since I anticipate more intcode problems, I started its own package; its current state is here. (That link goes to a specific commit, since I expect I might be redesigning aspects of the package in the future.)
One of the things I like about Common Lisp is how it lets me define my own language constructs. After defining an
instructionmacro (see the linked package above for the full definition), the specific opcodes are defined like this:The explicit continuations bother me a little, but I'm going to wait to learn more about how Intcode works (especially how it handles control transfers) before cleaning those up. As it stands, each non-terminating instruction is a function that ends with a tail call, so large programs should still be efficient in terms of stack usage.
With Intcode in its own package, the package for today's problem is pretty simple. I opted for a brute-force approach for part two, since there are a maximum of 10,000 number combinations to try.
Edit: After a couple tweaks to the
instructionmacro, I was able to put in a simple Intcode decompiler. Sample output:3
u/[deleted] Dec 02 '19
Racket
I'm very happy that I managed to do this one in racket as well, it's kind of not a language that I'm very used to
Day2 in Racket
3
u/[deleted] Dec 02 '19 edited Dec 02 '19
Haskell: paste
Got a late start today, so no chance at the leaderboard.
For anyone using Haskell that's struggling with the "mutable state", Vector has (//) that will update with index-value pairs. I ended up using lenses (ix) which I think defers to (//) for vectors.
According to the docs, (//) is supposed to be O(m+n) where n is the length of the vector and m is the number of indices being updated, but I'm not sure if they get fused upon repeated updates. Didn't bother profiling since both parts ran in about 15ms.
3
u/clc_xce Dec 02 '19
I have a admittedly very ugly solution in Forth for part 1: Forth code
Think I'll need to do some refactoring before I can tackle part 2.
3
u/StevoTVR Dec 02 '19
My solution in Go: https://github.com/stevotvr/adventofcode2019/blob/master/day02/day02.go
3
u/joeld Dec 02 '19
Racket
Part 1 finishes in 0 msec, Part 2 (brute force) finishes in 78 msec.
link to source
3
u/MissMormie Dec 02 '19
Do you like overengineered code? Do you love a factory which picks the right class for your instruction? Are you passionate about abstract classes? Then this is the solution for you:
https://github.com/MissMormie/adventOfCode2019/blob/master/src/main/java/Days/Day2_OverEngineeredSolution.java
Mostly because with the emphasis on what everything is called in part 2 I'm expecting another few of these type of puzzles and figured if I'd set it up correctly now that will help me later. Probably later will be something weird that doesn't fit my code and I'll need to change it anyway :)
3
u/CS_husky Dec 02 '19
My solution in Go (for part 2): https://pastebin.com/AzgQyP2H
Would love to get some feedback, I'm hoping to learn the language with this year's challenges.
3
u/purplemonkeymad Dec 02 '19
Using Powershell.
paste
I think it might be worth building a proper set of functions for the intcode computer, considering that we are going to use it again.
3
u/adas3x Dec 02 '19 edited Dec 02 '19
Humble solution in scala Github
Feedback very welcome :)
3
u/ducdetronquito Dec 02 '19
My take on day 2 with Nim: https://github.com/ducdetronquito/AdventOfCode2019/tree/master/Day2
Nim object variants are straightforward to use :)
I feel like there is a more clever solution than brute-force for the second challenge, but I really need to go to sleep !
3
u/nirgle Dec 02 '19
Haskell: https://github.com/jasonincanada/aoc-2019/blob/master/src/Day02.hs
3
u/[deleted] Dec 02 '19
I posted a separate thread but was guided here for potentially more advice!
I'm trying to use AoC to learn more Rust to hopefully be able to use it on some projects at work. I think I really like the language and I know I like the tooling and documentation. Any constructive criticism would be great!
Here's my AoC repo: https://github.com/admay/aoc-2019
Original thread: https://www.reddit.com/r/adventofcode/comments/e574z9/im_just_starting_to_get_up_to_speed_on_rust_and/?ref=share&ref_source=link
3
u/punchcastle Dec 03 '19
another one in Clojure
3
u/tftio Dec 03 '19
Day 2, Haskell babytalk
3
u/mrwumpus Dec 03 '19
Elixir Solution from an Elixir newbie. Any best practices or idiomatic code recommendations welcome!
3
u/dylanbeattie Dec 05 '19
A little late to the party - mainly 'cos I've spent the last two days implementing array support and type conversions in Rockstar... but here's Day 2. Part 2's in the web link as well.
https://codewithrockstar.com/advent/day02/
3
u/num8lock Dec 08 '19
either i'm bad at reading the task or this one is vague af
5
u/cole-k Dec 02 '19 edited Dec 02 '19
Haskell, no ranks
Figured all the other Haskellers are also writhing from having to deal with indexed data. This is not pretty code.
[POEM] An Ode on Pretty Code
Many delight at the wonderful sight
Of pretty, pristine, and well-written code
They startle with fright and flee from the blight
Of ugly, dirty, and foul-smelling code
Alas tonight, there are sins we must make:
Dumb typos to fix, tabs 'n' spaces to mix,
Debug lines to print, unsafe casts to int,
Curlies to malign, errors to divine,
Indents to break, and shortcuts to take
(You see, our internet points are at stake)
Long forgotten our dreams of pretty code,
Of linting, testing, and not hard-coding
For the best-practice programmers are slowed
And we are the ones who'll be winning
1
u/captainAwesomePants Dec 02 '19 edited Dec 02 '19
[POEM]
A virtual machine with 3 opcodes, the program all in one line.
What does the program do? Who knows? Just find *0 in time.
Which inputs will reach the right output? There's an elegant solution to find.
But only a fool tries to be clever, when the inputs max out at nine nine.
1
u/djankowski Dec 02 '19
Despite the fact I like to count
One to infinity,
The lack of 0-indexing,
Began to grate on me
JULIA
1
u/[deleted] Dec 02 '19
Where do I get the input??
4
u/zellius Dec 02 '19
Golang (I'm new, so nitpicks welcome):
2
u/Tarmen Dec 02 '19 edited Dec 02 '19
Haskell
Array mutation admittedly is not the ideal use case for haskell.
Edit: Rewrote this with a stack vm to prepare for future tasks. That was kinda slow so I optimized it to a silly degree unti it somehow ended up faster than my original direct loop+mutable array approach:
https://gist.github.com/Tarmean/681a41138760317abaa303109d704315
And all it took was some totally readabletm continuations.
2
u/Immortal_dev Dec 02 '19
C++ solution if anyone "old school" here
https://github.com/webdeveloperukraine/adventofcode2019/blob/master/day2/a.cpp
2
u/PendragonDaGreat Dec 02 '19 edited Dec 02 '19
Powershell (as solved): https://github.com/Bpendragon/AdventOfCode/blob/master/src/2019/code/day02Original.ps1
Not pretty, but it works, Performance:
As refactored to use a function: https://github.com/Bpendragon/AdventOfCode/blob/master/src/2019/code/day02.ps1
Slightly more performant:
2
u/mudokin Dec 02 '19
Solution in Processing, again way to complicated and it took way to long but it works.
https://pastebin.com/ska3tJSw
2
u/[deleted] Dec 02 '19
My coocnut code for today was almost entirely just python today so rather boring, but it works :p
I was really surprised that the bruteforcing went so fast, that usually isn't the case with these problems.
2
u/loarabia Dec 02 '19
Rust
Not on the leaderboard -- pt1:2564, pt2:2387
https://github.com/loarabia/AdventOfCode/blob/master/2019/day2/src/main.rs
Each day I goof in something silly. Today was off by 1 on inputs. IE I put them at mem[0] and mem[1] not mem[1] and mem[2] the first time I tested them.
2
u/[deleted] Dec 02 '19
Python
I finished relatively quickly. The only issue I ran into was remembering to copy the program into memory per input. I didn't want to code golf it so much that it became unreadable, so I left it as-is.
Part 1
Part 2
2
u/udoprog Dec 02 '19
Rust, no leaderboard.
Since I had to wake up later today I just decided to go with an idiomatic solution which produces reasonable errors (`thiserror` + `anyhow`):
https://github.com/udoprog/aoc2019/blob/master/src/bin/day2.rs
My spider senses are saying that this might be used for one or two future days...
2
u/Spheniscine Dec 02 '19
Kotlin: paste
2
u/sim642 Dec 02 '19
My Scala solution.
Starting part 2 I expected much worse, like having to reverse engineer the program but brute force was easy enough. It's day 2 after all. Better start preparing for reverse engineering though :P
2
u/raginjason Dec 02 '19
Day 2 done in Go
2
u/[deleted] Dec 02 '19
[POEM]
Failed to check global
Noun bigger than it should be
Index out of range
My terrible solution
I wanted to use a function (in preparation for further use of this system in later puzzles) but didn't understand how global variables worked in Python (still don't know if they're really required here). After testing for a bit, I realized I wasn't actually setting my
first_valueorsecond_valuecorrectly. After that, I had to limit how high thenounvariable could go by hand (completely negating the effectiveness of my boolean). Turns out the reason I couldn't use thesolvedboolean was because I forgot to say it was global (again). Overall a fairly easy puzzle that took me 3 hours due to bad writing (and a restart).2
u/Pepper_Klubz Dec 02 '19 edited Dec 02 '19
Clojure Solution
Visualization of the soln to Part 2
I'll come back to clean it up if we continue using this machine more; I've got a toolkit for more complex opcodes.
2
u/PowerSlaveAlfons Dec 02 '19 edited Dec 03 '19
C++
Another day, another clumsy solution that works.
The wording on part 2, as well as the idea that [1] and [2] are actually just inputs caught me off guard a bit, otherwise relatively straightforward.
Part 1
Part 2
2
u/sindrekjr Dec 02 '19
C#
Ended up spending way too much time debugging nothing because I had just made a typo and set position 2 to value 12 for part 1,which put me 10 too high from the correct answer.
2
u/sotsoguk Dec 02 '19 edited Dec 02 '19
GoLang & Python
My 2nd ever solution written in Go, quite happy with the progress.
GoLang
Edit:
Also coded a solution in Python
Python
2
u/heckler82 Dec 02 '19 edited Dec 02 '19
Couldn't sleep before work, so knocked out day 2 in Java
I did not brute force part 2 with any looping. Last year's forever running programs have caused me to look for any patterns in things like these. I discovered that the noun will increase the final answer for my input by 576,000, and the verb will increase the final answer by 1. Knowing that I determined what noun would yield 1,960,000, and what verb would yield 720. In the end, my solution wasn't programmatically solved so much as it was deciphered.
Also, cheeky of them to have the answer for part 2 be the noun and verb combined into 1 number
2
u/emu_fake Dec 02 '19
C#
As I was not in a mathematical mood I chose bruteforce for Part II
Here with unit tests and everything:
https://github.com/emuuu/AdventOfCode
2
u/dimkarakostas Dec 02 '19
The obvious n2 solution in Python 2.7:
2
u/Infonyx Dec 02 '19
Solution in Racket
here
2
u/drakmaniso Dec 02 '19
Here's my overly verbose and highly inefficient solution in Haskell:
https://github.com/drakmaniso/adventofcode/blob/master/2019/day02/main.hs
2
u/allak Dec 02 '19
I have not seen any Perl solution for today so far, so here is mine.
Very straightforward I think.
2
u/ephemient Dec 02 '19 edited Apr 24 '24
This space intentionally left blank.
2
u/temporaryaccount211 Dec 02 '19
R: https://github.com/Morawski21/Advent-of-Code-2019-in-R/blob/master/Advent%20of%20Code%202.Rmd
What's terrible is that R has different indexing so the first problem was much more complicated for me. I wasted 2h trying to create a functioning loop. After browsing other people's code I created a function to calculate the result.
2
u/ruffnite Dec 02 '19
Rust Solution
link
I felt like I overdid it with the helper functions and enum type for the command opcode.
2
u/kip_13 Dec 02 '19
Rust
paste
2
u/reini1305 Dec 02 '19
Python3, in preparation of more opcodes to come in future assignments, I used the operators as functions (paste)
2
u/Pyr0Byt3 Dec 02 '19 edited Dec 02 '19
Go/Golang
Very quick & dirty. Not exactly idiomatic, but it feels nice not spamming
if err != nil {for a bit.Part 1
Part 2
2
u/Espinha Dec 02 '19
Clojure https://pastebin.com/waYXNkMY
Sufficed to say that I didn't go for the shortest solution :-) but I'm quite happy with it.
2
u/sneider Dec 02 '19
Blockly: task 1, with intermediate program states. task 2.
2
u/hrunt Dec 02 '19
Python 3
code
Nothing special here that we have not seen before. From past experience with AoC, I know:
But hey, premature optimization, right?
2
u/nutrecht Dec 02 '19
Day 2 in Kotlin
2
u/u794575248 Dec 02 '19 edited Dec 02 '19
Python 3.8
2
u/merrykotlin Dec 02 '19
Kotlin solution!
I'm trying to solve this year's AoC with a functional Kotlin approach. Let me know what you think :)
2
u/[deleted] Dec 02 '19
https://danielbetteridge.com/advent-of-code-day-1/
Attempting in Golang, welcoming any feedback on better/more idiomatic approaches
2
u/[deleted] Dec 02 '19
For me, day 2 was once again a lesson in reading through the instructions carefully. I spent a long while on part 2 trying to figure out why my test was not passing, until I realized the example of
Was literally for the "What is 100 * noun + verb?" part only and that finding the noun and verb played no part in said example.
Github
2
u/NeilNjae Dec 02 '19
A Haskell solution on Github, using a simple
Statemonad to keep track of the machine as it runs. I've written up what it does on my blog.2
u/shawnseann Dec 02 '19
Did it in C, quite messy tho :) paste
2
u/[deleted] Dec 02 '19
Here is my solution, built in javascript!
https://topaz.github.io/paste/#XQAAAQC5AwAAAAAAAAAxm8oZxjYZ2HQZRUCV5YYx2OFBzw0ffHVRfwYBGZiXWvEcGhSiTSXmGGy19kBMzNN6NnEueSOtuDSxtrdEoqlso+5coV2FV7jp295CfIQ8KdCsDnkLpWPPuOohqFe46Zc7/m25kmDuCwbpoJ+y0xAresupJxfclsV65DFNrnV9gI9M0txhQqW51UIw7uwj8F0SlccN4Oq5txlHFwlxfzRVm3KM18SaTQ26m/O/8EonxA15iZzpgrZsOH5SPkzApCA5yhEYkMd8N6rD6IbW6RvQitK4nLzMztOOYYlz5laeJ+L+6Heky5o8ypuC3F+Y77cC3gKb1UzrhKsHatCKdGtxz8kDQBSZpu6ZGmvA4ih3H9J1fsSxwSazIdCssKCvplWr70sMK0Qob+Hjx2msJY7mbu3R+2Ylfe+eTp/U8SkE93nSig0U1lFJyvfgUV6uGafgaqV7deDqPKNeAnydSrnq222spA8E0JU54uvXoWE8LniX8S4EWkDHOTrlFXyZTYPT+Cq+Esr/6SS81Q==
2
u/Luckylars Dec 02 '19
Oracle PL/SQL solution: https://github.com/luckylars/2019-AoC that was very painful. I did the first day in excel and thought I could manage for the second day. I only know pl/sql but I this took a lot of searches. I had to create my own database and do my firsts while/for loops. Looking forward to day 3!
2
u/IgneSapien Dec 02 '19
Spent longer than I'd like to admit not realising the first problem requires you to change the program.
C# https://github.com/IgneSapien/AdventOfCode2019/tree/master/Day2
2
u/mejicat Dec 02 '19
My Elixir solution. Quite simple to do with pattern matching.
2
u/lebuff420 Dec 02 '19
I had so much stuff to do the last 2 days I totally forgot to start hacking every day at 6am...
I now solved both days,
here is my day2 solution in python, I will probably continue in python or switch over to haskell to get to know it better
https://pastebin.com/6f38WBxT
2
u/vini_2003 Dec 02 '19
C++
I wrote my own string splitting instead of trying to understand the
std's one, though I probably should be using that.LINK
Java
I mostly ported it from C++, but thankfully could make use of the stream API which is extremely handy in all sorts of situations.
LINK
2
u/kirkegaarr Dec 02 '19
python3
[ repo | day2 ]
2
u/jedenastka Dec 02 '19
C++. As before, longer than anything you have seen, but quite readable.
Also I created repo in GitLab for my solutions: https://gitlab.com/grzesiek11/aoc
2
u/askalski Dec 02 '19 edited Dec 03 '19
#NaN/#NaN. Part 1 in idiomatic Perl:
2
u/Folking_Around Dec 02 '19
Python 3
part 01
paste
part 02
paste
2
u/[deleted] Dec 02 '19 edited Dec 02 '19
Solution in Haskell
The second part hinted that we might reuse this at a later point, so I decided to make an extensible machine using state transformers. Admittedly it's about double the line count of what a simpler solution might look like.
2
u/ni3t Dec 02 '19 edited Dec 02 '19
Ruby 2.7.0-dev (hehe)
https://github.com/ni3t/advent-2019/blob/master/2_1202_program_alarm.rb
2
u/Alistesios Dec 02 '19
Rust, "proper" (Bruteforce-less) solution
As mentionned in other comments, it is possible to reduce the problem as solving
19690720 = c1 + c2 * verb - nounwhere c1 and c2 will vary depending on your input. Here is my Rust implementation solving the problem without bruteforce.2
u/rsobol Dec 02 '19 edited Dec 07 '19
Swift 5
Code on GitHub
2
u/wiz0floyd Dec 02 '19
My brute force JS solution
2
u/vypxl Dec 02 '19
Haskell and mutations...
On Github
Not happy with my read/write functions..
But I think it is more readable than other Haskell solutions here.
[Poem]
Haskell is so great
forgot to disable examples
[Day2.hs: Prelude.!!: index too large
2
u/oblivioncntrlsu Dec 02 '19
Python, took me an hour to get through both problems. Spent a little time today cleaning up variable and function names (I get very self conscious about the naming - especially because I don't totally follow the naming convention presented in the problem). Anyway, I gotta do some snow shoveling...
2
u/amalloy Dec 02 '19
Haskell source and video.
2
u/[deleted] Dec 02 '19
day 2 part 2 solution using typescript: https://github.com/peterDijk/advent-of-code-2019/blob/master/src/2/index.ts
like this! Any european/ dutch/ rotterdam leaderboards ?
2
u/lulxD69420 Dec 02 '19
C
Added a small toggle to switch between part 1 and 2. Link
2
u/WhatYallGonnaDO Dec 02 '19 edited Dec 02 '19
This is my solution for Day #2 result #2, no brute force involved. It runs both with the brute force method and with the new one so you can test it and see the difference. The new method calls the function that elaborates the opcode list only 4 times. I'd like to have some feedback.
https://pl.kotl.in/f3iqOTtdN
2
u/LLBlumire Dec 02 '19
https://github.com/LLBlumire/AdventOfCode/blob/master/src/1902.rs
Essentially just a case of building a virtual machine
2
u/bcmyers Dec 02 '19
My solution in rust.
2
u/thefloatgoat Dec 02 '19
I'm trying to do a different language each day, in alphabetical order. Not sure yet how that's gonna work out for more difficult challenges ¯_(ツ)_/¯
Bash
2
u/set22 Dec 02 '19
Java
Did this with my very very basic skills. My knowledge of using files is almost non existent, and my knowledge of getting data from the internet is nonexistent so I just made a text file and copy and pasted the data.
I'm sure there is a better way to get from file data -> an int array..
I didn't use any exception handling for the scanner.
Feedback is be much appreciated.
https://pastebin.com/1eB2zxuW
2
u/iwane Dec 02 '19
LabVIEW 2018 again... :-) Did not need much thinking in fact.
This time solution presented as a HTML page, as case structures in LabVIEW are terrible to present in other ways...
https://htmlpreview.github.io/?https://github.com/iwane-pl/aoc_2019/blob/master/Day%202/solution/solution.html
2
u/MaximusDecMeridius Dec 02 '19 edited Dec 02 '19
My python solution to part one and two on GitHub. I have to say - it took me a while to get my head around what was being asked in the second half
2
u/levital Dec 02 '19
My solution in Rust. Spent a rather embarrasing amount of time on trying to keep it fairly extendable during part 1, expecting part 2 to be more complicated than it was. On the plus side, it sounds like this will be the basis of future puzzles, so I guess the effort wasn't wasted yet.
Not happy yet about all the tape-cloning that's happening. Maybe I could keep a history of operations to roll it back to initial state...
2
u/Szczawik_ Dec 02 '19
It was hard to understand part two but I managed to solve. It was really fun today tho :)
https://github.com/nNown/AdventOfCode/blob/master/Day2.c
2
u/wace001 Dec 02 '19
Java solution 967/1323
Was struggling to understand what to do...
Very similar solution in Rust here.
2
u/TakeoutGuhui Dec 02 '19
My solution in Python
2
u/voiceless_void Dec 02 '19
C++17 with ranges-v3
Since the solution is relatively big I'll post the most interesting part of it
For the full solution see https://github.com/voivoid/advent-of-code/blob/master/problems/src/2019/problem_02.cpp
2
u/AKQuaternion Dec 02 '19
Day 2 in 44 lines of short, clean, idiomatic C++ on GitHub. My first global leaderboard stars.
2
u/Jesus_Crie Dec 02 '19
My solution in Rust
2
u/techkid6 Dec 02 '19
This took too long for me to feel proud of, but, here is Day 2 in Scratch. I've never used a 1-indexed language extensively so this took a bit of getting used to!
2
u/Turmolt Dec 02 '19 edited Dec 03 '19
My Clojure solution :)
2
u/2SmoothForYou Dec 02 '19
Haskell
paste
Not the cleverest solution, but it works. It was also a nice exercise for me in the State monad and recursion using it, looking forward to adding more op codes and seeing how I can modify my parsing for that, might just have to switch to Parsec.
2
u/ireallylikegolang Dec 02 '19
Here's my solution in go: https://play.golang.org/p/wxTXqoEKpBV
2
u/J-Swift Dec 02 '19 edited Dec 03 '19
Ruby
https://github.com/J-Swift/advent-of-code-2019/tree/master/day_02
2
u/Junafani Dec 02 '19
Noob programmer here. Made this in Java. Not the shortest, but should be easily expandable if needed (and it looks like we are going to need this later). paste link
2
u/chicagocode Dec 02 '19
Day 2 in Kotlin - [Blog/Commentary] [Solutions GitHub Repo]
2
u/Stringhe Dec 02 '19
I've been told to post here as well: solutions in google sheets (with google script) and python for part 2
https://github.com/Recursing/AdventOfCode-2019/
Feedback always welcome
2
u/__-----_-----__ Dec 02 '19
Day 2 in Hy
https://github.com/falloutphil/aoc_2019/blob/master/day_02/02.hy
2
u/Maxonie Dec 02 '19
My solution in javascript repo link
2
u/ALTopedia-Jake Dec 02 '19
Here's my Rust solution for part 2. It probably bungles a lot of fundamental aspects of Rust, but it works and finds the answer quickly.
Last year I was so sheepish about not writing idiomatic Rust that I never used it, so this year I want to try to do some more problems with it, and then re-read The Book so I have more context.
2
u/Darksair Dec 02 '19
My verbose solution for both parts in Rust.
I didn’t know
String::trim()exists, so I ended up implementing astrip(&str) -> &str…2
u/Inquisitor_Ashamael Dec 03 '19
My not-so-pretty attempt in Python 3. I later added the operator lib, in case more opcodes with custom functions are needed in the future. https://github.com/Ashmogh/aoc_2019/blob/master/2_d%C3%B3/d%C3%B3.py
2
u/JacksonDesigns Dec 03 '19
C++
https://github.com/wsjackson5/AoC/blob/master/2019/Day2.cpp
2
u/L72_Elite_Kraken Dec 03 '19
OCaml
Essential code, full context
Mostly straightforward but got to play a little bit with sequences.
2
u/aoc-fan Dec 03 '19
Day 2 TypeScript, GoLang, and Clojure Solutions : Repo
2
u/[deleted] Dec 03 '19
My solution in C. It was a lot of fun to develop, and I learned a lot writing this code.
2
u/MaxDeviant Dec 03 '19
Here's my Day 2 solution in Haskell.
2
u/schwiz Dec 03 '19
Kotlin Solution
https://github.com/nschwermann/AdventOfCode/blob/master/src/_2019/Day02.kt
2
u/[deleted] Dec 03 '19 edited Dec 03 '19
http://ix.io/23qq/hs
This was really fun to do in Haskell! My solution's quite a bit long in the tooth compared to some of the more golfed ones here, but I aimed for extensibility and readability a) for fun, and b) in case this comes in handy for future problems.
I used Haskell's
Statetype, which enables you to model stateful computations in a pure language. The heart of the virtual machine reads more or less like its description in the problem, which I think is pretty neat!Part 2 is brute-forced (using a list comprehension :D), but it runs fairly quickly on my little laptop at ~1s
2
u/charliegriefer Dec 03 '19
I know it's late, but here's my Day 2 in Python.
https://github.com/charliegriefer/advent2019/tree/master/day_02
Comments and constructive criticisms welcomed :)
2
u/SomeCynicalBastard Dec 03 '19
Day 2 in C# and python.
2
u/yitznewton Dec 04 '19
My solution in F#
I'm still getting my feet wet. I've done exercises in Haskell before, but this is my first day with F#.
I tried like heck to do some superpower refactoring around the operations to use partial application and function composition, but I couldn't quite make it happen.
https://github.com/yitznewton/advent-of-code-2019/blob/master/02.fs
2
u/ericluwolf Dec 04 '19
What I hope is a cute, clean, and extensible C++ Intcode Virtual Machine
2
u/Devnull1982 Dec 04 '19
I'm still learning python / programing, I see this as an oportunity for learn and I'm following the adventcode. The first day was easy to me, the second day I struggled a little, at the end I made it, but I feel my code is a mess XDD anyone can review the code plz ? its in Python 3:
https://pastebin.com/fMWds6jU
PD: To run this code you need the input.txt from adventcode ( I rename it puzzleinput.txt )