Example #1
0
        static void ConvertFormat(BitGrid grid, Options option)
        {
            switch (option.newFormat)
            {
            case Export.Format.Binary:
                Export.WriteToFile(option.outputFileName, grid, option.newFormat);
                break;

            case Export.Format.Csv:
                Export.WriteToFile(option.outputFileName, grid, option.newFormat);
                break;

            case Export.Format.Info:
                Export.WriteToFile(option.outputFileName, grid, option.newFormat);
                break;

            case Export.Format.Json:
                Export.WriteToFile(option.outputFileName, grid, option.newFormat);
                break;

            case Export.Format.Txt:
                Export.WriteToFile(option.outputFileName, grid, option.newFormat);
                break;

            case Export.Format.Yaml:
                Export.WriteToFile(option.outputFileName, grid, option.newFormat);
                break;
            }
        }
Example #2
0
        private static void ChangeData(BitGrid bitGrid, Coords from, Coords to, ChangeMethod changeMethod)
        {
            for (ushort x = from.X; x <= to.X; x++)
            {
                for (ushort y = from.Y; y <= to.Y; y++)
                {
                    switch (changeMethod)
                    {
                    case ChangeMethod.TurnOn:
                        bitGrid[x, y] = true;
                        break;

                    case ChangeMethod.TurnOff:
                        bitGrid[x, y] = false;
                        break;

                    case ChangeMethod.Toggle:
                        bitGrid[x, y] = !bitGrid[x, y];
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(changeMethod), changeMethod, null);
                    }
                }
            }
        }
Example #3
0
 private static void ForceCornersToOnState(BitGrid bitGrid)
 {
     bitGrid[0, 0] = true;
     bitGrid[0, bitGrid.Height - 1] = true;
     bitGrid[bitGrid.Width - 1, 0]  = true;
     bitGrid[bitGrid.Width - 1, bitGrid.Height - 1] = true;
 }
Example #4
0
        public static BitGrid Generate(Random rand, int width, int height)
        {
            BitGrid bitGrid = new BitGrid(width, height);
            // fill sets:
            List <List <Tree> > sets  = new List <List <Tree> >();
            List <int[]>        edges = new List <int[]>();

            for (int y = 0; y < height; y++)
            {
                sets.Add(new List <Tree>());
                for (int x = 0; x < width; x++)
                {
                    if (y > 0)
                    {
                        edges.Add(new [] { x, y, (int)Directions.N });
                    }
                    if (x > 0)
                    {
                        edges.Add(new[] { x, y, (int)Directions.W });
                    }
                    sets[y].Add(new Tree());
                }
            }
            // create edges
            Generator(ref bitGrid, ref sets, ref edges, ref rand);

            return(bitGrid);
        }
Example #5
0
        private static BitGrid MutateBitGrid2(BitGrid bitGrid)
        {
            var newBitGrid = new BitGrid(bitGrid);

            for (int x = 0; x < bitGrid.Width; x++)
            {
                for (int y = 0; y < bitGrid.Height; y++)
                {
                    var neighborsLit = EnumerateNeighbors(x, y, bitGrid.Width, bitGrid.Height).Select(a => bitGrid[a.X, a.Y]).Count(b => b);

                    if (newBitGrid[x, y] && !(neighborsLit == 2 || neighborsLit == 3))
                    {
                        newBitGrid[x, y] = false;
                        continue;
                    }

                    if (!newBitGrid[x, y] && neighborsLit == 3)
                    {
                        newBitGrid[x, y] = true;
                    }
                }
            }

            return(newBitGrid);
        }
Example #6
0
 private static void MarkFrontiers(int x, int y, ref BitGrid grid, ref List <Pos> frontiers)
 {
     grid[y][x] |= In;
     CreateFrontier(x - 1, y, ref grid, ref frontiers);
     CreateFrontier(x + 1, y, ref grid, ref frontiers);
     CreateFrontier(x, y - 1, ref grid, ref frontiers);
     CreateFrontier(x, y + 1, ref grid, ref frontiers);
 }
