Пример #1
0
        public override string SolvePart1()
        {
            LightGrid grid = new LightGrid(Input);

            grid.Evolve(100);
            return($"{grid.LightsOn()}");
        }
Пример #2
0
        /// <inheritdoc />
        protected override int SolveCore(string[] args)
        {
            int version = -1;

            switch (args[0])
            {
                case "1":
                    version = 1;
                    break;

                case "2":
                    version = 2;
                    break;

                default:
                    break;
            }

            if (version == -1)
            {
                Console.WriteLine("The instruction set specified is invalid.");
                return -1;
            }

            var instructions = new List<Instruction>();

            foreach (string line in ReadResourceAsLines())
            {
                if (version == 1)
                {
                    instructions.Add(InstructionV1.Parse(line));
                }
                else
                {
                    instructions.Add(InstructionV2.Parse(line));
                }
            }

            Console.WriteLine("Processing instructions using set {0}...", version);

            var grid = new LightGrid(1000, 1000);

            foreach (var instruction in instructions)
            {
                instruction.Act(grid);
            }

            if (version == 1)
            {
                LightsIlluminated = grid.Count;
                Console.WriteLine("{0:N0} lights are illuminated.", LightsIlluminated);
            }
            else
            {
                TotalBrightness = grid.Brightness;
                Console.WriteLine("The total brightness of the grid is {0:N0}.", TotalBrightness);
            }

            return 0;
        }
Пример #3
0
        public void LightGridProcessLightSwitchTest__22()
        {
            LightGrid lightGrid = new LightGrid();

            lightGrid.ProcessLightSwitch(2, 2);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[0, 0]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[1, 0]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[2, 0]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[3, 0]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[4, 0]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[0, 1]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[1, 1]);
            Assert.AreEqual(true, lightGrid.LightsOnGrid[2, 1]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[3, 1]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[4, 1]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[0, 2]);
            Assert.AreEqual(true, lightGrid.LightsOnGrid[1, 2]);
            Assert.AreEqual(true, lightGrid.LightsOnGrid[2, 2]);
            Assert.AreEqual(true, lightGrid.LightsOnGrid[3, 2]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[4, 2]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[0, 3]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[1, 3]);
            Assert.AreEqual(true, lightGrid.LightsOnGrid[2, 3]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[3, 3]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[4, 3]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[0, 4]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[1, 4]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[2, 4]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[3, 4]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[4, 4]);
            Assert.AreEqual(5, lightGrid.LightsOnCount);

            lightGrid.ProcessLightSwitch(2, 2);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[0, 0]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[1, 0]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[2, 0]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[3, 0]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[4, 0]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[0, 1]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[1, 1]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[2, 1]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[3, 1]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[4, 1]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[0, 2]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[1, 2]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[2, 2]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[3, 2]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[4, 2]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[0, 3]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[1, 3]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[2, 3]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[3, 3]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[4, 3]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[0, 4]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[1, 4]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[2, 4]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[3, 4]);
            Assert.AreEqual(false, lightGrid.LightsOnGrid[4, 4]);
            Assert.AreEqual(0, lightGrid.LightsOnCount);
        }
Пример #4
0
        public void Puzzle1_FindLowestArea()
        {
            // all thanks to https://www.reddit.com/r/adventofcode/comments/a4skra/2018_day_10_solutions/ebhafz0/

            var lights = new LightGrid(Input.Day10Parse(Input.Day10));

            var iterations = Enumerable.Range(0, 20_000).ToList();

            var iterationAreaMap = iterations.ToDictionary(iteration => iteration,
                                                           iteration =>
            {
                var positions = lights.Lights.Select(light => light.PositionAfter(iteration)).ToList();

                var minX = positions.Min(x => x.X);
                var maxX = positions.Max(x => x.X);
                var minY = positions.Min(x => x.Y);
                var maxY = positions.Max(x => x.Y);

                return(maxX - minY + maxY - minY);
            });

            var lowestArea = iterationAreaMap.OrderBy(x => x.Value).First();

            Output.WriteLine($"{lowestArea.Key}: {lowestArea.Value}");
        }
