示例#1
0
        private int General(int gridSize, Returner returner)
        {
            var grid = new BeamGrid(gridSize);

            var computer       = new IntcodeComputer(FileContents);
            int previousFirstY = 0;
            int previousLastY  = 0;

            for (int x = 0; x < gridSize; x++)
            {
                int firstY = -1;
                int lastY  = -1;

                for (int y = previousFirstY; y < gridSize; y++)
                {
                    int output = (int)computer.RunToHalt(null, x, y);
                    grid[x, y] = (PointType)output;
                    computer.Reset();
                    if (grid[x, y] == PointType.Beam)
                    {
                        firstY = y;
                        if (previousLastY < firstY)
                        {
                            previousLastY = firstY;
                        }
                        for (int y0 = firstY; y0 < previousLastY; y0++) // cover gaps
                        {
                            grid[x, y0] = PointType.Beam;
                        }
                        break;
                    }
                }
                for (int y = previousLastY; y < gridSize; y++)
                {
                    int output = (int)computer.RunToHalt(null, x, y);
                    grid[x, y] = (PointType)output;
                    computer.Reset();
                    if (grid[x, y] == PointType.Air)
                    {
                        lastY = y;
                        break;
                    }
                }

                if (firstY > -1)
                {
                    previousFirstY = firstY;
                }
                if (lastY > -1)
                {
                    previousLastY = lastY;
                }
            }

            return(returner(grid));
        }
示例#2
0
        private T General <T>(Returner <T> returner)
        {
            const int cardCount = 10007;

            var deck     = new CardDeck(cardCount);
            var lines    = FileLines;
            var commands = new DeckCommandArray(lines.Select(l => DeckCommand.Parse(l)).ToArray());

            return(returner(deck, commands));
        }
示例#3
0
 public static Exception TryGetValue <T>(Returner <T> returnerDelegate, out T gottenValue)
 {
     try
     {
         gottenValue = returnerDelegate();
         return(null);
     }
     catch (Exception ex)
     {
         gottenValue = default(T);
         return(ex);
     }
 }
示例#4
0
        public static string TryGetValueOrExceptionString <T>(Returner <T> returnerDelegate, ExceptionConverter <string> exceptionConverter)
        {
            Exception exception;
            T         gottenValue;

            if ((exception = TryGetValue <T>(returnerDelegate, out gottenValue)) == null)
            {
                return(gottenValue.ToString());
            }
            else
            {
                return(exceptionConverter(exception));
            }
        }
示例#5
0
        public string Login(string Username, string Password)
        {
            User U = new User();

            U.Username = Username;
            U.Password = Password;
            Returner R = U.Login();

            if (R.Message == Message.Success_Login)
            {
                Session["User"] = R.Data;
                return("true");
            }
            else
            {
                return("false");
            }
        }
示例#6
0
        public string CreateProduct(FormCollection FC)
        {
            Product P = new Product();

            P.Category     = Convert.ToInt32(FC["Cat"]);
            P.PtoSRate     = Convert.ToInt32(FC["per"]);
            P.PurchaseUnit = Convert.ToInt32(FC["pu"]);
            P.SalesUnit    = Convert.ToInt32(FC["su"]);
            P.LastEditBy   = (Session["User"] as User).ID;
            P.Description  = FC["notes"];
            P.ProductName  = FC["name"];
            Returner R = P.Create();

            if (R.Message == Message.Product_Name_Already_Exist)
            {
                return("false");
            }
            else
            {
                return("true");
            }
        }
示例#7
0
        private T General <T>(GeneralFunction beforeOperation, Returner <T> returner)
        {
            var grid = new PanelGrid(gridSize);

            for (int x = 0; x < gridSize; x++)
            {
                for (int y = 0; y < gridSize; y++)
                {
                    grid[x, y] = PanelColor.Untouched;
                }
            }

            var  currentLocation       = new Location2D(gridSize / 2, gridSize / 2);
            var  currentDirection      = Direction.Up;
            int  currentDirectionIndex = 0;
            int  paintedPanels         = 0;
            bool givenFirstOutput      = false;

            beforeOperation(grid, currentLocation);

            var computer = new IntcodeComputer(FileContents);

            computer.InputRequested += InputRequested;
            computer.OutputWritten  += OutputWritten;
            computer.RunToHalt();

            return(returner(paintedPanels, grid));

            BigInteger InputRequested()
            {
                givenFirstOutput = false;
                var(x, y)        = currentLocation;
                return((int)grid[x, y] & 1);
            }

            void OutputWritten(BigInteger output)
            {
                if (givenFirstOutput)
                {
                    AddDirectionIndex(GetDirectionIndexOffset((int)output));
                    currentLocation.Forward(currentDirection, true, true);
                }
                else
                {
                    PaintPanel(currentLocation, (PanelColor)(int)output);
                }

                givenFirstOutput = true;
            }

            int GetDirectionIndexOffset(int output) => output == 0 ? 1 : -1;

            void AddDirectionIndex(int offset)
            {
                currentDirectionIndex = (currentDirectionIndex + offset + 4) % 4;
                currentDirection      = orderedDirections[currentDirectionIndex];
            }

            void PaintPanel(Location2D location, PanelColor color)
            {
                var(x, y) = location;
                if (grid[x, y].HasFlag(PanelColor.Untouched))
                {
                    paintedPanels++;
                }
                grid[x, y] = color;
            }
        }
示例#8
0
 public void ReturnResults()
 {
     Returner.ReturnResults();
 }