Example #7
0
 public void Gen()
 {
     Grid = new BitGrid();
     Grid.Init(Width, Height, Scale);
     Grid.MeteoriteFill(Seed, MeteoriteRadius, MeteoriteCount);
     Finder = new PathFind(Grid);
     TestPath();
 }
Example #8
0
 private static void CreateFrontier(int posX, int posY, ref BitGrid grid, ref List <Pos> frontiers)
 {
     if (posX >= 0 && posY >= 0 && posY < grid.Height && posX < grid.Width && grid[posY][posX] == 0)
     {
         grid[posY][posX] |= Frontier;
         frontiers.Add(new Pos(posX, posY));
     }
 }
Example #9
0
        public static void WriteToFile(string file, BitGrid grid, Format format, bool appendFlag = false)
        {
            if ((format & Format.Info) != 0)
            {
                //to bites and ascii
            }
            if ((format & Format.Json) != 0)
            {
                var filename = file + ".json";
                WriteToFile(filename, appendFlag, (Stream stream) =>
                {
                    Write(stream, Converter.ToJSON(grid));
                });
            }

            if ((format & Format.Txt) != 0)
            {
                var filename = file + ".txt";
                WriteToFile(filename, appendFlag, (Stream stream) =>
                {
                    Write(stream, Converter.ToString(grid, false));
                });
            }
            if ((format & Format.Csv) != 0)
            {
                var filename = file + ".csv";
                WriteToFile(filename, appendFlag, (Stream stream) =>
                {
                    Write(stream, Converter.ToString(grid));
                });
            }
            if ((format & Format.Yaml) != 0)
            {
                var filename = file + ".yaml";
                WriteToFile(filename, appendFlag, (Stream stream) =>
                {
                    Write(stream, Converter.ToYAML(grid));
                });
            }
            if ((format & Format.Binary) != 0)
            {
                var filename = file + ".bin";
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                using (BinaryWriter writer = new BinaryWriter(File.Open(filename, FileMode.Create)))
                {
                    var byteGrid = Converter.ToBytes(grid);
                    writer.Write(grid.Width);
                    writer.Write(grid.Height);
                    foreach (var row in byteGrid)
                    {
                        writer.Write(row);
                    }
                }
            }
        }
Example #10
0
 private static BitGrid CreateVerticalConnection(BitGrid bitGrid, int row, int index)
 {
     bitGrid[row][index] |= S;
     if (row + 1 < bitGrid.Height)
     {
         bitGrid[row + 1][index] |= N;
     }
     return(bitGrid);
 }
        ////////////////////////////////////////////////////////////////////////////////////
        ////////// BitGrid To Converters                                          //////////
        ////////////////////////////////////////////////////////////////////////////////////
        static public byte[][] ToBytes(BitGrid bitgrid)
        {
            var           grid  = bitgrid.Grid;
            List <byte[]> bytes = new List <byte[]>();

            foreach (var row in grid)
            {
                bytes.Add(row.SelectMany(i => BitConverter.GetBytes(i)).ToArray());
            }
            return(bytes.ToArray());
        }
        public static string ToString(BitGrid bitgrid, bool newLine = true)
        {
            var    grid = bitgrid.Grid;
            string str  = "";

            foreach (var row in grid)
            {
                str += String.Join(";", row.Select(x => x.ToString()).ToArray());
                str += (newLine) ? "\n" : "|";
            }
            return(str);
        }
Example #13
0
        public static void WriteToConsole(BitGrid grid, DisplayFormat format)
        {
            if ((format & DisplayFormat.ASCII) != 0)
            {
                WriteLine(Console.OpenStandardOutput(), Converter.ToASCII(grid));
            }

            if ((format & DisplayFormat.Bits) != 0)
            {
                WriteLine(Console.OpenStandardOutput(), Converter.ToBits(grid));
            }
        }
Example #14
0
        private static int CalculateAnswer1(bool[] input)
        {
            var bitGrid = new BitGrid(100, 100, input);

            for (int step = 0; step < 100; step++)
            {
                bitGrid = MutateBitGrid1(bitGrid);
            }

            var answer = bitGrid.GetLightsOnCount();

            return(answer);
        }
