Ejemplo n.º 1
0
    public override void PartTwo()
    {
        Grid <int> map = Input.Select2D(c => c == '#' ? 2 : 0).ToGrid();

        Carrier   = map.Bounds.MidPos;
        Direction = Pos.Up;
        var infections = 0;

        void Step()
        {
            var state = map[Carrier];

            if (state == 0)
            {
                Direction = Direction.CounterClockwise();
            }
            else if (state == 2)
            {
                Direction = Direction.Clockwise();
            }
            else if (state == 3)
            {
                Direction = -Direction;
            }
            infections = (map[Carrier] = (state + 1) % 4) == 2 ? infections + 1 : infections;
            Carrier   += Direction;
        }

        foreach (var _ in Enumerable.Range(0, 10_000_000))
        {
            Step();
        }

        WriteLn(infections);
    }
Ejemplo n.º 2
0
    public override void PartOne()
    {
        Grid <bool> map = Input.Select2D(c => c == '#').ToGrid();

        Carrier   = map.Bounds.MidPos;
        Direction = Pos.Up;
        var infections = 0;

        void Step()
        {
            if (map[Carrier])
            {
                Direction = Direction.Clockwise();
            }
            else
            {
                Direction = Direction.CounterClockwise();
            }
            infections = (map[Carrier] = !map[Carrier]) ? infections + 1 : infections;
            Carrier   += Direction;
        }

        foreach (var _ in Enumerable.Range(0, 10000))
        {
            Step();
        }

        WriteLn(infections);
    }