Пример #5
0
        public override int SolvePart1()
        {
            var optimizedGrid = new LightGrid();

            optimizedGrid.ApplyInstructions(instructions);
            return(optimizedGrid.GetTurnedOnLightCount());
        }
Пример #6
0
        public void LightGridConstructorTest()
        {
            LightGrid lightGrid = new LightGrid();

            Assert.AreEqual(5, lightGrid.Columns);
            Assert.AreEqual(5, lightGrid.Rows);
            Assert.AreEqual(0, lightGrid.LightsOnCount);
            Assert.AreEqual(lightGrid.LightsOnGrid.Cast <bool>().ToArray().Count(x => x.Equals(true)), 0);
        }
Пример #7
0
        public void CreateGridColumnsAndRowsAndTurnOnCenterCheckLightsOn(int columns, int rows, int x, int y, int expected)
        {
            var grid = new LightGrid(columns, rows);

            grid.ToggleLight(x, y);

            var lightsOn = grid.GetLights(LightState.On);

            Assert.Equal(expected, lightsOn.Count);
        }
Пример #8
0
        public void Puzzle1_After10345Iterations_PrintGrid()
        {
            var lights = new LightGrid(Input.Day10Parse(Input.Day10));

            lights.Tick(10345); // answer to puzzle 2

            Output.WriteLine($"{lights}");

            // prints out CPJRNKCF
        }
Пример #9
0
        public void LightGridInitialiseTest()
        {
            LightGrid lightGrid = new LightGrid();

            lightGrid.InitialiseLightGrid();

            Assert.AreNotEqual(0, lightGrid.LightsOnCount);
            Assert.AreNotEqual(0, lightGrid.LightsOnGrid.Cast <bool>().ToArray().Count(x => x.Equals(true)));
            Assert.AreEqual(lightGrid.LightsOnGrid.Cast <bool>().ToArray().Count(x => x.Equals(true)), lightGrid.LightsOnCount);
        }
Пример #10
0
        public void Example1()
        {
            const string ruleString = "toggle 0,0 through 99,99";
            var          rule       = RuleInterpreter.Eval(ruleString);

            var newGrid = rule.ExecuteRule(LightGrid.Create(100, 100));

            var lightsLit = newGrid.GetLights();

            Assert.Equal(10000, lightsLit);
        }
Пример #11
0
        public void CreateGridColumnsAndRowsAndTurnOnCenter()
        {
            int x    = 2;
            int y    = 2;
            var grid = new LightGrid(5, 5);

            var centerLight = grid.GetLight(x, y);

            grid.ToggleLight(x, y);

            Assert.Equal(LightState.On, centerLight.State);
        }
Пример #12
0
        public static long Part1()
        {
            var lines     = FileReader.ReadInputLines(Day).ToList();
            var lightGrid = new LightGrid();

            foreach (var line in lines)
            {
                lightGrid.ApplyInstruction(line);
            }

            return(lightGrid.LightsOn());
        }
Пример #13
0
        protected override void LoadState()
        {
            var lines = FileLines;

            defaultGrid = new();
            for (int y = 0; y < defaultGrid.Height; y++)
            {
                for (int x = 0; x < defaultGrid.Width; x++)
                {
                    defaultGrid[x, y] = ParseState(lines[y][x]);
                }
            }
        }
Пример #14
0
        public void Puzzle2()
        {
            var input = FileReader
                        .GetResource("AdventOfCode.Tests._2015.Day6.PuzzleInput.txt");

            var lines     = input.Split(Environment.NewLine);
            var lightGrid = LightGrid.Create(1000, 1000);

            lightGrid = lines.Select(RuleInterpreter.Eval)
                        .Aggregate(lightGrid, (current, rule) => rule.ExecuteRule(current));

            var lightsLit = lightGrid.GetLightBrightness();

            Assert.Equal(13614336, lightsLit);
        }
Пример #15
0
        public void LitDiodsAre400410()
        {
            var show = new LightShow();
            using (var stream = CreateResource())
            {
                var textStream = InputReader.ReadResource(stream);
                var commands = show.CreateCommands(new Tokenizer().Tokenize(textStream));

                var grid = new LightGrid<LightDiod>(1000, 1000, () => new LightDiod(false));
                foreach (var command in commands)
                {
                    grid.Execute(command);
                }

                Assert.AreEqual(400410, grid.GetDiods().Count(x => x.Item2.On));
            }
        }
Пример #16
0
            public LightGrid RunStep(bool stuckLights)
            {
                var result = new LightGrid();

                for (int x = 0; x < Width; x++)
                {
                    for (int y = 0; y < Height; y++)
                    {
                        int neighbors = GetNeighborValues(x, y, LightState.On);
                        var value     = this[x, y];
                        result[x, y] = (value, neighbors) switch
                        {
                            (LightState.Off, 3) => LightState.On,
                            (LightState.On, 2 or 3) => LightState.On,
                            _ => LightState.Off,
                        };
                    }
                }
Пример #17
0
        public void BrightnessIs15343601()
        {
            var show = new LightShow();
            using (var stream = CreateResource())
            {
                var textStream = InputReader.ReadResource(stream);
                var commands = show.CreateCommands(new Tokenizer().Tokenize(textStream));

                var grid = new LightGrid<DimmedLightDiod>(1000, 1000, () => new DimmedLightDiod(0));

                foreach (var command in commands)
                {
                    grid.Execute(command);
                }

                Assert.AreEqual(15343601, grid.GetDiods().Sum(x => x.Item2.Brightness));
            }
        }
Пример #18
0
        public MainWindow()
        {
            _LightGrid = new LightGrid(Columns, Rows);
            _Buttons   = new Button[Rows, Columns];

            InitializeComponent();

            // Set the grid size so the lights rendering in the correct order
            grid.Columns = Columns;
            grid.Rows    = Rows;

            // Loops the lights and attach click handlers
            foreach (var light in _LightGrid.Lights)
            {
                var button = new Button()
                {
                    Background = Brushes.Gray
                };
                // Apply the event handler and add the light to the grid
                button.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) =>
                {
                    if (_LightGrid.Complete)
                    {
                        return;
                    }

                    _LightGrid.ToggleLight(light.X, light.Y);
                    RedrawGrid();

                    if (_LightGrid.Complete)
                    {
                        MessageBox.Show("You win");
                    }
                });
                _Buttons[light.Y, light.X] = button;
                grid.Children.Add(button);
            }

            // Reset and randomize the grid
            _LightGrid.ResetAndRandomizeStates();
            RedrawGrid();
        }
Пример #19
0
        /// <inheritdoc />
        public override void Act(LightGrid grid)
        {
            switch (Action)
            {
            case "OFF":
                grid.TurnOff(Bounds);
                break;

            case "ON":
                grid.TurnOn(Bounds);
                break;

            case "TOGGLE":
                grid.Toggle(Bounds);
                break;

            default:
                throw new PuzzleException($"The current instruction '{Action}' is invalid.");
            }
        }
Пример #20
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        GUILayout.Space(8);

        LightGrid lg = (LightGrid)target;

        //if (GUILayout.Button("Reconstruct"))
        //{
        //	lg.ReconstructGrid();
        //}
        if (GUILayout.Button("Bake"))
        {
            lg.Bake();
        }
        //if (GUILayout.Button("Bake 2"))
        //{
        //	lg.Bake2();
        //}
        if (GUILayout.Button("Bake All"))
        {
            lg.BakeAll(false);
        }
        if (GUILayout.Button("Transfer Settings"))
        {
            lg.SyncGlobals();
        }
        if (GUILayout.Button("Transfer & Bake All"))
        {
            lg.BakeAll();
        }
        //if (GUILayout.Button("Update Property Block"))
        //{
        //	lg.UpdatePropertyBlock();
        //}
    }
Пример #21
0
 protected override void ResetState()
 {
     defaultGrid = null;
 }
Пример #22
0
 /// <inheritdoc />
 public override void Act(LightGrid grid)
 {
     grid.IncrementBrightness(Bounds, Delta);
 }
Пример #23
0
            /// <inheritdoc />
            public override void Act(LightGrid grid)
            {
                switch (Action)
                {
                    case "OFF":
                        grid.TurnOff(Bounds);
                        break;

                    case "ON":
                        grid.TurnOn(Bounds);
                        break;

                    case "TOGGLE":
                        grid.Toggle(Bounds);
                        break;

                    default:
                        throw new InvalidOperationException("The current instruction is invalid.");
                }
            }
Пример #24
0
 /// <summary>
 /// Performs the instruction on the specified <see cref="LightGrid"/>.
 /// </summary>
 /// <param name="grid">The grid to perform the instruction on.</param>
 public abstract void Act(LightGrid grid);
Пример #25
0
 public void DefaultGridHasNoDiodsLit()
 {
     var grid = new LightGrid<LightDiod>(10, 10, () => new LightDiod(false));
     Assert.AreEqual(0, grid.GetDiods().Count(x => x.Item2.On));
 }
Пример #26
0
 public void ToggleEntireAreaTogglesAll()
 {
     var grid = new LightGrid<LightDiod>(10, 10, () => new LightDiod(false));
     grid.Execute(new LightCommand(new Rectangle<int>(new Point2D<int>(0,0), new Point2D<int>(9,9)),LightCommandType.Toggle ));
     Assert.AreEqual(100, grid.GetDiods().Count(x => x.Item2.On));
 }
Пример #27
0
        public void CreateGridColumnsAndRows(int columns, int rows, int expected)
        {
            var grid = new LightGrid(columns, rows);

            Assert.Equal(expected, grid.Lights.Length);
        }
Пример #28
0
 /// <inheritdoc />
 public override void Act(LightGrid grid)
 => grid.IncrementBrightness(Bounds, Delta);
Пример #29
0
    /// <inheritdoc />
    protected override async Task <PuzzleResult> SolveCoreAsync(string[] args, CancellationToken cancellationToken)
    {
        int version = args[0] switch
        {
            "1" => 1,
            "2" => 2,
            _ => - 1,
        };

        if (version == -1)
        {
            throw new PuzzleException("The specified instruction set is invalid.");
        }

        var lines = await ReadResourceAsLinesAsync();

        var instructions = new Instruction[lines.Count];

        Func <string, Instruction> parser = version == 1 ? InstructionV1.Parse : InstructionV2.Parse;

        for (int i = 0; i < lines.Count; i++)
        {
            instructions[i] = parser(lines[i]);
        }

        if (Verbose)
        {
            Logger.WriteLine("Processing instructions using set {0}...", version);
        }

        const int Length = 1_000;
        var       grid   = new LightGrid(Length, Length);

        foreach (Instruction instruction in instructions)
        {
            instruction.Act(grid);
        }

        if (version == 1)
        {
            LightsIlluminated = grid.Count;

            if (Verbose)
            {
                Logger.WriteLine("{0:N0} lights are illuminated.", LightsIlluminated);
            }

            return(PuzzleResult.Create(LightsIlluminated));
        }
        else
        {
            TotalBrightness = grid.Brightness;

            if (Verbose)
            {
                Logger.WriteLine("The total brightness of the grid is {0:N0}.", TotalBrightness);
            }

            return(PuzzleResult.Create(TotalBrightness));
        }
    }
Пример #30
0
 /// <summary>
 /// Performs the instruction on the specified <see cref="LightGrid"/>.
 /// </summary>
 /// <param name="grid">The grid to perform the instruction on.</param>
 public abstract void Act(LightGrid grid);