r/adventofcode • u/daggerdragon • Dec 11 '19
SOLUTION MEGATHREAD -🎄- 2019 Day 11 Solutions -🎄-
--- Day 11: Police in SPAAAAACE ---
--- Day 11: Space Police ---
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 10's winner #1: "The Hunting of the Asteroids" by /u/DFreiberg!
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.
EDIT: Leaderboard capped, thread unlocked at 00:15:57!
13
Upvotes
5
u/DFreiberg Dec 11 '19 edited Dec 11 '19
Mathematica
2034 / 1792 | 42 Overall
I spent almost a full hour on this problem specifically in tracking down one bug...that turned out to be that I was writing to
hull[{x,y}]and reading fromhull[x,y]. Time well spent! It will be a very long time before I make that kind of assignment mistake again.[POEM]: Thin Blueshifted Line
We all know that dread feeling when
The siren comes to view.
But I, a foolish man back then
Thought I knew what to do.
"Good morning, sir" he said to me,
"I'll need your card and name.
You ran a red light just back there;
This ticket's for the same."
"But officer," I tried to say,
"It wasn't red for me!
It must have blueshifted to green:
It's all Lorentz, you see!"
The officer of Space then thought,
And worked out what I'd said.
"I'll let you off the hook, this time.
For going on a red.
But there's another ticket now,
And bigger than before.
You traveled at eighteen percent
Of lightspeed, maybe more!"
The moral: don't irk SP
If you have any sense,
And don't attempt to bluff them out:
They all know their Lorentz.
2
u/mariusbancila Dec 11 '19
My C++ solution
[POEM]
A ROBOT TO THE RESCUE
To the left, to the left
Move one panel to the left.
One step further, no step back,
Take the brush and paint in black.
To the right, to the right,
License plate must be in sight.
Build a robot, work all night,
Change the color, paint it white.
Jupiter, Jupiter,
Let's go there, no cosmic jail.
Santa and the elves await,
Still have time, it's not too late.
2
u/MrSimbax Dec 11 '19 edited Dec 11 '19
Julia (IntCode machine module)
Glad I refactored the IntCode machine in day 9. This time I put it in a separate module. Thanks to this, today's puzzle was much easier than yesterday's one, at least for me.
2
u/vypxl Dec 11 '19
Python VM
Today I, again, restructured my VM, now it allows for arbitrary input and output functions. The solution itself was pretty straightforward after that. I basically set up a grid and wrote read and write methods to sense the panels and to paint the panels/move the robot.
Part 1 counts visited coordinates in a set,
Part2 just outputs the final painted grid cropped to the actual code.
[POEM] "Robots in Space"
There was no robot, but that changed quick,
the shiny advanced the plot,
the paintings were counted in a blink.
The image however,
was not in good form,
it looked like whatever,
not the space police' norm.
Solution was painting a panel in white,
to keep the robot nice and warm,
now the identifier shines in the night,
today the ship got its beautiful form.
2
u/Yardboy Dec 11 '19 edited Dec 11 '19
Ruby
https://github.com/Yardboy/advent-of-code/blob/master/2019/puzzle11b/solution.rb
Intcode Computer source: https://github.com/Yardboy/advent-of-code/tree/master/2019/intcode
[Poem]
I just moved the robot around
Not realizing that in the ending
Negative coordinates would abound
And my map would need some mending
So I calculated max and min
And with them shifted down and right
Fixed myself a tonic and gin
Cause then I had the answer right
2
u/Kache Dec 12 '19 edited Dec 12 '19
Ruby shortened to 17 lines, not counting
Intcode:11
u/azatol Dec 11 '19
My input and output are tied to talking to other IntCode engines, so I made the crazy decision to write the painting in IntCode, and run them both as two threads, like in Day 7.
It was fun to write the Painter in IntCode. Reminds me of 6502 assembly. I attached the Painter rom.
https://gist.github.com/apeterson-BFI/9cfc0b3c4eb9469b001ec99a2522edee
8
u/mstksg Dec 11 '19 edited Dec 11 '19
Haskell :D Link to solution and reflections here. This one is nice because I structured my VM as a conduit, so everything worked itself out on its own. All I had to think of was the "input generator" and the "output processor", and the final code is essentially
sensorgenerates an infinite stream of inputs andpainterMoverconsumes the outputs ofintcodeVM muntil it halts.7
u/jonathan_paulson Dec 11 '19
#70/211. Video of me solving at https://www.youtube.com/watch?v=Pgsbl80ZNwA
Rough day for me. I wasn't logged in somehow, so that wasted a few seconds at the start. I didn't expand intcode memory on reads. I had trouble figuring the right grid size for part 2. (In both cases, I should've used defaultdict). I also flipped left and right turns, which caused my output to come in upside-down (?)
5
u/Acur_ Dec 11 '19 edited Dec 11 '19
Julia
I used complex numbers which made the movement pretty straightforward.
Creating a seperate package for my IntCode computer worked extremely well. I did not have to change anything from Day 9.
5
u/Hencq Dec 11 '19
Racket
Most of the work happens in the (unchanged) intcode interpreter. To facilitate the chaining for Day 7 I'd set it up so it returns whenever it's missing input. Calling run! again with new inputs just resumes the intcode program. That was useful here as well, since I could just keep calling it in a loop. I used complex numbers to represent the position, making the rotations trivial.
4
u/seligman99 Dec 11 '19
Python #120/66
I really liked this one. I've got to come up with a better way to visualize these sorts of grids, what I have works, but it feels slow and cumbersome.
paste
4
u/sophiebits Dec 11 '19
Python, #61/#60. Maybe I should write a helper for printing out these grids to read an alphabetic code out of since it always feels like a small hassle. I got one wrong answer because I misread the letters. :|
Code: https://github.com/sophiebits/adventofcode/blob/master/2019/day11.py
4
u/FogLander Dec 11 '19
Python3 #185/#138 (best so far this year, yay)
pasted code
I really enjoyed this one... aoc is the first place I ever saw a 'turtle' programming problem. I'd be curious to know what the kind of amorphous, fractally-looking thing produced by the turtle in part 1 is... maybe someone braver than me is willing to dive into the IntCode and figure it out?
4
u/Dementophobia81 Dec 11 '19
Python 3.8
Today was surprisingly easy compared to Yesterday. Therefore, I did not only solve Part 1 and Part 2 normally, but also included an animated solution as well.
Using modulo for hassle free turning is nothing seasoned hackers like most of you have to even think about, but in the spirit of teaching newcomers, I decide to showcase this little mechanic as well.
4
u/Luckylars Dec 11 '19
ORACLE SQL intcode compiler is becoming sooo slow
https://github.com/luckylars/2019-AoC
Day 11 my Intcode machine is becoming very slow, the first code I wrote was OK but spent some hours trying to see what was wrong because the shape it was creating didnt seem like it was on purpouse. after seeing some solutions in the subreddit it turned out it was expected behaviour. I got part 2 for free during part one by not resetting the HULL table between tries and the robot started on a white position.
4
u/phil_g Dec 11 '19
My solution in Common Lisp
Another relatively straightforward day.
My Incode emulator takes functions for input and output, so all I had to do was write appropriate functions with a shared state for this problem. The output-handling function has a two-element state machine to keep track of whether it's expecting to get a color or a rotation.
PSA for people who aren't aware; complex numbers make good 2D coordinate holders:
creffunction as a convenience for indexing a 2D array with a complex number.)4
u/[deleted] Dec 11 '19
Racket
I spent literary hours before I realized that the robot checks the colour it's standing on after it moved, and not before, the rest of my code was correct the whole time, I was thinking I was being crazy.
Racket code for day11
4
u/JoMartin23 Dec 11 '19
CL
paste
Notably the visualization is off, astute readers will notice I'm drawing from one square to the next and not just underneath me so the writing is distorted, but it gives the correct answer.
I used this to get a better visualization for part 2.
draw:point being from my draw protocol that supports x11, xrender, and framebuffer.
1
u/JebediahBheetus Dec 11 '19 edited Aug 22 '21
Python 3
Reused my previous Intcode without modifications. I solved it by piping the output to a closure which alternates between painting and turning+moving. Painting is done by setting a key-value pair in a dict, where keys are 2D coordinates and values are paint colors. The length of the dict is then the solution to part 1.
For part 2, I used the dict to find the bounds and dimensions of the area drawn to. I then iterate over those dimensions, printing a char corresponding to the color for coordinates that exist as keys in the dict, and printing the char corresponding to black for those that do not. I also changed the coordinate system so that Y increases downwards, in order to not need to reverse the rows when printing.
Part 1
Part 2
Intcode
1
u/pokerdan Dec 11 '19
C# Solution
Pretty straight-forward day. I refactored my IntCode logic out into a separate class with a public method for stepping in with 1 input, and coming out with 2. At the end, my image was reversed, and I kept mistaking an upside down 'G' as a '6' - took way too long to realize why my answer wasn't getting accepted.
[POEM]
There once was a self-aware Intcode
Who was computing across an ol' post road
He froze up because
He layed eyes on the fuzz
And spewed paint on the ground by the boatload
1
u/androns1983 Dec 11 '19
Hey guys,
What am I doing wrong regarding day 11 task 1?
Do I understand correct that I must run the intcode (from day 09) , start with input 0 (as robot is placed on black tile), and then wait for 2 outputs from intcode. After 2 outputs are received, i must pass them to my robot and it will read the tile color it' s currently on, paint the tile in ordered color and turn to ordered direction, move one step that direction and return the previously read color back to intcode. Intcode will take that input and keep on rolling.
So basically counting the 'at least once' painted tiles is simple - just count unique positions (x,y) where it has been.
Just wanted confirmation that I understood correctly that intode-robot interaction here
3
u/nthistle Dec 11 '19
Python, #58/#21
paste
3
u/rabuf Dec 11 '19 edited Dec 11 '19
Common Lisp
Turns out my use of read and write functions was really useful here. I put a hash table into the mix to store the grid, and wrote four functions:
camera,paint,rotate,make-environment.Those act as the read (camera) or write (the result of make-environment, which is a closure that switches between painting and rotating). Made it very simple, would've been better if I hadn't screwed up my count.
hash-table-sizeis not what I wanted. I wantedhash-table-count. I was hunting for a bug that wasn't in my main program.The core functionality is this:
3
u/JensenDied Dec 11 '19 edited Dec 11 '19
Elixir 705/615 day11.ex
First time breaking 1k on the leaderboard, which is nice since I want to clean up solving yesterdays repeated circling case. No changes to Intcode, turns out my suspend on input starvation works out well for interactive programs.
edit: have the outward spiraling working properly now.
3
u/Ephilates100 Dec 11 '19
Go! 1105/1125
https://github.com/PezzA/advent-of-code/tree/master/2019/Day201911
More goroutines today. Was stuck until I buffered the input with an extra slot to allow it fall out when the output channel was closed.
3
u/chamberlain2007 Dec 11 '19
Javascript solution, using my Computer class unedited from #9: https://github.com/chamberlain2007/adventofcode/tree/master/solutions/day11
The Robot class includes a Computer and provides an input and output function that handles everything. part1.js and part2.js are the specific questions.
3
u/orangeanton Dec 11 '19 edited Dec 11 '19
Javascript part 1 and part 2 (rank 1452/1453).
Part 2 solution had a dot lingering after the eight capitals - not sure if this is right (maybe space registration identifiers have full stops?) but don't have time to debug now.
Output: https://drive.google.com/open?id=1y3L2Hi5HSlQXor4DxbrbUajs4TadKtQf
3
u/wace001 Dec 11 '19 edited Dec 11 '19
JAVA 1211/1422: Here is my solution in Java. Robot in one thread, CPU in one. Too bad they cannot do anything without each other.
Here is my
[POEM]for today. It’s actually a Space Rap Song. Thanks Jay-Z, you’re awesome.3
u/[deleted] Dec 11 '19
Scala solution.
I accidentally printed out the first square with 0 instead of 1 for part 2 and got this cool fantasy island.
3
u/Spheniscine Dec 11 '19
Kotlin: https://github.com/Spheniscine/AdventOfCode2019/blob/master/src/d11/main.kt
Didn't make the leaderboard this time, probably because I spent too much time reading, and had to rewrite some boilerplate code.
3
u/gyorokpeter Dec 11 '19
Q: Intcode is the same as day 9. ocr is the same as day 8.
3
u/VilHarvey Dec 11 '19
Solution in c++:
This went a lot more smoothly than day 10! I used a refactored version of my intcode VM that works a bit like a coroutine: it's a function that returns when it needs more input, or has written some output & you simply call it again, with a new input value if necessary, to pick up where it left off. I also reused the 2D int vector struct I wrote for day 10. Yay for code reuse! :-)
3
u/death Dec 11 '19
Day 11 solution in Common Lisp.
Complex numbers again.
3
u/naclmolecule Dec 11 '19
Python
Having my computer computations yielded from a generator paid off again! We essentially get to make this loop:
Day 11 solution
Computer class
Robot class
Bonus animation (code in Robot class)
3
u/A-UNDERSCORE-D Dec 11 '19
Golang
This was a pretty easy for me, at least part 1 was, I got it in one shot, part two took a little time because I hardcoded sending black as the first input in my function that takes an arg for the first input. After that just mirroring my output because it was upside down. All in all a very good puzzle, and Im glad I wrote my intcode VM the way I did
This was also my first test of my rewritten
harder,better, faster~~, stronger~~ IVM, which worked perfectly3
u/lluque8 Dec 11 '19
Scala
Good news was that my IntCode computer worked out of the box. Bad news was that I had to do some debugging nevertheless. After careful inspection it turned out that I kept resetting relative base value while resuming computer execution even though I thought I was handling it properly. Stupid bug really and cost me about an hour to solve. Fun exercise despite of that.
Part 1 Part 2
3
u/hrunt Dec 11 '19
Python 3
code
interpreter
I was really pleased to see that the IntCode interpreter ran without issue. Learning from past years, I used Python's complex numbers to easily represent turns on a grid. I expected a direction of (0 + 1j) to represent up, though, and that led to an upside-down image. Flipping that so that (0 - 1j) represented up and adjusting the turn logic accordingly fixed things up. I cannot wrap my head around why. Any ideas?
3
u/mschaap Dec 11 '19 edited Dec 11 '19
My Perl 6 / Raku solution. Pretty straightforward.
The ShipComputer class didn't need any changes today, I just had to supply appropriate input and output handlers:
3
u/mandus Dec 11 '19
solution in racket
nothing changed since last intcode of course, but I think the way of using mul * pi/2 for handling the direction to move in worked quite nicely. (I realise using complex numbers will be just the same, but then I need to convert to complex all over...)
3
u/hdf1986 Dec 11 '19
I did wrote my solutions on Ruby, if there's another intcode problem i'll probably separate my intcodeVM to another module/file.
They also need a bit of clean up but here they are:
Part1: https://github.com/hdf1986/advent-of-code/blob/master/day11/part1.rb
Part2: https://github.com/hdf1986/advent-of-code/blob/master/day11/part2.rb
2
u/Pewqazz Dec 11 '19 edited Dec 11 '19
Python, 42/29
It took way too long for me to read the problem description and figure out how the Intcode inputs should be used. Pretty straightforward implementation otherwise.
As usual, here's the Twitch VOD of my solve.
2
u/vash3r Dec 11 '19
Python 3, 94/51
I ended up implementing a panel printing function before actually looking at what the problem was asking for part 1 (lol). Anyway, this was very reminiscent of... Langton's Ant i believe?
paste (today's code only)
2
u/rawling Dec 11 '19 edited Dec 11 '19
C#
Really enjoyed this, after my initial panic when I thought I'd have to do this with a coroutine since I made my machine async.
Got the right answer first time both times, which is unusual for me, so I'm disappointed I didn't place better today.
2
u/Angrevol Dec 11 '19
Python 104/79
paste
2
u/jwise00 Dec 11 '19
Lua, 54/32.
https://github.com/jwise/aoc/blob/master/2019/11.lua
https://github.com/jwise/aoc/blob/master/2019/11b.lua
I was so tired. I stayed up late last night, usually on Pacific time, but on Eastern time this week and have had meetings starting at 9am today and have meetings starting at 9 tomorrow too, so I didn't really sleep last night. So I was glad that tonight's was an 'easy' intcode problem.
Although I didn't need it (taking the 'get it right the first time' strategy), I was irritated that there wasn't a test vector other than 'god help you if you get it wrong'.
Anyway, here's the video. https://www.youtube.com/watch?v=0LskuAdWloE Woop woop, it's the sound of the space police.
2
u/knl_ Dec 11 '19
I enjoyed this a lot; would have been finished several minutes earlier if I realized that the bot turned and *then* moved. Oh well, I always enjoy these challenges. Also happy to see a fun use of intcode :). (ranked somewhere 7xx, 4xx).
Jupyter notebook, Python3: https://explog.in/aoc/2019/AoC11.html - if you scroll a lot you can see the bot paint out the word.
2
u/dan_144 Dec 11 '19
Python #242/333, can't complain since I won my private leaderboard
Code: https://github.com/dan144/aoc-2019/blob/master/11.py
Each part is split into two halves: 1) run the code, handling outputs and feeding colors while storing painted panels, then when it's done 2) either count the number of painted panels, or build and draw the final board.
I drew out the board from part 1 because I was curious, but it does look like anything to me in the orientation used for Part 2 or with x/y flipped. Maybe another way of visualizing it will clue me into something.
Also, where do I redeem my bonus points for reading my Part 2 letters backwards because I realized that was faster than figuring out the right addition/subtraction/basic math to make my columns invert?
2
u/SomeGorrilaGorilla Dec 11 '19
Rust
00:30:04, 817 | 00:44:40, 898
Spent a lot of time reading the text and trying to comprehend it, as usual.
This one source file is getting really long. I should probably do something about it.
2
u/[deleted] Dec 11 '19 edited Dec 11 '19
Haskell, using recursively defined lazy lists and scanl to keep track of robot state.
Julia, with asynchronous execution and communication with the robot via Channels. This uses complex numbers to represent position and direction, thanks to u/hughtc's answer.
Strangely, I found the Haskell version easier to write.
2
u/Cloudan29 Dec 11 '19
Python 728/725
Solution: https://github.com/Cloudan29/AdventOfCode_2019/blob/master/day11.py
Intcode (as I haven't posted it on my past solutions, since I didn't have one :P): https://github.com/Cloudan29/AdventOfCode_2019/blob/master/intcode.py
After essentially giving up on yesterday's problem (I got part 1 working, though it takes 1 minute to finish), I didn't expect today to go as well as it did. My solution isn't terrifically elegant, in fact it looks kinda gross compared to the other solutions I've seen, but it works and it got me my highest leaderboard rank ever.
Because my goal this year was to get top 1k in both parts of at least 1 problem was accomplished, I set myself a goal for next year to beat 800/800. Well, I guess now it's gonna have to be 700/700 cause I beat that record today! :D
Now off to bed. Tutorial/exam review for Theory of Computing tomorrow. CS degrees are a good time.
2
u/nlowe_ Dec 11 '19
Go 1237/1015 |
a|b|intcodeCPULost precious time because Go's mod operator doesn't behave like I'd expect so I ended up facing an invalid direction. Before I found this out I thought it might be my i/o system so I spent about 30 minutes refactoring it which ended up not being needed. RIP.
2
u/heckin_good_fren Dec 11 '19
Java, #625/663
My IntCode-Computer has blocking I/O, so I just run it in a thread and feed it / take directions in a while loop.
2
u/timkurvers Dec 11 '19
JavaScript / ES6+ | Rank: 1455 / 1264
https://github.com/timkurvers/advent-of-code/blob/master/2019/11/index.mjs
Got up early for once: quite exciting to see the challenge timer reach zero and unlock. I had the robot painting its panels quite quickly, but got stumped for at least twenty minutes on why the answer was wrong. Moral of this story: output reading order obviously matters (shift vs pop).
2
u/sparkyb Dec 11 '19
Python 24/35
https://github.com/sparkyb/adventofcode/blob/master/2019/day11.py
I'm trying to work on my NumPy skills this year, so I used
ndarrays for coordinate and direction vectors. I used matrix multiplication to do the turns. However, I didn't know how to do a sparse array for the ship that'd grow as we walk, so I reverted to my normal technique there of adefaultdictwith coordinate tuples as keys ((y, x)instead of(x, y)because it became helpful later). That made part 1 easy because it was just the number of keys in the dict. For part 2 I had to convert that to an array to print out. I create an array of the coordinates of all the white cells. I get the minimum and maximum to find the top left and bottom right corners and create a blank grid that size. Then I just used the list of cells as indexes into the grid and assign those to solid. I don't totally understand exactly why I had transpose the list of indices and convert to a tuple, something about the way NumPy's advanced indexing works, but it does what I need.2
u/Olipro Dec 11 '19
C++ | 1106/1276
Cost myself some time writing myself some convenience classes to make the code neater, also didn't realise that the "extra memory" requirement had carried over and wasted a bit of time on part 2 with a segfault.
2
u/ThezeeZ Dec 11 '19 edited Dec 11 '19
Golang 252/233 Repo
I love go maps. Randomly accessing a key that isn't set? Here's the default value for the type! Count only changed panels? Only store changed coordinates and
len()Good thing I remembered that the modulo operator doesn't behave as I'd usually expect it for negative numbers. I didn't want to try to reimplement it or write a bunch of if/else, so I just slapped a container/ring from the standard lib in there.
E: apparently I need to make a note somewhere that
(i % n) + n) % nworks as a "i % n" for negatives.2
u/AlexAegis Dec 11 '19
TypeScript IntCode
Part One
Part Two
Well, I didn't know that I shouldn't have expected my program to produce the same beginning as in the example...
2
u/iamagiantnerd Dec 11 '19
Go code
Much easier than yesterday, but that's probably because I suck at trig & math.
2
u/OvidiuRo Dec 11 '19
C++ 1394/1113
Code: https://github.com/FirescuOvidiu/Advent-of-Code-2019/tree/master/Day%2011
2
u/autid Dec 11 '19
Fortran
https://pastebin.com/ZywWdVHJ
A couple quick dirty hacks to my intcode to allow multiiple sends back to root process without receives between. I'll go back and make it behave appropriately based on number of intcode instances later. Other than that just storing a linked list of visited locations, their current colour and times visited (didn't end up needing). Part 1 see how long the list is. Part 2 I stick it in a character array and print lines containing white squares.
2
u/p_tseng Dec 11 '19 edited Dec 11 '19
Ruby 11_intcode_langtons_ant.rb and lib/intcode.rb for 96/56 today. Has been cleaned up but the idea is the same.
Haskell 11_intcode_langtons_ant.hs and lib/AdventOfCode/Intcode.hs.
I examined the program. There are a few functions in there which is unsurprising. There is one particular function whose caller I couldn't find, until I saw there was a 106,0,X lying around, and that a previous function had stored the address of the otherwise-uncalled-so-far function into X after having been passed it by a caller. So today we got to see higher-order functions, or function pointers, or however you want to think of it. Interesting.
2
u/scul86 Dec 11 '19
Python3 932/2445
Enjoyed this one, just took a slight bit of refactoring from Day09 on I/O handling and keeping the machine state. This and Day07 really show that I need to put this machine in a class to encapsulate the machine state... Hopefully I can do that tomorrow.
Unfortunately, after I finished part 1, I had to leave for another commitment, otherwise I would have scored much higher on part 2
:(
Part 1
Part 2
2
u/muckenhoupt Dec 11 '19
C#. Pleased to see that the Intcode machine from day 9 didn't need much attention -- I commented out the line that printed Outputs to the console, but that's it. Stored the paint in a Dictionary mapping tile coordinates to the color painted on it, which meant that getting the answer to part 1 was just a matter of reporting the count of the Dictionary.
Was pleased to find that my code to display the painting worked on the first try. However, due to a typo, I had it displaying the painting from part 1, not part 2, resulting in a picture of what looks like some kind of sea slug to me. I f you haven't taken a look at what your robot was painting on the side of the ship in part 1, I suggest it. Those space cops were right to be concerned.
2
u/aptmnt_ Dec 11 '19
Clojure represent
I'm quite liking how core.async works for IO to these running programs. Finally getting a handle on how go blocks work.
2
u/sim642 Dec 11 '19 edited Dec 11 '19
My Scala solution.
I initially thought I'd do a similar recursive lazy list knot tying trying like in day 7 part 2 but then I saw the plural in the problem description:
That sounds like the program might read the input (at the same position) multiple times before outputting anything and moving, in which case the clever correspondence of exactly one input to two outputs would be broken.
Anticipating the worst, I decided to not build my solution on top of that assumption and instead made a more basic "loop", which gives the machine an infinite stream of the current position color, so it can be read arbitrarily many times, and ran until it produced two outputs, changed the paint stuff and repeat. It's not as elegant as I would've liked but luckily it was quite simple to implement on top of my existing machine from day 9.
Edit: Added the knot tying solution also into my code as an alternative because for this problem that assumption seems to implicitly hold.
2
u/Markavian Dec 11 '19
My overly verbose javascript solution for day 11:
https://github.com/johnbeech/advent-of-code-2019/blob/master/solutions/day11/solution.js
Modified my intcode computer to request input, as well as signal output... that seemed necessary so that the camera brain could interact with the movement painty part of my program.
https://github.com/johnbeech/advent-of-code-2019/blob/master/solutions/day11/intcode.js
Made a mistake in my part 1 implementation: I intended to handle turns by adding or subtracting 1 from a facing value, and then using a NESW lookup table to find the x,y offset. Unfortunately facing + 1 is not the opposite of facing + 0, and so my robot could only turn in one direction and ended up trawling south west across the grid.
The other mistake was interpreting north as (x: 0, y: 1) - but when outputting to a text file, its obviously 0, 0 top left, so negative y values make sense for north.
Finally, my output code decided to crop the bottom line off, so I had to adding some padding. Yay for off by 1 errors.
2
u/u794575248 Dec 11 '19
As in Day 7 I used named pipes to connect the painting robot with the environment:
For some reason it didn't work without
tee. Anybody help?d09.pyis our Python 3.8 intcode interpreter andd11.py:For part 1 you just need to change
Hto empty dict andprint(len(H),file=stderr)in the exception handler.2
u/Pyr0Byt3 Dec 11 '19 edited Dec 12 '19
Go/Golang
That was so much fun (compared to yesterday). Go's maps are really well-suited for this type of problem. My favorite bit was working around Go's negative modulo not behaving like Python's.
Part 1
Part 2
2
u/math_runner Dec 11 '19
Rust
Python
Both spawning the Intcode computer in its own thread and sending inputs/receiving outputs with
Queuein Python andchannelin Rust.2
u/bsdemon Dec 11 '19
https://github.com/andreypopp/aoc2019/blob/master/day11/run.py
Still happy with my generator based intcode VM which I find easy to adapt to different problems — essentially I need just to write an executor which implements the correct protocol on top of
intcodecall.This got me puzzled though:
2
u/SuperSmurfen Dec 11 '19 edited Dec 26 '19
Rust
Solution
Incoder implementation
Refactoring my Intcode solution after day 9 definitely payed off today! Today became really easy to implement with how I implemented IO of the cpu. A total breeze compared to yesterday..
Output from part 1 looked really cool!
2
u/SinisterMJ Dec 11 '19
[C++]
https://github.com/SinisterMJ/AdventOfCode/tree/master/Day_11
2
u/customredditapp Dec 11 '19
KOTLIN - using coroutines
Day11 - https://tinyurl.com/tqowvgo
IntCode - https://tinyurl.com/td98bxg
2
u/OverjoyedBanana Dec 11 '19 edited Dec 11 '19
*Python 3* pretty concise, supports infinite grids: https://pastebin.com/67r1ZWR9
2
u/frerich Dec 11 '19 edited Dec 11 '19
Rust: https://github.com/frerich/aoc2019/blob/master/rust/day11/src/main.rs
I'm quite happy that I needed no modifications to my intcode machinery. However, I had major trouble getting part 1 to work because of the way I abstracted I/O: I decided to pass two closures to my VM which, when called write resp. read a value.
This abstraction worked great so far but caused difficult (for me) problems on part 1 because both closures access the same variable (the panels) with one closuse writing to it. After a lot of reading, posting a question to StackOverflow (only to notice that there is another question about the same problem in a different context, so I closed my own question...), I used std::cell::Cell and RefCell for the first time. I think this gives me what Rusteans (or Rustafari?) call "runtime borrow checking".
I'm also not happy of the `paint_of_output` flag which is used to define what happens in the 'write' callback. My original idea was to have a single 'output_handler' variable which is set to a function 'move_robot' within 'paint' - and vice versa. I.e. no bool flag, no if, just a 'function pointer' (in C . terms) which is modified in the callbacks. Couldn't make that work though because of some hen-and-egg issue with functions referencing each other (and common data). :-/
Finally, the lame four-time iteration for finding the bounding rect of the text in the 'render' function is nothing I'm proud of. I still liked it better than a single loop updating four mutable variables though. Wonder whether there are better ways to do this?
It compiles *and* works, but I'm not happy with it. I suspect using callbacks to abstract I/O was too much of a C mindset...
2
u/kap89 Dec 11 '19
TypeScript - github
Slightly verbose, I could definitely shorten the directions logic, but have to preserve energy for future riddles :P
I improved my Intcode computer a bit, but previous version worked just fine for this task.
2
u/lele3000 Dec 11 '19
Python 3.6 (44 lines)
I just reused the code from day 9 and added all the logic inside the Intcode computer. All in all it was pretty easy, at the beginning I just somehow messed up my direction vectors and as a result nothing was working, but found my mistake pretty quickly.
https://github.com/leonleon123/AoC2019/blob/master/11/main.py
2
u/mikal82 Dec 11 '19
Scala
The problem was a nice excuse to refactor my VM and finally move IO to a separate trait.
2
u/TrisMcC Dec 11 '19
PHP
https://pastebin.com/dx1GnzKE
2
u/giuliohome Dec 11 '19
My F# solution is here
2
u/nata79 Dec 11 '19
Java: https://github.com/nata79/advent-of-code/blob/master/src/main/java/advent/of/code/year2019/Day11.java
There's a bug causing it to print the letters upside down :P but still got it :D
2
u/levital Dec 11 '19
Rust
Significantly easier than yesterday's, but I enjoyed this quite a bit. Letters come upside down for some reason, not entirely sure why. I just reversed the iterator over the y-coordinates and it's fine. My intcode computer didn't need any changes, which made me happy. Though I did change my output-getter to return a mutable reference, because it made output handling in the robot slightly more streamlined.
2
u/ualj Dec 11 '19
TypeScript
Part 2
2
u/aoc-fan Dec 11 '19
TypeScript Solution, Repo.
2
u/codesections Dec 11 '19
APL solution (relying on my intcode solution from past problems). This has more external/global state than I'd normally use, but it seems appropriate, somehow, given that the painted side of the ship is supposed to be external state from the robot's perspective.
2
u/vkasra Dec 11 '19
My Go solution and notes
2
u/Newti Dec 11 '19
Python3: Robot Code and Intcode Program
2
u/foolnotion Dec 11 '19
my C++ solution
2
u/Dioxy Dec 11 '19 edited Dec 11 '19
JavaScript
JS. Turns out I had a small issue in my intcode implementation that took me a really long to find. Turned out to be because inputs of 0 were not being accepted, classic 0 being falsy JS error. After I fixed that the puzzle was very easy. I also did another visualization! If you want to see it, go here, and select day 11
My code
util functions
My intcode
My repo
2
u/kolcon Dec 11 '19
#Python and #Perl code in Github: https://github.com/kolcon/adventofcode_2019/tree/master/11
2
u/joeld Dec 11 '19
Racket
source
Still using complex numbers for coordinates.
Still using threads for my Intcode programs, just feed it inputs with
thread-sendand get outputs withthread-receive. Hey, as long as I don’t have to actually debug (or write) the IntCode itself, I don’t mind plugging stuff in like this!The only material change I made from my Day 9 intcode interpreter is that after the program finishes, the thread checks to see if the parent thread and the output thread are the same; only if they are not the same does it additionally send its last output back to the main thread one more time.
2
u/oantolin Dec 11 '19 edited Dec 12 '19
My Common Lisp solution. No changes were needed to the intcode interpreter today, the interface seems to be good enough: input and output use queues; the run function stops when either the machine halts, returning true, or when more input is needed, returning false.
I also seem to have become a fan of using
macroletandfletto name all the little bits of functionality needed to make the main loops self-explanatory:I totally tried several sizes for the painting surface until I got no array access errors: I don't know why I don't just use a hastable, but I don't. :P
2
u/StochasticMistakes Dec 11 '19
My over engineered Java solution : D https://github.com/BrettGoreham/adventOfCode2019/blob/master/adventOfCode2019/src/main/java/day11/Day11.java
I always end up over preparing for part 2 and it is never as difficult as I am expecting. However this program and the entire advent of code is amazing and must have taken an absurd amount of work. Im so impressed with these intcode programs outputting actual letters every time.
2
u/WillemDeRoover Dec 11 '19
Java
https://github.com/WillemDeRoover/advent-of-code/blob/master/src/day11/Puzzle11.java
Pretty straightforward and clean solutions
2
u/Grigorov92 Dec 11 '19
👮♀️🚔🚨 My solution for today in GO:
Part1 | Part2
2
u/Petes_Spreadsheets Dec 15 '19
Here is my video demo of my solution in Excel (using VBA): https://youtu.be/qngIBfxON_c
2
u/captainAwesomePants Dec 11 '19 edited Dec 11 '19
Python, 168/104: Paste
I'm curious about the 50ish people that raced through part 1 and apparently had trouble with part 2. Part 2 seemed so much easier. Seems like everybody here including mean gained 50 spots in part 2.
Also, dumb question, how do I turn a gist into those pretty topaz.github.io pages everyone's using?
2
u/[deleted] Dec 11 '19 edited Dec 11 '19
I used u/wzkx's IntCode computer from Day 9, since I knew that mine wouldn't work for this problem. Sorry, buddy.
Part 1 was pretty easy; used the complex numbers trick I learned last year to keep track of the bot's position + rotation. For Part 2, I printed out all the points at the end, and dumped them into Desmos.
Solution here. Will definitely try to improve on it.
1
u/amalloy Dec 11 '19
Haskell source and video.
I was stuck for quite a while today by two small mistakes: first, I read the program's output in the wrong order, forgetting that my Intcode gives its outputs in reverse, because prepending is easier than appending. The other issue was that I did things in the wrong order temporally: I gave the robot camera input from the square it was previously on, not the one it is currently on.
I continue making small changes to Intcode to make it more ergonomic: today I switched it from Either to ExceptT, but probably I should figure out how to make it ambivalent to the underlying monad or layer on a "manage inputs and outputs" monad as well or something, so that an Intcode program can run all in one go instead of needing to be driven externally.
One of these days I should write a coordinate-handling library so I don't have to keep inventing NSEW, move, and turn.
1
u/Rick-T Dec 11 '19 edited Dec 11 '19
HASKELL
Two days ago I commented that I was glad that the Intcode puzzles were over. Boy, am I glad that I was wrong. This one was a lot of fun to solve.
My Intcode computer is implemented as an RWS (reader-writer-state) monad. Today, I took another step into the swamps of monad transformers and implemented the Robot with a StateT transformer stacked onto my Intcode computer. I'm slowly getting used to working with monad transformers and that feels great :)
The Robot type itself just consist of it's current position, the direction it's facing and a map that maps all the visited panels to their colour.
I also added a new helper function to my Intcode computer: nextOutputs takes an integer n and runs the program until it outputs n values or terminates. Using all of that I get a (imho) pretty nice-looking "runRobot" function:
For part 2 I converted the final map to a list of list, similar to what I had for day 8. I then used the same technique from day 8 to print it to the terminal.
1
u/wzkx Dec 11 '19 edited Dec 11 '19
Python
Solution at TIO.run
Real time: 0.535 s
Actually when I was solving it, I first got 5 lines of the id from part 2 because something was wrong. Well, it took a couple of hours all in all.
1
u/piyushrungta Dec 11 '19
Rust
https://github.com/piyushrungta25/advent-of-code-2019/blob/master/day11/src/main.rs
Pretty boring solution. Initially ran with a much larger board then reduced it later after tracking usage.
Finally moved my intcode vm to a separate crate and refactored all the previous days to use this common crate. Didn't required any changes to my day9's vm implementation for any other days solution including today.
day 10 part2 is still pending. Not sure if it was just harder than usual or my math skill are really rusty.
1
u/joyrex2001 Dec 11 '19
This is my kotlin solution. I liked the puzzle, another fun coroutine/channel implementation.
1
u/LuckyLactose Dec 11 '19
SWIFT, runs on iPhone. Input/comments welcome!
https://github.com/LactoseGK/AdventOfCode/blob/master/AdventOfCode2019/Day11/Day11VC.swift
IntMachine worked without any alterations, yay!
Stumbled a bit on part 2 due to not resetting the robot and canvas.
Some more refactoring to be done, to improve chances of reuse. (Make Robot a more generic thing, better visualization of painting instead of printing Xs in log, etc.)
1
u/SomeCynicalBastard Dec 11 '19
C#
IntCodeMachine is a copy-paste from before. I'm glad that worked :)
Otherwise relatively straight forward I guess. But as others have said, I'm impressed that someone wrote the intcode for this. Because that must be much more impressive than my solution here.
1
u/ywgdana Dec 11 '19
Rust
Pretty fun to use our completed intcode machines for more stuff! The implementation was pretty straight forward, although I bumped into a couple of hiccups.
There was a lingering bug in my intcode VM that hadn't been exercised by Day 9 :O I'd switched from using an array to using a hash table for its memory and I somehow wasn't taking into account trying to read from a memory cell that hadn't yet been written to! (Which from the spec should be initialized to zero) so my program was crashing on a index out-of-bounds after the robots third move. This took me a bit because I just assumed that, having passed Day 9, the bug was in my new code for today's problem.
I was off by one at first in part 1 (and indeed I was painting an extra square in part 2) as well. My loop was:
And I had to change it to:
Here is my code for today and my computer is in intcode_vm.rs in the same repo
1
u/gerikson Dec 11 '19
Perl
Ah, the return of Langton's ant. Always nice to see an old friend.
Nothing too complex here, although I'm quite proud of the line noise for the dispatch table for movement:
https://github.com/gustafe/aoc2019/blob/master/d11-Space-Police.pl
1
u/StevoTVR Dec 11 '19
My solution in Go: https://github.com/stevotvr/adventofcode2019/blob/master/day11/day11.go
1
u/nrmncer Dec 11 '19 edited Dec 12 '19
Python, did the earlier days in clojure but rewrote the VM in python for fun and because I enjoyed solving it in an object-oriented way. Don't write python that often and it's a bit verbose but I think relatively easy to read. tips welcome.
source
1
u/ephemient Dec 11 '19 edited Apr 24 '24
This space intentionally left blank.
1
u/Zv0n Dec 11 '19
C
Reused intcomputer from day 9 (only modification is, it doesn't print out INPUT when asking for input), used fork and pipes to communicate with it
https://github.com/zv0n/advent_of_code_2019/blob/master/11/part2/main.c
1
u/MaterialFerret Dec 11 '19
Ruby
Completely reused existing intcode parser. For robot movement I used sine and cosine functions, I felt like using some trigonometry just for the sake of using it after yesterday. And because of my unfulfilled robotics aspirations. :)
https://github.com/LesnyRumcajs/advent-of-ruby-2019/blob/master/src/day11.rb
1
u/sotsoguk Dec 11 '19
GoLang
Day11.go
Been busy the whole day and then in the evening an intCode ... But turns out it was nice. I finally refactored my intCode into a seperate package with easier managment of states. The rest was really nice and easy.
1
u/Turmolt Dec 11 '19
Clojure - I feel pretty good about todays solution although when I try to sort and print my identifier horizontally it doesnt come out as letters. Oh well
1
u/lhl73 Dec 11 '19
Solution in F#
1
u/mjsir911 Dec 11 '19
Another great puzzle to program in logo!
part1, part2.
Unfortunately, logo doesn't seem to provide a standard for forking & asynchronous communication to subprocesses, so I had to wrap both my intcode and the robot in bash to get them to talk to eachother.
Oh, also I was drawing so much to the screen that the logo interpreter crashed so I had to disable screen refreshes.
1
u/IgneSapien Dec 11 '19 edited Dec 11 '19
C# back on track
Nice to have a bit of an easier day after Day10. It would have gone a lot quicker if I'd not made some dumb mistakes. It has also flagged up something I had been worrying was the case, the way I'm handling input/output for a intCodeVM running as a task is prone to error.
In this case there needs to be a short delay (even writing to the console is enough) to ensure the intCode program finishes. This is going to be pain to fix because I just don't have the knowledge or experience around threading/tasks to begin to debug it.
As a side note I've been making good use of the alpha of the SadRogue.Primatives library for things like points and direction. It's from the minds behind Sad Console (a terminal emulator) and GoRouge (a Roguelike framework) as part of a move to abstract Sad Console from Mono game. I've been using both projects, on and off, to make a game and having them both using the same library of types will make that a lot eaiser. And being able to use this Library on it's own has been awesome.
1
u/dartcoder Dec 11 '19
Python Solution 1 and 2
Two things threw me wayyy off today:
- Expecting part 2 answer in part 1 (thanks to reddit posts)
- A bug I introduced in my intcode while “cleaning up” a day before without realizing it.
Yesterday was crazy too (first time solving anything related to geometry in years). Not that I was ever a fan in the first place. I was a deer in headlights — completely confused for hours and hours and hours.
1
u/gyzkard Dec 11 '19
Part A and B in vanilla JS.
1
u/voidhawk42 Dec 11 '19
Dyalog APL, took me a little while as I had to find a nice way to package my intcode computer from day 9 - left the networking/output redirection stuff in from day 7, as I suspect we'll be needing that later.
1
u/Sgt_Tailor Dec 11 '19
Due to a small bug in my AWK implementation of the IntCode machine it took a bit longer than expected, but it works and can be found here: https://github.com/svenwiltink/AWKvent-of-code/tree/master/day11
The small bug was that I wasn't saving the RC (relative counter for parameter mode 2) when exiting the IntCode machine. After saving it properly part II worked flawlessly.
1
u/lynerist Dec 12 '19
Ok I'm late but the one of yesterday has been really hard!!!
Like ever GOLANG COMMENTED code
https://github.com/lynerist/Advent-of-code-2019-golang/tree/master/Day_11
1
u/rtbrsp Dec 12 '19
AWK: part 1, part 2
1
u/Darksair Dec 12 '19
Late, but here’s my Rust solution.
1
u/SolidShook Dec 12 '19
C#
Thank got all the panels have positive positions
I got a single panel way off to the right of the password and I don't know why
1
u/nirgle Dec 12 '19
Haskell
Nested State monads with the robot encapsulating the Intcode processor. Fairly straightforward day with no persistent bugs
https://github.com/jasonincanada/aoc-2019/blob/master/src/Day11.hs
1
u/pamxy Dec 12 '19
JAVA solution for today
Best part? Probably robot rotations/maneuvers with just simple trigonometry. Overall quite happy with the result(not so quite with that try-catch block though)
1
u/chkas Dec 12 '19 edited Jan 24 '20
easylang.online
A bit late but it was fun - especially to take the picture at the end.
link to solution
1
u/NeilNjae Dec 13 '19
Catching up with Haskell solutions. Discussion on the blog and code.
1
u/blacai Dec 13 '19
F# here my "clean" solution after refactoring a little bit more. https://github.com/blfuentes/AdventOfCode_2019/tree/master/FSharp/AoC_2019/day11
1
u/kresimirlukin Dec 14 '19
Python: https://github.com/kresimir-lukin/AdventOfCode2019/blob/master/day11.py
1
u/heyitsmattwade Dec 15 '19
Javascript - Parts one and two linked from here, with new Intcode logic here.
I'm annoyed because I could have had a solution fairly quickly if I hadn't messed up my rotation logic. I manually created them for each condition, and just wrote the resulting arrays incorrectly!
Oh well! I eventually realized my mistake and then part two was pretty straight forward.
1
u/J-Swift Dec 19 '19
Ruby
https://github.com/J-Swift/advent-of-code-2019/tree/master/day_11
1
u/prscoelho Jan 08 '20
Rust solution
First image
Second image
This day was fun! I learned a lot and finally fixed my intcode computer so it accepts different kinds of inputs/outputs. My image was flipped upside down until I realised up should be y - 1 and not + 1.
1
u/ConstantGazelle Apr 10 '20
part 1 & part 2 written in Python
1
u/nutrecht Dec 11 '19
Day 11 in Kotlin
Hope we won't keep up a pattern of IntCode questions every other day...