Exemplo n.º 1
0
        private int RunBot(char[,] grid)
        {
            IntCode.Emulator ICE = new IntCode.Emulator(input[0]);
            var response         = IntCode.Emulator.ResultTemplate;

            long       dir = 0;
            Vector2Int loc = new Vector2Int(gridSize / 2, gridSize / 2);

            HashSet <Vector2Int> paintedTiles = new HashSet <Vector2Int>();

            for (int i = 0; ; i++)
            {
                ICE.QueueInput((long)char.GetNumericValue(grid[loc.y, loc.x]));
                response = ICE.Run();
                if (response.Item1 != IntCode.StatusCode.OutputDelivery)
                {
                    break;
                }

                grid[loc.y, loc.x] = response.Item2.ToString()[0];
                paintedTiles.Add(loc);

                response = ICE.Run();
                long dirDelta = response.Item2 == 1 ? 1 : -1;

                dir  = (dir + dirDelta + directions.Length) % directions.Length;
                loc += directions[dir];
            }
            return(paintedTiles.Count);
        }
Exemplo n.º 2
0
        private long RunFeedbackLoop(int[] settings)
        {
            (IntCode.StatusCode, long)response = (IntCode.StatusCode.Null, 0);
            for (int i = 0; i < thrusters.Length; i++)
            {
                thrusters[i].Reboot();
                thrusters[i].ExpandMem(20_000_000); // yup
                thrusters[i].QueueInput(settings[i], response.Item2);
                response = thrusters[i].Run();
            }

            int currentThruster = 0;

            while (true)
            {
                IntCode.Emulator thruster = thrusters[currentThruster];
                thruster.QueueInput(response.Item2);
                response = thruster.Run();

                if (response.Item1 == IntCode.StatusCode.Complete && currentThruster == thrusters.Length - 1)
                {
                    return(response.Item2);
                }

                currentThruster++;
                currentThruster %= thrusters.Length;
            }
        }
Exemplo n.º 3
0
        public override object Task1()
        {
            IntCode.Emulator ICE = new IntCode.Emulator(input[0]);
            var response         = IntCode.Emulator.ResultTemplate;

            int blockTiles = 0;

            while (true)
            {
                response = ICE.Run();
                if (response.Item1 == IntCode.StatusCode.Complete)
                {
                    break;
                }
                long x    = response.Item2;
                long y    = ICE.Run().Item2;
                long tile = ICE.Run().Item2;
                display[y, x] = tiles[tile];
                if (tile == 2)
                {
                    blockTiles++;
                }
            }
            return(blockTiles);
        }
Exemplo n.º 4
0
        public override object Task1()
        {
            IntCode.Emulator ICE = new IntCode.Emulator(rawInput);

            string dir = ExploreMap(ICE);

            return(TrySecDoor(ICE, dir));
        }
Exemplo n.º 5
0
        public override object Task2()
        {
            long[] code = IntCode.Tools.ParseCode(input[0]);
            code[0] = 2;
            IntCode.Emulator ICE = new IntCode.Emulator(code);
            var response         = IntCode.Emulator.ResultTemplate;

            long ballX   = 0;
            long paddleX = 0;

            // make grid
            for (int _ = 0; _ < DISPLAY_H * DISPLAY_W; _++)
            {
                long x    = ICE.Run().Item2;
                long y    = ICE.Run().Item2;
                long tile = ICE.Run().Item2;
                if (tile == 4)
                {
                    ballX = x;
                }
                if (tile == 3)
                {
                    paddleX = x;
                }
            }

            // play
            while (true)
            {
                response = ICE.Run();
                if (response.Item1 == IntCode.StatusCode.Complete)
                {
                    return(response.Item2);
                }
                if (response.Item1 == IntCode.StatusCode.InputRequest)
                {
                    ICE.QueueInput(comp(ballX, paddleX));
                    ICE.Run();
                }
                long x = response.Item2;
                long y = ICE.Run().Item2;
                long z = ICE.Run().Item2;
                if (x == -1)
                {
                    continue;
                }
                if (z == 4)
                {
                    ballX = x;
                }
                if (z == 3)
                {
                    paddleX = x;
                }
            }
        }
Exemplo n.º 6
0
 private long SendString(IntCode.Emulator ICE, string instructions)
 {
     long[] inst = new long[instructions.Length + 1];
     for (int i = 0; i < instructions.Length; i++)
     {
         inst[i] = instructions[i];
     }
     inst[inst.Length - 1] = (char)10;
     ICE.QueueInput(inst);
     return(ICE.Run().Item2);
 }
