6
votes
Day 15: Oxygen System
Today's problem description: https://adventofcode.com/2019/day/15
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>
This one felt like a graph problem that happens to have Intcode involved, but it made the problem significantly more complex in the details.
Part 1 Strategy
Graph search and shortest distance on unweighted graphs is pretty solved (Breadth first search, yay algorithms!), so the hard part of this is just getting the Intcode computer to do BFS.Of course, the nature of this problem makes it possible to search manually, which I seriously considered before deciding to actually do coding.
We're going to make the robot BFS the graph.
In particular, to make the robot go to any previously explored position, we essentially make the computer replay all the moves that got the robot to that position. This is less memory intensive than storing the exact computer state at that position (which would also work), but does mean that we're executing a lot more instructions.
Once we have the ability to set the robot in an arbitrary position, we just use a queue to explore positions and be careful to deal with the robot's output properly.
In particular, we keep our own graph representation with walls and distances from the start (which thanks to BFS is always the shortest) and always try to visit new nodes in BFS order until we can't (or until we find the oxygen tank).
Once done, output the distance from the oxygen tank.
Part 2 Strategy
If you saved time by stopping at the oxygen tank, you'll need to change the code to go through the entire graph.Other than that, most of the code is already done Part 1 and now we don't even need to deal with the robot.
Just BFS the full graph from the oxygen tank with a new distance array, and return the greatest distance from that.
Parts 1/2 Code
This was an interesting one, I kept having little issues where my robot would get lost and start plotting to the wrong places while mapping.
Day 15
I spent a bunch of time on setting up mapping display and interactivity which ended up being kind of waste, if rather entertaining.
Wandering around manually, I quickly noticed that the map was all single-tile corridors, so a naive "keep the wall on the left" approach would probably get me somewhere interesting eventually. It wasn't too difficult to write a function to have the robot check a direction and then return back to its original position if it actually succeeded in moving forward.
Sticking to the left wall, my robot quickly found the goal, but its path was apparently sub-optimal, so I needed to explore more. I set the robot to just keep exploring in the same manner, building up a map as it goes. In my case I just had it stop after 3k steps, but I'm sure a more elegant solution is possible.
Once you have the map, the actual solutions are quite straightforward graph problems: A* from the start to the station, then flood-fill from the station and count the steps.
Day 15 Crystal
I am catching up! If I can do two a day from now through Christmas I will only finish a day late heh. Anyway, I was lazy and did this in a very slow and clunky way, but it worked and I didn't have to think too hard about it.
Intputer.py
RepairBot.py
Oxygen.py