示例#9
0
        public string AddAccount(string name, string pass, string per)
        {
            User A = new User();

            A.Password = pass;
            A.Status   = 1;
            A.Username = name;
            string[] Access = per.Split('#');
            //List<Screen> LOUA = new List<Screen>();
            List <UserAccess> LOUA = new List <UserAccess>();

            foreach (string AP in Access)
            {
                if (LOUA.Any(p => p.ScreenID == Convert.ToInt32(AP.Replace("Opt1", "").Replace("Show", "").Replace("Edit", "").Replace("Delete", ""))))
                {
                    var UA = LOUA.Where(p => p.ScreenID == Convert.ToInt32(AP.Replace("Opt1", "").Replace("Show", "").Replace("Edit", "").Replace("Delete", ""))).SingleOrDefault();
                    if (AP.Contains("Opt1"))
                    {
                        UA.ScreenID = int.Parse(AP.Replace("Opt1", ""));
                        UA.Opt1     = true;
                        LOUA.Add(UA);
                    }
                    else if (AP.Contains("Show"))
                    {
                        UA.ScreenID = int.Parse(AP.Replace("Show", ""));
                        foreach (string item in Access.Where(p => p.Contains(UA.ScreenID.ToString())).ToList())
                        {
                            if (item.Contains("Opt1"))
                            {
                                UA.Opt1 = true;
                            }
                            if (item.Contains("Edit"))
                            {
                                UA.CanEdit = true;
                            }
                            if (item.Contains("Delete"))
                            {
                                UA.CanDelete = true;
                            }
                        }
                        LOUA.Add(UA);
                    }
                    else
                    {
                        if (AP.Contains("Edit"))
                        {
                            UA.ScreenID = int.Parse(AP.Replace("Edit", ""));
                            UA.CanEdit  = true;
                        }
                        else if (AP.Contains("Delete"))
                        {
                            UA.ScreenID  = int.Parse(AP.Replace("Delete", ""));
                            UA.CanDelete = true;
                        }
                        else
                        {
                            UA.ScreenID = int.Parse(AP);
                        }
                        LOUA.Add(UA);
                    }
                }
                else
                {
                    if (AP.Contains("Opt1"))
                    {
                        UserAccess UA = new UserAccess();
                        UA.ScreenID = int.Parse(AP.Replace("Opt1", ""));
                        UA.Opt1     = true;
                        LOUA.Add(UA);
                    }
                    else if (AP.Contains("Show"))
                    {
                        UserAccess UA = new UserAccess();
                        UA.ScreenID = int.Parse(AP.Replace("Show", ""));
                        foreach (string item in Access.Where(p => p.Contains(UA.ScreenID.ToString())).ToList())
                        {
                            if (item.Contains("Opt1"))
                            {
                                UA.Opt1 = true;
                            }
                            if (item.Contains("Edit"))
                            {
                                UA.CanEdit = true;
                            }
                            if (item.Contains("Delete"))
                            {
                                UA.CanDelete = true;
                            }
                        }
                        LOUA.Add(UA);
                    }
                    else
                    {
                        UserAccess UA = new UserAccess();
                        if (AP.Contains("Edit"))
                        {
                            UA.ScreenID = int.Parse(AP.Replace("Edit", ""));
                            UA.CanEdit  = true;
                        }
                        else if (AP.Contains("Delete"))
                        {
                            UA.ScreenID  = int.Parse(AP.Replace("Delete", ""));
                            UA.CanDelete = true;
                        }
                        else
                        {
                            UA.ScreenID = int.Parse(AP);
                        }
                        LOUA.Add(UA);
                    }
                }
            }
            Returner R = A.CreateAccount(LOUA);

            if (R.Message == Message.Username_Already_Exists)
            {
                return("false");
            }
            else
            {
                return("true");
            }
        }
示例#10
0
        private T General <T>(GeneralFunction beforeOperation, Returner <T> returner)
        {
            int startingRow = Console.CursorTop;

            var  grid        = new GameGrid(50, 25);
            int  outputs     = 0;
            bool gameStarted = false;

            var currentLocation = new Location2D();
            int score           = 0;

            var currentBallLocation   = new Location2D();
            var currentBallVelocity   = new Location2D(1);
            var currentPaddleLocation = new Location2D();

            var computer = new IntcodeComputer(FileContents);

            computer.InputRequested += InputRequested;
            computer.OutputWritten  += OutputWritten;

            beforeOperation(computer);

            computer.RunToHalt();

            return(returner(grid.ValueCounters, score));

            BigInteger InputRequested()
            {
                gameStarted = true;
                PrintGrid();
                var newBallLocation = currentBallLocation + currentBallVelocity;

                return(currentBallLocation.X.CompareTo(currentPaddleLocation.X));
            }

            void OutputWritten(BigInteger output)
            {
                int intput = (int)output;

                switch (outputs % 3)
                {
                case 0:
                    currentLocation.X = intput;
                    break;

                case 1:
                    currentLocation.Y = intput;
                    break;

                case 2:
                    if (currentLocation == (-1, 0))
                    {
                        score = intput;
                    }
                    else
                    {
                        var(x, y) = currentLocation;
                        switch (grid[x, y] = (TileType)intput)
                        {
                        case TileType.Ball:
                            if (gameStarted)
                            {
                                currentBallVelocity = currentLocation - currentBallLocation;
                            }
                            currentBallLocation = currentLocation;
                            break;

                        case TileType.HorizontalPaddle:
                            currentPaddleLocation = currentLocation;
                            break;
                        }
                    }
                    break;
                }
                outputs++;
            }