Example #15
0
        public static CellGrid ToCellGrid(BitGrid bitGrid)
        {
            CellGrid grid = new CellGrid(bitGrid.Width, bitGrid.Height);

            for (var row = 0; row < grid.Height; row++)
            {
                for (var cell = 0; cell < grid.Width; cell++)
                {
                    var gridCell = bitGrid[row][cell];
                    grid[row][cell] |= gridCell;
                }
            }
            return(grid);
        }
Example #16
0
        public static BitGrid Generate(Random rand, int width, int height)
        {
            BitGrid bitGrid = new BitGrid(width, height);

            //var row = 0;
            for (var row = 0; row < height; row++)
            {
                var sets = CreateNewSets(width);
                if (row == bitGrid.Height - 1 || row == 0)
                {
                    for (var idx = 0; idx < width; idx++)
                    {
                        bitGrid[row][idx] |= (idx < width - 1) ? E : W;
                    }
                }
                else
                {
                    //join them:
                    for (var idx = 0; idx < width; idx++)
                    {
                        if (rand.Next(0, 2) > 0 && (idx + 1) < sets.Count)
                        {
                            if (!sets[idx].Overlaps(sets[idx + 1]))
                            {
                                sets[idx].UnionWith(sets[idx + 1]);
                                sets.RemoveAt(idx + 1);
                            }
                        }
                    }

                    foreach (var set in sets)
                    {
                        if (set.Count != 1)
                        {
                            foreach (var index in set)
                            {
                                bitGrid[row][index] |= (index != set.Last()) ? E : W;
                            }
                            bitGrid = CreateVerticalConnection(bitGrid, row, set.ElementAt(rand.Next(0, set.Count - 1)));
                        }
                        else
                        {
                            bitGrid = CreateVerticalConnection(bitGrid, row, set.Last());
                        }
                    }
                }
            }
            return(bitGrid);
        }
Example #17
0
    public static void DrawGrid(BitGrid grid)
    {
        Vector3 size = new Vector3(grid.Scale, 0.2f, grid.Scale);

        for (int i = 0; i < grid.Width; ++i)
        {
            for (int j = 0; j < grid.Height; ++j)
            {
                if (grid.Get(i, j))
                {
                    Gizmos.DrawCube(new Vector3(grid.Scale * (i + 0.5f), 0.2f, grid.Scale * (j + 0.5f)), size);
                }
            }
        }
    }
        public static string ToYAML(BitGrid grid)
        {
            string export = "---\n";

            foreach (var row in grid.Grid)
            {
                export += "- ";
                var newRow = true;
                foreach (var cell in row)
                {
                    export += ((newRow) ? "" : "\t") + "- " + cell + "\n";
                    newRow  = false;
                }
            }
            return(export);
        }
Example #19
0
        public static BitGrid ToBitGrid(CellGrid cellGrid)
        {
            BitGrid grid = new BitGrid(cellGrid.Width, cellGrid.Height);

            for (var row = 0; row < grid.Height; row++)
            {
                for (var cell = 0; cell < grid.Width; cell++)
                {
                    var gridCell = cellGrid[row][cell];
                    grid[row][cell] |= (!gridCell.North)? (int)Directions.N:0;
                    grid[row][cell] |= (!gridCell.East)? (int)Directions.E:0;
                    grid[row][cell] |= (!gridCell.South)? (int)Directions.S:0;
                    grid[row][cell] |= (!gridCell.West)? (int)Directions.W:0;
                }
            }
            return(grid);
        }
Example #20
0
        ////////////////////////////////////////////////////////////////////////////////////
        ////////// None Parsebale Converters                                      //////////
        ////////////////////////////////////////////////////////////////////////////////////

        public static string ToASCII(BitGrid grid)
        {
            string output = "\n";
            var    width  = grid.Width;
            var    height = grid.Height;

            for (int y = 0; y < width; y++)
            {
                if (y == 0)
                {
                    output += CreateLine(y, width) + "\n";
                }
                string line = "#";
                for (int x = 0; x < width; x++)
                {
                    var cell              = "|_|";
                    var isEmpty           = grid[y][x] == 0;
                    var hasNoWallNorth    = (grid[y][x] & (int)Directions.N) != 0;
                    var hasNoWallEast     = (grid[y][x] & (int)Directions.E) != 0;
                    var hasNoWallSouth    = (grid[y][x] & (int)Directions.S) != 0;
                    var hasNoWallWest     = (grid[y][x] & (int)Directions.W) != 0;
                    var privX             = x - 1;
                    var privHasNoWallEast = ((privX >= 0) ? (grid[y][privX] & (int)Directions.E) != 0 : false);
                    //cell: |_|
                    cell = CarveWay(hasNoWallWest, 0, '.', cell);
                    if (x < width)
                    {
                        cell = CarveWay(hasNoWallEast, 2, '.', cell);
                    }
                    if (hasNoWallEast)
                    {
                        cell = CarveWay(privHasNoWallEast, 0, '.', cell);
                    }
                    cell  = CarveWay(hasNoWallSouth, 1, ' ', cell);
                    line += cell;
                }
                line   += "#";
                output += line + "\n";
                if (y == height)
                {
                    output += CreateLine(y, height) + "\n";
                }
            }
            return(output);
        }
Example #21
0
        public static string ToBits(BitGrid grid)
        {
            int    width  = grid.Width;
            int    height = grid.Height;
            string output = "\n";

            for (int y = 0; y < height; y++)
            {
                string line = "| ";
                for (int x = 0; x < width; x++)
                {
                    line += ((grid[y][x] < 10) ? " " : "") + grid[y][x] + " | ";
                }
                output += line + "\n";
            }
            output += '\n';
            return(output);
        }
Example #22
0
        /// <summary>
        /// Plots and draws the sealing slabs.
        /// </summary>
        /// <param name="plotter">The plotter.</param>
        /// <param name="drawer">The drawer.</param>
        /// <param name="calculation">The calculation.</param>
        /// <param name="iteration">The iteration.</param>
        /// <param name="freeToTotalPixelRatioPercent">The free to total pixel ratio percent.</param>
        /// <param name="sealingSlabs">The sealing slabs.</param>
        private void PlotAndDraw(IPlotter plotter, BitBresenhamDrawer drawer, Calculation calculation, int iteration, out double freeToTotalPixelRatioPercent, out List <SealingSlab> sealingSlabs)
        {
            // Instantiate for each loop instead of initially, otherwise the memory will be exhausted.
            var grid = new BitGrid(calculation.WidthPixels, calculation.HeightPixels);

            sealingSlabs = plotter.PlotSealingSlabs <SealingSlab>().ToList();

            // Only save sealing slabs that were drawn.
            sealingSlabs = calculationService.DrawSealingSlabs(drawer, grid, sealingSlabs);

            foreach (var sealingSlab in sealingSlabs)
            {
                sealingSlab.CalculationId = calculation.Id;
                sealingSlab.Iteration     = iteration + 1;
            }

            // Save in percent.
            freeToTotalPixelRatioPercent = grid.FreeToTotalPixelRatioPercent;
        }