Exemplo n.º 7
0
        // Makes a loop around all the rooms by always trying to take the left-
        // hand door when possible. If no door is to the drone's left it turns
        // right until thre is.
        // Returns once the security checkpoint is reached for the 2nd time,
        // ensuring every other room has been visisted at least one time.
        // Returns a string for the direction of the pressure-plate room.
        private string ExploreMap(IntCode.Emulator ICE)
        {
            Queue <char> outBuffer = new Queue <char>();

            int  currentDir = 0;
            bool secCheck   = false;

            while (true)
            {
                var resp = ICE.Run();
                if (resp.Item1 == IntCode.StatusCode.InputRequest)
                {
                    string report = string.Join("", outBuffer);
                    outBuffer.Clear();
                    string[] doors = report.Split("lead:")[1].Split("\n\n")[0].Substring(3).Split("\n- ");

                    if (report.Contains("here:"))
                    {
                        string[] items = report.Split("here:")[1].Split("\n\n")[0].Substring(3).Split("\n- ");
                        foreach (string item in items)
                        {
                            if (Array.IndexOf(forbiddenItems, item) != -1)
                            {
                                continue;
                            }
                            SendString(ICE, "take " + item);
                        }
                    }

                    if (report.Contains("Security Checkpoint"))
                    {
                        if (secCheck == false)
                        {
                            secCheck   = true;
                            currentDir = (currentDir + 2) % 4;
                            SendString(ICE, directions[currentDir]);
                            continue;
                        }
                        else
                        {
                            return(GetNextDirection(currentDir, doors).Item2);
                        }
                    }

                    string nextRoom;
                    (currentDir, nextRoom) = GetNextDirection(currentDir, doors);

                    SendString(ICE, nextRoom);
                    ICE.Run();
                }

                outBuffer.Enqueue((char)resp.Item2);
            }
        }
Exemplo n.º 8
0
 private void MakeNetwork()
 {
     long[] program = IntCode.Tools.ParseCode(rawInput);
     network = new IntCode.Emulator[networkSize];
     for (int i = 0; i < networkSize; i++)
     {
         network[i] = new IntCode.Emulator(program);
         var resp = network[i].Run();
         network[i].QueueInput(i);
     }
 }
Exemplo n.º 9
0
        private (IntCode.StatusCode, string) RunUntilDone(IntCode.Emulator ICE)
        {
            List <char> outputBuffer = new List <char>();

            while (true)
            {
                var r = ICE.Run();
                if (r.Item1 != IntCode.StatusCode.OutputDelivery)
                {
                    return(r.Item1, string.Join("", outputBuffer));
                }
                outputBuffer.Add((char)r.Item2);
            }
        }
Exemplo n.º 10
0
        public override object Task2()
        {
            long[] code = IntCode.Tools.ParseCode(input[0]);
            code[0] = 2;

            IntCode.Emulator ICE = new IntCode.Emulator(code);
            var response         = IntCode.Emulator.ResultTemplate;

            ICE.ExpandMem(1800);

            // Complete program:
            // L,10,L,12,R,6 | R,10,L,4,L,4,L,12 | L,10,L,12,R,6 | R,10,L,4,L,4,L,12 | L,10,L,12,R,6 | L,10,R,10,R,6,L,4 | R,10,L,4,L,4,L,12 | L,10,R,10,R,6,L,4 | L,10,L,12,R,6 | L,10,R,10,R,6,L,4
            // A                                   A                                   A                                                                           A
            // L,10,L,12,R,6
            //                 B                                   B                                                       B
            //                 R,10,L,4,L,4,L,12
            //                                                                                         C                                       C                                   C
            //                                                                                         L,10,R,10,R,6,L,4
            // A,B,A,B,A,C,B,C,A,C

            string Main = "A,B,A,B,A,C,B,C,A,C";
            string A    = "L,10,L,12,R,6";
            string B    = "R,10,L,4,L,4,L,12";
            string C    = "L,10,R,10,R,6,L,4";

            Queue <char> outputbuffer = new Queue <char>();

            ICE.Run();
            SendString(ICE, Main);
            ICE.Run();
            SendString(ICE, A);
            ICE.Run();
            SendString(ICE, B);
            ICE.Run();
            SendString(ICE, C);
            ICE.Run();
            SendString(ICE, "y");

            while (true)
            {
                response = ICE.Run();
                if (response.Item1 == IntCode.StatusCode.Complete)
                {
                    return(response.Item2);
                }
            }
        }
Exemplo n.º 11
0
        public override object Task2()
        {
            IntCode.Emulator ICE = new IntCode.Emulator(input[0])
            {
                Verbose = false
            };

            int y = shipSize;
            int x = 0;

            bool changes = true;

            while (changes)
            {
                changes = false;
                // move down until top-right in field
                while (true)
                {
                    ICE.Reboot();
                    ICE.QueueInput(x + shipSize - 1, y);
                    if (ICE.Run().Item2 == 1)
                    {
                        break;
                    }
                    y++;
                    changes = true;
                }

                // move right until bottom-left in field
                while (true)
                {
                    ICE.Reboot();
                    ICE.QueueInput(x, y + shipSize - 1);
                    if (ICE.Run().Item2 == 1)
                    {
                        break;
                    }
                    x++;
                    changes = true;
                }
            }
            return((x * 10000) + y);
        }
