01020304050607080910111213141516171819202122232425

Advent of Code

2019/19

Tractor Beam

in C#

by encse

Unsure of the state of Santa's ship, you borrowed the tractor beam technology from Triton. Time to test it out.

When you're safely away from anything else, you activate the tractor beam, but nothing happens. It's hard to tell whether it's working if there's nothing to use it on. Fortunately, your ship's drone system can be configured to deploy a drone to specific coordinates and then check whether it's being pulled. There's even an Intcode program (your puzzle input) that gives you access to the drone system.

Read the full puzzle.

using System;
using System.Linq;

namespace AdventOfCode.Y2019.Day19;

[ProblemName("Tractor Beam")]
class Solution : Solver {

    Func<int, int, bool> Detector(string input) {
        var icm = new ImmutableIntCodeMachine(input);
        return (int x, int y) => {
            var (_, output) = icm.Run(x, y);
            return output[0] == 1;
        };
    }

    public object PartOne(string input) {
        var detector = Detector(input);
        return (from x in Enumerable.Range(0, 50)
                from y in Enumerable.Range(0, 50)
                where detector(x, y)
                select 1).Count();
    }

    public object PartTwo(string input) {

        var detector = Detector(input);

        var (xStart, y) = (0, 100);
        while (true) {
            while (!detector(xStart, y)) {
                xStart++;
            }
            var x = xStart;
            while (detector(x + 99, y)) {
                if (detector(x, y + 99) && detector(x + 99, y + 99)) {
                    return (x * 10000 + y);
                }
                x++;
            }
            y++;
        }

    }
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator