Ejemplo n.º 1
0
Archivo: Day2.cs Proyecto: stefc/AoC
        public void TestPut()
        {
            var prg    = ProgramAlarm.CreateProgram(1, 2, 3, 4, 5);
            var newPrg = from a in Some(prg) from b in Some(a.Write(4, 87)) select b;

            Assert.Equal(87, newPrg.GetOrElse(prg).Program.Last().Value);
        }
Ejemplo n.º 2
0
Archivo: Day2.cs Proyecto: stefc/AoC
        public void PuzzleTwo()
        {
            string input = File.ReadAllText("tests/y2019/Day2.Input.txt");
            var    prg   = ProgramAlarm.CreateProgram(input.ToBigNumbers());

            var range = Enumerable.Range(0, 99);

            Func <int, int, Option <ProgramState> > patching = (noun, verb) =>
                                                               from a in Some(prg)
                                                               from b in Some(a.Write(1, noun))
                                                               from c in Some(b.Write(2, verb))
                                                               select c;

            var programs =
                from noun in range
                from verb in range
                select(
                    Program : patching(noun, verb).GetOrElse(prg),
                    Code : 100 *noun + verb);

            var machine = ProgramAlarm.CreateStateMaschine();
            var actual  = programs
                          .AsParallel()
                          .Single(
                x => machine.Run(x.Program).FirstOrDefault().Value == 19690720)
                          .Code;

            Assert.Equal(7195, actual);
        }
Ejemplo n.º 3
0
Archivo: Day7.cs Proyecto: stefc/AoC
        public void Chain(long expected, long sequence, string program)
        {
            var prg      = ProgramAlarm.CreateProgram(program.ToBigNumbers());
            var computer = ProgramAlarm.CreateStateMaschine();
            var actual   = computer.Compute(prg, sequence, 10000, 0L);

            Assert.Equal(expected, actual);
        }
        public void TestPart2()
        {
            var target = 19690720;

            var output = ProgramAlarm.SolvePart2(target);

            Assert.AreEqual(3892, output);
        }
Ejemplo n.º 5
0
Archivo: Day7.cs Proyecto: stefc/AoC
        //   [InlineData(18216, 97856,
        //   "3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10")]
        public void Feedback(long expected, int sequence, string program)
        {
            var prg      = ProgramAlarm.CreateProgram(program.ToBigNumbers());
            var computer = ProgramAlarm.CreateStateMaschine();

            var states = AmplificationCircuit.SetUpStates(prg, sequence);

            Assert.Equal(expected, computer.ComputeLoop(states, 0));
        }
Ejemplo n.º 6
0
Archivo: Day2.cs Proyecto: stefc/AoC
        public void PartOne(string expected, string value)
        {
            var actual = ProgramAlarm
                         .CreateStateMaschine()
                         .Run(ProgramAlarm.CreateProgram(value.ToBigNumbers()))
                         .Select(i => i.Value.ToString());

            Assert.Equal(expected, string.Join(",", actual));
        }
Ejemplo n.º 7
0
Archivo: Day5.cs Proyecto: stefc/AoC
        public void PuzzleOne()
        {
            string input = File.ReadAllText("tests/y2019/Day5.Input.txt");
            var    prg   = ProgramAlarm.CreateProgram(input.ToBigNumbers(), 1);


            var computer = ProgramAlarm.CreateStateMaschine();

            Assert.Equal(14155342, computer(prg).State.Match(
                             () => 0, s => s.Output.Peek()));
        }