Exemplo n.º 12
0
        public override object Task1()
        {
            IntCode.Emulator ICE = new IntCode.Emulator(input[0])
            {
                Verbose = false
            };

            long counter = 0;

            for (int y = 0; y < gridSize; y++)
            {
                for (int x = 0; x < gridSize; x++)
                {
                    ICE.QueueInput(x, y);
                    counter += ICE.Run().Item2;
                    ICE.Reboot();
                }
            }
            return(counter);
        }
Exemplo n.º 13
0
        public override object Task1()
        {
            IntCode.Emulator ICE = new IntCode.Emulator(input[0]);
            var response         = IntCode.Emulator.ResultTemplate;

            ICE.ExpandMem(1800);

            grid = new List <List <char> >();

            List <char> row = new List <char>();

            while (true)
            {
                response = ICE.Run();
                if (response.Item2 == 10)
                {
                    if (row.Count == 0)
                    {
                        break;
                    }
                    grid.Add(row);
                    row = new List <char>();
                    continue;
                }
                row.Add((char)response.Item2);
            }

            int alignParamSum = 0;

            for (int r = 1; r < grid.Count - 1; r++)
            {
                for (int c = 1; c < grid[r].Count - 1; c++)
                {
                    if (grid[r][c] == scaffold && grid[r + 1][c] + grid[r - 1][c] + grid[r][c + 1] + grid[r][c - 1] == scaffold * 4)
                    {
                        alignParamSum += c * r;
                    }
                }
            }
            return(alignParamSum);
        }
Exemplo n.º 14
0
        public override object Task2()
        {
            thrusters = new IntCode.Emulator[5];
            for (int i = 0; i < 5; i++)
            {
                thrusters[i] = new IntCode.Emulator(thrusterCode);
            }

            int[] phaseSettings = new int[] { 5, 6, 7, 8, 9 };
            long  maxSignal     = 0;

            foreach (int[] arr in Permutator.Permutate(phaseSettings))
            {
                long sig = RunFeedbackLoop(arr);
                if (sig > maxSignal)
                {
                    maxSignal = sig;
                }
            }
            return(maxSignal);
        }
Exemplo n.º 15
0
        private string TrySecDoor(IntCode.Emulator ICE, string doorDirection)
        {
            SendString(ICE, "inv");
            string           output    = RunUntilDone(ICE).Item2;
            HashSet <string> inventory = new HashSet <string>();

            foreach (string line in output.Split((char)10))
            {
                if (line.StartsWith('-'))
                {
                    inventory.Add(line.Substring(2));
                }
            }

            foreach (HashSet <string> i in PermutateInventory(inventory))
            {
                HashSet <string> remove = new HashSet <string>(inventory);
                remove.ExceptWith(i);
                foreach (string r in remove)
                {
                    DropItem(ICE, r);
                }

                HashSet <string> add = new HashSet <string>(i);
                add.ExceptWith(inventory);
                foreach (string a in add)
                {
                    PickUpItem(ICE, a);
                }
                inventory = new HashSet <string>(i);

                SendString(ICE, doorDirection);
                var resp = RunUntilDone(ICE);
                if (resp.Item1 != IntCode.StatusCode.InputRequest)
                {
                    return(resp.Item2.Split("typing ")[1].Split(' ')[0]);
                }
            }
            return(string.Empty);
        }
Exemplo n.º 16
0
        public override object Task1()
        {
            thrusterCode = IntCode.Tools.ParseCode(input[0]);
            IntCode.Emulator ICE       = new IntCode.Emulator(thrusterCode);
            long             maxSignal = 0;

            var response = IntCode.Emulator.ResultTemplate;

            foreach (int[] arr in Permutator.Permutate(new int[] { 0, 1, 2, 3, 4 }))
            {
                response.Item2 = 0;
                foreach (int s in arr)
                {
                    ICE.Reboot();
                    ICE.QueueInput(s, response.Item2);
                    response = ICE.Run();
                }
                if (response.Item2 > maxSignal)
                {
                    maxSignal = response.Item2;
                }
            }
            return(maxSignal);
        }
Exemplo n.º 17
0
 public override object Task1()
 {
     IntCode.Emulator ICE = new IntCode.Emulator(input[0]);
     ICE.QueueInput(1);
     return(ICE.Run().Item2);
 }
Exemplo n.º 18
0
 private void DropItem(IntCode.Emulator ICE, string item)
 {
     SendString(ICE, "drop " + item);
     RunUntilDone(ICE);
 }
Exemplo n.º 19
0
 private void PickUpItem(IntCode.Emulator ICE, string item)
 {
     SendString(ICE, "take " + item);
     RunUntilDone(ICE);
 }