01020304050607080910111213141516171819202122232425

Advent of Code

2016/1

No Time for a Taxicab

in C#

by encse

Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace AdventOfCode.Y2016.Day01;

[ProblemName("No Time for a Taxicab")]
class Solution : Solver {

    public object PartOne(string input) {
        var (irow, icol) = Travel(input).Last();
        return irow + icol;
    }

    public object PartTwo(string input) {
        var seen = new HashSet<(int, int)>();
        foreach (var pos in Travel(input)) {
            if (seen.Contains(pos)) {
                return (pos.icol + pos.irow);
            }
            seen.Add(pos);
        }
        throw new Exception();
    }

    IEnumerable<(int irow, int icol)> Travel(string input) {
        var (irow, icol) = (0, 0);
        var (drow, dcol) = (-1, 0);
        yield return (irow, icol);

        foreach (var stm in Regex.Split(input, ", ")) {
            var d = int.Parse(stm.Substring(1));

            (drow, dcol) = stm[0] switch {
                'R' => (dcol, -drow),
                'L' => (-dcol, drow),
                _ => throw new ArgumentException()
            };
           
            for (int i = 0; i < d; i++) {
                (irow, icol) = (irow + drow, icol + dcol);
                yield return (irow, icol);
            }
        }
    }
}

Please ☆ my repo if you like it!

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