13
votes
Day 2: 1202 Program Alarm
Today's problem description: https://adventofcode.com/2019/day/2
Join the Tildes private leaderboard! You can do that on this page, by entering join code 730956-de85ce0c
.
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
Day 2 in Befunge
Day 2 was definitely significantly more challenging, but the puzzle worked well with the put and get instructions in Befunge.
Part 1
This snippet of code might not work in all Befunge interpreters/compilers. I saw nothing in the spec that said I couldn't use the put instruction to store arbitrarily large numbers, but it doesn't work consistently. I actually created my own interpreter for this to run more smoothly (also so I could enter all the inputs as one array, rather than individually at each
&
). For the input I needed to add a -1 at the end to signify the end.Part 2
I just brute forced this section by manually trying a lot of different values.
wtf is Befunge? And wow.
I know nothing about Befunge other than what people said in the comments here (that it's some kind of weird grid-based language), but I was looking at these solutions last night and had a realization: it basically turns any real-world programming problem into something a lot like a text-based Zachtronics game.
Just looking at the solutions without reading anything about the language, I can tell that it's doing something with the
^ > v <
characters (as arrows that point up/right/down/left) to direct the execution of the program, and that you can put some kind of instructions along the path to do things, change direction conditionally, etc. So it's similar in a lot of ways to the Zachtronic games like SpaceChem, where you've got objects physically moving around and you process them on the way and move them into different sections of your machine.I think that's fascinating—of course it's not very practical, but it's like making a Zachtronics-style puzzle out of every programming task you use it for.
I have it on good authority that this is Befunge, a rather silly esoteric language: https://esolangs.org/wiki/Befunge
Befunge is a 2d grid based programming language. Here's a good wiki page on it: https://esolangs.org/wiki/Befunge.
Befunge is an esolang that was designed to be as difficult to compile as possible: https://esolangs.org/wiki/Befunge
What am I reading and how did you write this?
Day 2 in Crystal
I did a quick pass in ruby first to get the solutions, then went back and refactored a bit to make the interpreter more extensible, in the hope that we come back to it later. I really enjoy working with bytecode interpreters/VMs (the Synacor Challenge was one of my favorite puzzles), so this was really fun.
Intcode Module
This should make it easy to add things like a registers and a stack, along with the corresponding opcodes if/when we get to that point, as well as providing hooks for debugging/disassembly (something that I didn't do early enough in my first attempts at the Synacor puzzle, and made later bits very challenging).
As an aside, apparently the markdown/highlighter doesn't support Crystal syntax specifically, I've used Ruby here and it looks fine though.I guess I was just doing the preview wrong? It works fine now.Well, I've read the problem for part 1 and thought it'd be a fairly straight-forward process..but, hot damn, your solution is robust! Pretty nice.
Haha, yeah it can be solved with little more than a switch/case, and doing that will have better performance than mine (there's a lot of indirection). At this point it's entirely unnecessary to create the
VM
struct or reify the opcodes the way I have, but I'm hoping that I'll have an excuse to use it in a future puzzle.Also, I just now realized that the Eric Wastl behind Advent of Code is, in fact, the same Eric Wastl behind the Synacor puzzle, which explains a few things. :P
Using
awk
again.Part 2
My solution to both parts using Python. I had some trouble with my lists not behaving as expected, and then learned that deep copy is a good idea.
Parts 1 and 2
I think you need an extra line in between the </summary> and your codeblock.
Thanks for pointing that out. Fixed!
while trying to solve the same issue I also tried that, but some more googling showed that deep copy is better reserved for more complex objects. for lists it's an order of magnitude or two slower than just copying the contents of the list. Deimos's comment here is a good explanation of one way to do that.
Ah okay! I wasn't sure reading things online if a regular copy was going to be enough, but I will try to keep in mind that it is for the future.
Deepcopy isn't necessary, but maybe it's preferable for some reason unbeknownst to me? Here's my python solution.
Part 1
My need for copying came in in the second part, as I needed to iterate and change the initial conditions. I didn't realize that my list would be modified inside the function, I got it into my head that Python passes by value but of course a list is an object. Strictly speaking I don't need
deepcopy
as per the discussion with @hhh, but it's what I found that worked.Copy pasted from my original comment in the original thread
Day 2: 1202 Program Alarm
I really liked this one. It was much harder to wrap my head around how the intcode actually worked than to program it.
Part 1
I didn't include the actual input data in this post. I really liked this challenge.
One thing that tripped me up though. The challenge said:
I didn't read the question properly, never changed that second value and spent a good 10 minutes manually checking that it was properly processing the intcode, before reading the question again and facepalming at my own negligence.
Part 2
The first part is the same as part 1, but now it is part of a function that gets run over and over by a double for loop. I'm sure if you were mathematically talented you could probably use clever maths to solve this with a method that isn't brute force. But I'm not that smart.
Language: Rust
I just refactored and extended my initial solution to give me the answer to both parts, so i don't have a Part 1/2 distinction.
I used this as an opportunity to get a little more used to streams! I haven't really used them at all before, but structured file parsing is a good use case where streams prevent me from allocating unnecessary
Vec
s.Solution
Part 1
For the life of me I'm still not sure why I needed a iterating copy loop to reset the data.
In my first attempt I passed the original memory state into functions to be modified locally but it would adopt false values for reasons unknown.
Part 2