예제 #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("");
        }
예제 #2
0
파일: Day201905.cs 프로젝트: payou42/aoc
        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("");
        }
예제 #3
0
파일: Day201925.cs 프로젝트: payou42/aoc
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                // Fetch all the item
                string[] commands =
                {
                    "west", "west",                 "west",  "west",  "take dark matter",         "east",  "south",        "take fixed point", "west",            "take food ration",
                    "east", "north",                "east",  "south", "take astronaut ice cream", "south", "take polygon", "east",             "take easter egg",
                    "east", "take weather machine", "north", "inv"
                };

                _cpu.Reset(_code);
                Step();
                foreach (string command in commands)
                {
                    Step(command);
                }

                // Now try any combinaison of them
                string[] items     = { "polygon", "fixed point", "astronaut ice cream", "easter egg", "dark matter", "food ration", "weather machine" };
                int      inventory = 0;

                // Drop evrything
                foreach (string item in items)
                {
                    Step($"drop {item}");
                }

                while (_cpu.State != IntCpu.RunningState.Halted)
                {
                    int next = inventory + 1;
                    for (int i = 0; i < 7; ++i)
                    {
                        if ((((next >> i) & 0x01) == 0x1) && (((inventory >> i) & 0x01) == 0x0))
                        {
                            Step($"take {items[i]}");
                        }

                        if ((((next >> i) & 0x01) == 0x0) && (((inventory >> i) & 0x01) == 0x1))
                        {
                            Step($"drop {items[i]}");
                        }
                    }

                    Step($"north");
                    inventory = next;
                }

                return(_result.ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                return("Victory!");
            }

            return("");
        }
예제 #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());
 }
예제 #5
0
파일: Day201913.cs 프로젝트: payou42/aoc
        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("");
        }
예제 #6
0
파일: Day201909.cs 프로젝트: payou42/aoc
        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("");
        }
예제 #7
0
파일: Day201915.cs 프로젝트: payou42/aoc
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                // prepare the robot
                _cpu.Reset(Aoc.Framework.Input.GetLongVector(this, ","));
                _position    = new Point(0, 0);
                _board[0, 0] = 0;
                Move(Direction.Up);
                Move(Direction.Down);
                Move(Direction.Left);
                Move(Direction.Right);
                return(_board[_oxygen].ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                // Clean the board
                _board.Cells.ForEach(c => _board[c.Item1] = c.Item2 > 0 ? 0 : c.Item2);
                _board[_oxygen] = 1;

                long minutes = 0;
                while (true)
                {
                    // Get the oxygen
                    var  oxygen = _board.Cells.Where(c => c.Item2 == 1).ToList();
                    long count  = oxygen.Count();

                    // Spread oxygen
                    foreach (var c in oxygen)
                    {
                        Spread(c.Item1);
                    }

                    // Count oxygen again
                    long newCount = _board.Cells.Count(c => c.Item2 == 1);
                    if (newCount == count)
                    {
                        break;
                    }

                    // Add minutes
                    minutes++;
                }

                return(minutes.ToString());
            }

            return("");
        }
예제 #8
0
파일: Day201907.cs 프로젝트: payou42/aoc
        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);
        }
예제 #9
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);
        }
예제 #10
0
파일: Day201921.cs 프로젝트: payou42/aoc
        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("");
        }
예제 #11
0
파일: Day201917.cs 프로젝트: payou42/aoc
        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("");
        }