示例#1
0
    public override void PartOne()
    {
        const int size    = 256;
        var       nums    = Enumerable.Range(0, size).Repeat(2).ToArray();
        var       current = 0;
        var       skip    = 0;

        foreach (var length in InputLine.Csv().Ints())
        {
            new Span <int>(nums, current, length).Reverse();
            if (current + length > size)
            {
                Array.Copy(nums, size, nums, 0, current + length - size);
                Array.Copy(nums, current, nums, current + size, size - current);
            }
            else
            {
                Array.Copy(nums, current, nums, current + size, length);
            }
            current = (current + length + skip) % size;
            skip++;
        }
        var result = nums[0] * nums[1];

        WriteLn(result);
    }
示例#2
0
    public void Dance(CircularBuffer <int> buf)
    {
        var offset = 0;

        foreach (var cmd in InputLine.Csv())
        {
            if (cmd[0] == 's')
            {
                offset -= int.Parse(cmd[1..]);
示例#3
0
    public override void PartOne()
    {
        var target = InputLine.Csv()
                     .Select(Pos3D.ParseHexDir)
                     .Aggregate(Pos3D.Add);
        var result = target.HexDistance(Pos3D.Origin);

        WriteLn(result);
    }
示例#4
0
    public override void PartTwo()
    {
        var result = InputLine.Csv()
                     .Select(Pos3D.ParseHexDir)
                     .Scan(Pos3D.Add)
                     .Select(p => p.HexDistance(Pos3D.Origin))
                     .Max();

        WriteLn(result);
    }
示例#5
0
    public override void PartTwo()
    {
        var pos = InputLine.Csv().Ints().ToList();
        var min = pos.Min();
        var max = pos.Max() + 1;

        Interval range  = min..max;
        var      target = range.Min(i => pos.Select(p => Math.Abs(p - i)).Select(Algorithms.Sum1ToN).Sum());

        WriteLn(target);
    }
示例#6
0
    public IEnumerable <Pos> GetPoints()
    {
        var dir = Pos.Up;
        var pos = Pos.Origin;

        yield return(pos);

        foreach (var move in InputLine.Csv(true))
        {
            dir  = move[0] == 'L' ? dir.CounterClockwise() : dir.Clockwise();
            pos += dir * move[1..].AsInt();
示例#7
0
    public long Steps(int count)
    {
        var counts = InputLine.Csv().Ints().Frequencies()
                     .ToArrayByIndex(9, pair => pair.Key, pair => (long)pair.Value);

        var timers = new CircularBuffer <long>(counts);

        for (var i = 0; i < count; i++)
        {
            timers.Offset++;
            timers[6] += timers[8];
        }
        return(timers.Unordered().Sum());
    }
示例#8
0
    public override void PartTwo()
    {
        var program = InputLine.Csv().Longs().ToArray();

        foreach (var sequence in Algorithms.Sequences(2, 100, true))
        {
            var noun = sequence[0];
            var verb = sequence[1];
            var c    = new Computer(program.ToArray())
            {
                [1] = noun, [2] = verb
            };
            c.Execute();
            if (c[0] == 19690720)
            {
                WriteLn(100 * noun + verb);
                return;
            }
        }
        WriteLn("None");
    }