Example #1
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                _cpu.Code[1] = 12;
                _cpu.Code[2] = 2;
                _cpu.Run();
                return(_cpu.Code[0].ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                for (int noun = 0; noun <= 99; noun++)
                {
                    for (int verb = 0; verb <= 99; verb++)
                    {
                        _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                        _cpu.Code[1] = noun;
                        _cpu.Code[2] = verb;
                        _cpu.Run();
                        if (_cpu.Code[0] == 19690720)
                        {
                            return((100 * noun + verb).ToString());
                        }
                    }
                }
                return("Not found");
            }

            return("");
        }
Example #2
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                _cpu.Input.Enqueue(1);
                _cpu.Run();

                while (_cpu.Output.Count > 0)
                {
                    long result = _cpu.Output.Dequeue();
                    if (_cpu.Output.Count == 0)
                    {
                        return(result.ToString());
                    }
                }

                return("Not found");
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                _cpu.Input.Enqueue(5);
                _cpu.Run();
                return(_cpu.Output.Dequeue().ToString());
            }

            return("");
        }
Example #3
0
        private void Step(string command = null)
        {
            if (command != null)
            {
                foreach (char c in command)
                {
                    _cpu.Input.Enqueue((long)c);
                }
                _cpu.Input.Enqueue(10);
            }

            _cpu.Run();
            while (_cpu.Output.Count > 0)
            {
                char c = (char)_cpu.Output.Dequeue();
                if (char.IsDigit(c))
                {
                    if (!_first)
                    {
                        _result = _result * 10 + int.Parse(c.ToString());
                    }

                    _first = false;
                }
            }
        }
Example #4
0
 private long Probe(int x, int y)
 {
     _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
     _cpu.Input.Enqueue(x);
     _cpu.Input.Enqueue(y);
     _cpu.Run(true);
     return(_cpu.Output.Dequeue());
 }
Example #5
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                // Run the simulation
                _board = new Board2D <int>();
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                while (_cpu.State != IntCpu.RunningState.Halted)
                {
                    _cpu.Run();
                }

                // Fill the board
                while (_cpu.Output.Count > 0)
                {
                    int x = (int)_cpu.Output.Dequeue();
                    int y = (int)_cpu.Output.Dequeue();
                    int a = (int)_cpu.Output.Dequeue();
                    _board[x, y] = a;
                }

                // Count the non empty cells
                return(_board.Cells.Count(c => c.Item2 == BLOCK).ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                // Run the simulation
                _board = new Board2D <int>();
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                _cpu.Code[0] = 2;

                while (_cpu.State != IntCpu.RunningState.Halted)
                {
                    Simulate();
                    Play();
                }

                return(_score.ToString());
            }

            return("");
        }
Example #6
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                _cpu.Input.Enqueue(1);
                _cpu.Run();
                return(_cpu.Output.Dequeue().ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                _cpu.Input.Enqueue(2);
                _cpu.Run();
                return(_cpu.Output.Dequeue().ToString());
            }

            return("");
        }
Example #7
0
        private long DirectRun(List <int> settings)
        {
            long output = 0;

            for (int i = 0; i < settings.Count; ++i)
            {
                _cpu.Reset(_code);
                _cpu.Input.Enqueue(settings[i]);
                _cpu.Input.Enqueue(output);
                _cpu.Run(true);
                output = _cpu.Output.Dequeue();
            }

            return(output);
        }
Example #8
0
        private long Paint(long initial)
        {
            long painted = 0;

            _hull       = new Board2D <long>();
            _hull[0, 0] = initial;
            _painted    = new Board2D <bool>();
            _direction  = Direction.Up;
            _position   = new Point(0, 0);
            _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
            _cpu.Input.Enqueue(_hull[_position.X, _position.Y]);
            while (_cpu.State != IntCpu.RunningState.Halted)
            {
                _cpu.Run();

                if (_cpu.State != IntCpu.RunningState.Halted)
                {
                    if (!_painted[_position.X, _position.Y])
                    {
                        _painted[_position.X, _position.Y] = true;
                        painted++;
                    }

                    _hull[_position.X, _position.Y] = _cpu.Output.Dequeue();
                    long turn = _cpu.Output.Dequeue();
                    if (turn == 0)
                    {
                        _direction = Board2D <long> .Turn(_direction, Direction.Left);
                    }

                    if (turn == 1)
                    {
                        _direction = Board2D <long> .Turn(_direction, Direction.Right);
                    }

                    _position = Board2D <long> .MoveForward(_position, _direction);

                    _cpu.Input.Enqueue(_hull[_position.X, _position.Y]);
                }
            }

            return(painted);
        }
