01020304050607080910111213141516171819202122232425

Advent of Code

2017/11

Hex Ed

in C#

by encse

Crossing the bridge, you've barely reached the other side of the stream when a program comes up to you, clearly in distress. "It's my child process," she says, "he's gotten lost in an infinite grid!"

Fortunately for her, you have plenty of experience with infinite grids.

Read the full puzzle.

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

namespace AdventOfCode.Y2017.Day11;

[ProblemName("Hex Ed")]
class Solution : Solver {

    public object PartOne(string input) => Distances(input).Last();

    public object PartTwo(string input) => Distances(input).Max();

    IEnumerable<int> Distances(string input) => 
        from w in Wander(input) select (Math.Abs(w.x) + Math.Abs(w.y) + Math.Abs(w.z))/2;

    IEnumerable<(int x, int y, int z)> Wander(string input) {
        var (x, y, z) = (0, 0, 0);
        foreach (var dir in input.Split(',')) {
            switch (dir) {
                case "n":  (x, y, z) = (x + 0, y + 1, z - 1); break;
                case "ne": (x, y, z) = (x + 1, y + 0, z - 1); break;
                case "se": (x, y, z) = (x + 1, y - 1, z + 0); break;
                case "s":  (x, y, z) = (x + 0, y - 1, z + 1); break;
                case "sw": (x, y, z) = (x - 1, y + 0, z + 1); break;
                case "nw": (x, y, z) = (x - 1, y + 1, z + 0); break;
                default: throw new ArgumentException(dir);
            }
            yield return (x, y, z);
        }
    }
}

Please ☆ my repo if you like it!

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