Ejemplo n.º 8
0
Archivo: Day5.cs Proyecto: stefc/AoC
        public void ImmediateMode(string expected, string value)
        {
            var computer =
                from v in ProgramAlarm.CreateStateMaschine()
                select string.Join(",", (from r in v select r.Value.ToString()));

            var actual = computer.Run(
                ProgramAlarm.CreateProgram(value.ToBigNumbers()));

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 9
0
        public void PuzzleTwo()
        {
            string input    = File.ReadAllText("tests/y2019/Day9.Input.txt");
            var    prg      = ProgramAlarm.CreateProgram(input.ToBigNumbers(), 2);
            var    computer = ProgramAlarm
                              .CreateStateMaschine();

            var result = computer(prg);

            Assert.Equal(70634,
                         result.State.Match(() => 0, s => s.Output.Peek()));
        }
Ejemplo n.º 10
0
Archivo: Day7.cs Proyecto: stefc/AoC
        public void PuzzleOne()
        {
            string input    = File.ReadAllText("tests/y2019/Day7.Input.txt");
            var    prg      = ProgramAlarm.CreateProgram(input.ToBigNumbers());
            var    computer = ProgramAlarm.CreateStateMaschine();
            var    actual   = AmplificationCircuit.Permutate(5)
                              .AsParallel()
                              .Select(sequence => computer.Compute(prg, sequence, 10000, 0))
                              .Max();

            Assert.Equal(38500, actual);
        }
Ejemplo n.º 11
0
Archivo: Day5.cs Proyecto: stefc/AoC
        public void Jump(int input, int output, string value)
        {
            var prg      = ProgramAlarm.CreateProgram(value.ToBigNumbers(), input);
            var computer = ProgramAlarm
                           .CreateStateMaschine();

            var result = computer(prg);
            var actual = result.State.Match(
                () => - 99, s => s.Output.Peek());

            Assert.Equal(output, actual);
        }
Ejemplo n.º 12
0
Archivo: Day5.cs Proyecto: stefc/AoC
        public void InputOpCode(long input, string expected, string value)
        {
            var prg      = ProgramAlarm.CreateProgram(value.ToBigNumbers(), input);
            var computer = ProgramAlarm
                           .CreateStateMaschine();

            var result = computer(prg);

            var actual = string.Join(",",
                                     result.Value.Select(i => i.Value.ToString()));

            Assert.Equal(expected, actual);

            Assert.Equal(input, result.State.Match(
                             () => 0, s => s.Output.Peek()));
        }
Ejemplo n.º 13
0
        public void CopyProgram()
        {
            var input = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99";
            var prg   = ProgramAlarm.CreateProgram(
                input.ToBigNumbers());
            var computer = ProgramAlarm
                           .CreateStateMaschine();

            var result = computer(prg);

            var actual =
                result.State.Match(
                    () => string.Empty, s => string.Join(",", s.Output.Reverse().Select(i => i.ToString())));

            Assert.Equal(input, actual);
        }
Ejemplo n.º 14
0
Archivo: Day7.cs Proyecto: stefc/AoC
        public void PuzzleTwo()
        {
            string input        = File.ReadAllText("tests/y2019/Day7.Input.txt");
            var    prg          = ProgramAlarm.CreateProgram(input.ToBigNumbers());
            var    computer     = ProgramAlarm.CreateStateMaschine();
            var    permutations = AmplificationCircuit
                                  .Permutate(56789.ToDigits()).ToList();

            var actual = permutations
                         .AsParallel()
                         .Select(sequence => AmplificationCircuit.SetUpStates(prg, sequence))
                         .Select(states => computer.ComputeLoop(states, 0))
                         .Max();

            Assert.Equal(33660560, actual);
        }
Ejemplo n.º 15
0
        public void PuzzleOne()
        {
            string input    = File.ReadAllText("tests/y2019/Day13.Input.txt");
            var    prg      = ProgramAlarm.CreateProgram(input.ToBigNumbers());
            var    computer = ProgramAlarm
                              .CreateStateMaschine();

            var seq = computer(prg).State.Match(
                () => Enumerable.Empty <int>(),
                x => x.Output.Reverse().Select(Convert.ToInt32));

            var actual = seq.ToTiles();


            var count = actual.Count(x => x.TileId == TileId.Block);

            Assert.Equal(213, count);
        }
Ejemplo n.º 16
0
Archivo: Day2.cs Proyecto: stefc/AoC
        public void PuzzleOne()
        {
            string input = File.ReadAllText("tests/y2019/Day2.Input.txt");
            var    prg   = ProgramAlarm.CreateProgram(input.ToBigNumbers());

            Func <int, int, Option <ProgramState> > patching = (noun, verb) =>
                                                               from a in Some(prg)
                                                               from b in Some(a.Write(1, noun))
                                                               from c in Some(b.Write(2, verb))
                                                               select c;


            var actual = ProgramAlarm.CreateStateMaschine()
                         .Run(patching(12, 2).GetOrElse(prg))
                         .FirstOrDefault();

            Assert.Equal(3760627, actual.Value);
        }
        public void TestRun(int[] program, int[] expectedProgram)
        {
            var outputProgram = ProgramAlarm.Run(program);

            Assert.AreEqual(expectedProgram, outputProgram);
        }
Ejemplo n.º 18
0
        public void Playing()
        {
            string input    = File.ReadAllText("tests/y2019/Day13.Input.txt");
            var    prg      = ProgramAlarm.CreateProgram(input.ToBigNumbers()).Write(0, 2);
            var    computer = ProgramAlarm
                              .CreateStateMaschine();

            prg = Enumerable.Range(0, 36)
                  .Aggregate(prg, (accu, idx) => accu.Write(639 + (36 * 23) + idx, 1));

            prg = computer(prg).State.GetOrElse(prg);


            var total = Enumerable.Range(1719, 1).Select(idx => prg.Program[idx]).Sum();
            // Assert.Equal(11441, total);


            var gameState = prg.Output
                            .Reverse()
                            .Select(Convert.ToInt32)
                            .ToTilesOrScore();

            var max = gameState.Aggregate(code.common.Point.Zero,
                                          (accu, current) => current.Match(
                                              l => new code.common.Point(
                                                  Math.Max(accu.X, l.Position.X),
                                                  Math.Max(accu.Y, l.Position.Y)),
                                              r => accu)
                                          );

            int w = (max.X + 1) * 10;
            int h = (max.Y + 2) * 10;



            var queue = new long[]
            { 0, 0, 0, 0, 0, 0, 0, 0, 0,
              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              +1, +1, +1, }
            .Aggregate(
                ImmutableQueue <long> .Empty,
                (accu, current) => accu.Enqueue(current));
            // prg = prg.WithInput(queueAsImmutableStack());

            var imgFactory = new ImageFactory(w, h);

            var frame = 0;

            var rnd = new Random();

            var scores = ImmutableList <int> .Empty;

            while (frame < 5000)
            {
                scores = imgFactory.AddFrame(gameState, frame, scores);

                if (prg.OpCode == OpCode.Exit)
                {
                    break;
                }

                queue = queue.Dequeue(out var control);
                prg   = computer(prg
                                 .WithInput(control)
                                 .WithOutput(ImmutableStack <long> .Empty))
                        .State.GetOrElse(prg);

                gameState = prg.Output
                            .Reverse()
                            .Select(Convert.ToInt32)
                            .ToTilesOrScore();
                frame += 1;

                if (queue.IsEmpty)
                {
                    queue = queue.Enqueue(rnd.Next(3) - 1);
                }
            }

            imgFactory.SaveGif("Test.gif");
        }
        public void TestPart1()
        {
            var output = ProgramAlarm.SolvePart1(12, 2);

            Assert.AreEqual(7210630, output);
        }