Example #9
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                // Load the program
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));

                // Hardcode our springdroid program
                // !A + !C.D
                //   !A
                //     There's a hole next to us, we need to jump no matter what
                //   !C.D
                //     There's a hole in 3 cases and the next one is ground, jump right now.
                //     The idea is to jump as soon as possible when we can.
                string[] program = new string[5]
                {
                    "NOT C T",
                    "AND D T",
                    "NOT A J",
                    "OR T J",
                    "WALK",
                };

                // Enter input
                foreach (string s in program)
                {
                    foreach (char c in s)
                    {
                        _cpu.Input.Enqueue((long)c);
                    }

                    _cpu.Input.Enqueue((long)10);
                }

                // Run the simulation
                _cpu.Run();

                // Show the output
                for (int i = 0; i < 33; ++i)
                {
                    _cpu.Output.Dequeue();
                }

                if (_cpu.Output.Count == 1)
                {
                    return(_cpu.Output.Dequeue().ToString());
                }

                while (_cpu.Output.Count > 0)
                {
                    char c = (char)_cpu.Output.Dequeue();
                    if (c == 10)
                    {
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.Write(c);
                    }
                }
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                // Load the program
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));

                // Hardcode our springdroid program
                // (!A+!B+!C).D.(E+H)
                //   (!A+!B+!C)
                //       There's at elast a hole in the next 3 cases, we need to jump as soon as possible
                //   D
                //       If we jump now, we'll land on ground
                //   (E+H)
                //       If we need to jump right after lunding (!E) then we'll land on ground again (H).
                //       Or if we can't jump right away (!H), we can just walk (E)
                string[] program = new string[11]
                {
                    "NOT A T",
                    "NOT B J",
                    "OR J T",
                    "NOT C J",
                    "OR J T",
                    "AND D T",
                    "NOT E J",
                    "NOT J J",
                    "OR H J",
                    "AND T J",
                    "RUN",
                };

                // Enter input
                foreach (string s in program)
                {
                    foreach (char c in s)
                    {
                        _cpu.Input.Enqueue((long)c);
                    }

                    _cpu.Input.Enqueue((long)10);
                }

                // Run the simulation
                _cpu.Run();

                // Show the output
                for (int i = 0; i < 33; ++i)
                {
                    _cpu.Output.Dequeue();
                }

                if (_cpu.Output.Count == 1)
                {
                    return(_cpu.Output.Dequeue().ToString());
                }

                while (_cpu.Output.Count > 0)
                {
                    char c = (char)_cpu.Output.Dequeue();
                    if (c == 10)
                    {
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.Write(c);
                    }
                }
            }

            return("");
        }
Example #10
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                // Run the program
                int x      = 0;
                int y      = 0;
                int width  = 0;
                int height = 0;
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                _cpu.Run();

                // Build the camera view
                while (_cpu.Output.Count > 0)
                {
                    // Get the value
                    long value = _cpu.Output.Dequeue();

                    // Process it
                    // Running the ASCII program on your Intcode computer will provide the current view of the scaffolds.
                    // This is output, purely coincidentally, as ASCII code: 35 means #, 46 means .,
                    // 10 starts a new line of output below the current one, and so on. (Within a line, characters are drawn left-to-right.)
                    // In the camera output, # represents a scaffold and . represents open space.
                    // The vacuum robot is visible as ^, v, <, or > depending on whether it is facing up, down, left, or right respectively.
                    // When drawn like this, the vacuum robot is always on a scaffold; if the vacuum robot ever walks off of a scaffold
                    // and begins tumbling through space uncontrollably, it will instead be visible as X.
                    switch (value)
                    {
                    case 10:
                    {
                        x = 0;
                        y++;
                        height++;
                        break;
                    }

                    default:
                    {
                        _board[x, y] = value;
                        x++;
                        width = Math.Max(width, x);
                        break;
                    }
                    }
                }

                // Debug
                // Draw(width, height - 1);

                // Get the intersections
                var intersections = _board.Cells.Where(c => IsIntersection(c.Item1));

                // Evaluate the calibration value
                long calibration = intersections.Select(c => c.Item1.X * c.Item1.Y).Sum();
                return(calibration.ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                // R,10,R,8,L,10,L,10,R,8,L,6,L,6,R,8,L,6,L,6,R,10,R,8,L,10,L,10,L,10,R,10,L,6,R,8,L,6,L,6,L,10,R,10,L,6,L,10,R,10,L,6,R,8,L,6,L,6,R,10,R,8,L,10,L,10
                string[] commands = new string[5]
                {
                    "A,B,B,A,C,B,C,C,B,A",  // Main
                    "R,10,R,8,L,10,L,10",   // A
                    "R,8,L,6,L,6",          // B
                    "L,10,R,10,L,6",        // C
                    "n",                    // Live feed mode
                };

                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                _cpu.Code[0] = 2;

                // Send the commands
                foreach (string line in commands)
                {
                    foreach (char c in line)
                    {
                        _cpu.Input.Enqueue((long)c);
                    }

                    _cpu.Input.Enqueue(10);
                }

                _cpu.Run();
                long dust = 0;
                while (_cpu.Output.Count > 0)
                {
                    dust = _cpu.Output.Dequeue();
                }

                return(dust.ToString());
            }

            return("");
        }
Example #11
0
        private void Move(Direction d)
        {
            // Get the place we want to visit
            Point moved = Board2D <long> .MoveForward(_position, d);

            // Check if there's a wall
            if (_board[moved] == -1)
            {
                return;
            }

            // Check if it has been visited
            if (_board[moved] > 0)
            {
                // Already visited, update its distance
                if (_board[moved] > _board[_position])
                {
                    _board[moved] = _board[_position] + 1;
                }

                // Don't propragate
                return;
            }

            // Ask the robot to visit the place
            _cpu.Input.Enqueue(DirectionToMovement[(int)d]);
            _cpu.Run(true);
            long result = _cpu.Output.Dequeue();

            switch (result)
            {
            case 0:
            {
                // We hit a wall
                _board[moved] = -1;
                return;
            }

            case 1:
            case 2:
            {
                _board[moved] = _board[_position] + 1;
                _position     = moved;
                if (result == 2)
                {
                    _oxygen = _position;
                }

                // Process other movements
                Move(Direction.Up);
                Move(Direction.Down);
                Move(Direction.Left);
                Move(Direction.Right);

                // Once explored, backtrack
                BackTrack(d);

                // All done
                return;
            }
            }
        }