Example #23
0
 private static void Generator(ref BitGrid bitGrid, ref List <List <Tree> > sets, ref List <int[]> edges, ref Random rand)
 {
     edges.Shuffle(ref rand);
     while (edges.Count != 0)
     {
         var edge      = edges.Pop();
         var x         = edge[0];
         var y         = edge[1];
         var direction = (Directions)edge[2];
         var nx        = x + directions[direction][0];
         var ny        = y + directions[direction][1];
         var set1      = sets[y][x];
         var set2      = sets[ny][nx];
         if (!set1.IsConnected(set2))
         {
             set1.Connect(set2);
             bitGrid[y][x]   |= (int)direction;
             bitGrid[ny][nx] |= (int)oppositeDirections[direction];
         }
     }
 }
        private static ref BitGrid Generator(int x, int y, ref BitGrid bitGrid, ref Random rand)
        {
            Directions[] directions_ = new Directions[] { Directions.E, Directions.N, Directions.S, Directions.W };
            directions_.Shuffle(ref rand);
            foreach (var direction in directions_)
            {
                var nx = x + directions[direction][0];
                var ny = y + directions[direction][1];
                if (
                    ny >= 0 && ny <= bitGrid.Height - 1 &&
                    nx >= 0 && nx <= bitGrid[ny].Size() &&
                    bitGrid[ny][nx] == 0
                    )
                {
                    bitGrid[y][x]   |= (int)direction;
                    bitGrid[ny][nx] |= (int)oppositeDirections[direction];
                    Generator(nx, ny, ref bitGrid, ref rand);
                }
            }

            return(ref bitGrid);
        }
Example #25
0
        private static List <Pos> GetNeighbors(int x, int y, BitGrid grid)
        {
            var neighours = new List <Pos>();

            if (x > 0 && (grid[y][x - 1] & In) != 0)
            {
                neighours.Add(new Pos(x - 1, y));
            }
            if (x + 1 < grid.Width && (grid[y][x + 1] & In) != 0)
            {
                neighours.Add(new Pos(x + 1, y));
            }
            if (y > 0 && (grid[y - 1][x] & In) != 0)
            {
                neighours.Add(new Pos(x, y - 1));
            }
            if (y + 1 < grid.Height && (grid[y + 1][x] & In) != 0)
            {
                neighours.Add(new Pos(x, y + 1));
            }
            return(neighours);
        }
Example #26
0
        public static BitGrid Generate(Random rand, int width, int height)
        {
            var grid      = new BitGrid(width, height);
            var frontiers = new List <Pos>();
            var beginX    = rand.Next(0, width - 1);
            var beginY    = rand.Next(0, height - 1);

            //start algorithm
            MarkFrontiers(beginX, beginY, ref grid, ref frontiers);
            while (frontiers.Count != 0)
            {
                var frontier = frontiers[rand.Next(0, frontiers.Count - 1)];
                frontiers.Remove(frontier);
                var neighours      = GetNeighbors(frontier.X, frontier.Y, grid);
                var randomNeighour = neighours[rand.Next(0, neighours.Count - 1)];
                var direction      = GetDirection(frontier.X, frontier.Y, randomNeighour.X, randomNeighour.Y);
                grid[frontier.Y][frontier.X]             |= (int)direction;
                grid[randomNeighour.Y][randomNeighour.X] |= (int)oppositeDirections[direction];
                MarkFrontiers(frontier.X, frontier.Y, ref grid, ref frontiers);
            }

            return(grid);
        }
Example #27
0
        static void Main(string[] args)
        {
            PrintHeader("Day 06");

            var input = File.ReadAllText("Input.txt");

            var commands = LightingGrammar.Program.Parse(input);

            var bitGrid = new BitGrid(1000, 1000);
            var intGrid = new IntGrid(1000, 1000);

            foreach (var changeCommand in commands)
            {
                ChangeData(bitGrid, changeCommand.CoordsFrom, changeCommand.CoordsTo, changeCommand.ChangeMethod);
                ChangeData(bitGrid, changeCommand.CoordsFrom, changeCommand.CoordsTo, changeCommand.ChangeMethod);
            }

            var answer1 = bitGrid.GetLightsOnCount();
            var answer2 = intGrid.GetLightsTotalBrightness();


            PrintAnswer("Answer 1", answer1);
            PrintAnswer("Answer 2", answer2);
        }
        public static BitGrid Generate(Random rand, int width, int height)
        {
            BitGrid bitGrid = new BitGrid(width, height);

            return(Generator(0, 0, ref bitGrid, ref rand));
        }
Example #29
0
 public static CellGrid Convert(BitGrid grid)
 {
     return(ToCellGrid(grid));
 }
Example #30
0
 public PathFind(BitGrid grid)
 {
     Grid = grid;
 }