010203040506

Advent of Code

2026/6

Obscured Airfield

in C#

by encse

Visibility from the storms is so bad that you don't even notice the towering control tower until you're almost right next to it. Although there haven't been any planes in or out recently, an entire delivery of pixie dust is still here from one of the last planes that made it in. Since it's not a very safe substance to handle, the Elves shoo you away while they transfer it to the train.

Because no planes are flying, the airfield staff are taking this opportunity to install a completely new airfield lighting system. The lighting system is a large, square grid of lights; each light is connected to the orthogonally (not diagonally) adjacent lights by wires.

Visit the website for the full story and full puzzle description.

The proper solution is probably giving coordinates to the corners first, then compute the rest from these. Once you know the position of everything in this coordinate system, you figure out the transformation to the one Eric gave as using rotation/translation and possibly flipping. But I did feel like messing with coordinate transforms today, so went for the Santa way.

To localize a node 'N': measure the distances from the known nodes in the graph (using the algo from part 1). Draw a diamond shape around the known nodes with the computed distances. If they intersect each other in a single point: we have localized 'N'.

This would't necessarily work, because taxi driver metric is not as nice as euclidean. You don't get a single point in 2d if you know the distances from 3 distinct points.

But in this particular problem if you take the labels in increasing order you can put coordinates on them one by one. Each new point rules out candidates for the later labels and it folds out nicely.

The only point that was not localized this way in my input had label '8'.

namespace AdventOfCode.Y2026.Day06;

using System.Collections.Generic;
using System.Linq;

using Graph = System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<int>>;

[ProblemName("Obscured Airfield")]
class Solution : Solver {

    public object PartOne(string input) {
        var g = Parse(input);

        // corners have 2 neighbours:
        var corners = g.Where(kvp => kvp.Value.Count == 2).Select(x => x.Key).ToArray();

        // 'center' is the node that is in equal distance from the 4 corners:
        var distances = corners.Select(n => Distances(g, n)).ToArray();
        return g.Keys.Single(n => 
            distances[0][n] == distances[1][n] && 
            distances[1][n] == distances[2][n] && 
            distances[2][n] == distances[3][n]
        );
    }

    public object PartTwo(string input) {
        var g = Parse(input);

        var map = new Dictionary<int, (int x, int y)> {
            { 1, (47, 30) },
            { 2, (29, 59) },
            { 3, (56, 35) }
        };

        // A simple heuristics that happens to work (for my input). We can 
        // localize the points in increasing order as follows.
        foreach (var i in g.Keys.OrderBy(x => x)) {
            // compute the distance of 'i' in the graph from the point with label '1'
            var d = Distances(g, 1)[i];

            // the coordinate candidates for 'i' at distance 'd' are spread in a 
            // diamond shape pattern centered around '1':
            var candidates = Diamond(map[1], d).ToHashSet();

            // compute this diamond around the already localized points.
            // if the intersection of these candidates results in a single 
            // position, that must be the position of i:
            foreach (var (label, coords) in map) {
                d = Distances(g, label)[i];
                candidates.IntersectWith(Diamond(coords, d));
                if (candidates.Count == 1) {
                     map[i] = candidates.Single();
                     break;
                }
            }
        }

        return map[2026].x + "," + map[2026].y;
    }


    // returns the coordinates of a diamond shape with 'radius' r around c.
    // eg. for r = 2 you get the points marked with '#':
    //             #
    //            # #
    //           # c #
    //            # #
    //             #
    List<(int, int)> Diamond((int x, int y) c, int r) {
        var res = new List<(int, int)>();
        if (r == 0) {
            res.Add(c);
        }
        for (var i = 0; i < r; i++) {
            res.Add((c.x + i, c.y - r + i));
            res.Add((c.x + r - i, c.y + i));
            res.Add((c.x - i, c.y + r - i));
            res.Add((c.x - r + i, c.y - i));
        }
        return res;
    }

    // return the distances from start to every node of g using
    // standard breadth first distance finder
    Dictionary<int, int> Distances(Graph g, int start) {
        var q = new Queue<(int n, int dist)>();
        q.Enqueue((start, 0));

        var res = new Dictionary<int, int>();
        while (q.Any()) {
            var (n, d) = q.Dequeue();
            if (!res.ContainsKey(n)) {
                res.Add(n, d);
                foreach (var nTo in g[n]) {
                    q.Enqueue((nTo, d + 1));
                }
            }
        }
        return res;
    }

    Graph Parse(string input) {
        var g = new Graph();
        void add(int a, int b) {
            if (!g.ContainsKey(a)) {
                g.Add(a, new List<int>());
            }
            g[a].Add(b);
        }
        foreach (var line in input.Split('\n')) {
            var parts = line.Split("-");
            var a = parts[0];
            var connTo = parts[1].Split(',');
            foreach (var b in connTo) {
                add(int.Parse(a), int.Parse(b));
                add(int.Parse(b), int.Parse(a));
            }
        }
        return g;
    }
}

Please ☆ my repo if you like it!

© 2026 | Advent of Code is a registered trademark in the US | Images provided by ChatGPT (2026+), in previous years Nano Banana and Bing