6 votes

Day 19: Tractor Beam

Today's problem description: https://adventofcode.com/2019/day/19


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>

1 comment

  1. Crespyl
    Link
    I needed a breather after that last one, which I barely finished in time for Day 19. With a working Intcode VM this is pretty straightforward. Part 1 This works exactly the way you'd expect, just...

    I needed a breather after that last one, which I barely finished in time for Day 19. With a working Intcode VM this is pretty straightforward.

    Part 1

    This works exactly the way you'd expect, just run a loop over the 50x50 grid and test each point in turn, counting up the hits as we go. One important detail is that each "drone" is an independent unit, they only accept one target destination and you have to "send a new drone" (clone or reset) your device before testing the next point.

    #!/usr/bin/env crystal
    
    require "../lib/vm2.cr"
    require "../lib/utils.cr"
    
    
    prog = Utils.get_input_file(Utils.cli_param_or_default(0,"day19/input.txt"))
    vm = VM2.from_string(prog)
    
    hits = 0
    (0...50).each do |x|
      (0...50).each do |y|
        hit = check_point(x,y,vm.clone)
        hits += hit
    
        if hit > 0
          print '#'
        else
          print '.'
        end
      end
      print '\n'
    end
    
    puts "Part 1: %i" % hits
    
    def check_point(x,y,drone)
      drone.send_input(x)
      drone.send_input(y)
      drone.run
      return drone.read_output || 0
    end
    
    Part 2 I spent a few minutes trying to figure out if there was some way to use the drones to take enough measurements for a clever triangulation solution, but that was taking too long so I reverted to a simple iterative check.

    I start out with a 100x100 box that overlaps the start of the beam, then keep scooting it right and down until the whole thing just barely fits. The only difficulty here is making sure that the output is actually the north-west corner of the box, not the south-west corner that I was basing my tests off of.

    x,y = 0,50
    while true
      while check_point(x,y,vm.clone) == 0
        x += 1
      end
    
      if check_point(x+99,y-99,vm.clone) > 0 &&
         check_point(x+99,y,vm.clone) > 0 &&
         check_point(x,y-99,vm.clone) > 0 &&
         check_point(x,y,vm.clone) > 0
        # puts "hit at #{x},#{y}"
        break
      end
    
      y += 1
    end
    
    # adjust to nw corner
    puts "Part 2: %i" % [x*10000 + y-99]
    
    1 vote