Example #1
0
    private static int Part2()
    {
        IntProgram program;
        int        count = Input.Count;
        int        target = 19690720;
        int        noun = 0, verb = 0;

        for (int i = 0; i < count; i++)
        {
            for (int j = 0; j < count; j++)
            {
                program = new IntProgram(Input, i, j);
                program.Run();

                if (program.GetResult() == target)
                {
                    noun = i;
                    verb = j;
                    break;
                }
            }

            if (noun != 0 && verb != 0)
            {
                break;
            }
        }

        return(100 * noun + verb);
    }
Example #2
0
        public static void Main(string[] args)
        {
            var prog   = IntProgram.ParseFrom("input.txt");
            var comp   = new Computer(prog, input: new long[0]);
            var camera = Camera.Process(comp.Run());

            Console.WriteLine(camera);

            var sum = camera.DetectIntersections().Select(p => p.x * p.y).Sum();

            Console.WriteLine($"Part I: {sum}");

            Console.ReadLine();
            Console.Clear();


            while (true)
            {
                var logic = File.ReadAllText("movementlogic.ascii").Select(c => (long)c).ToArray();

                prog[0] = 2;
                comp    = new Computer(prog, input: logic);

                foreach (char c in comp.Run())
                {
                    Console.Write(c);
                }

                Console.WriteLine("DONE! Press enter to retry");
                Console.ReadLine();
            }
        }
Example #3
0
        public void Program0()
        {
            int[] baseProgram = new int[] { 1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50 };
            int[] actual      = IntProgram.RunIntProgram(baseProgram);
            int[] expected    = new int[] { 3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50 };

            Assert.True(expected.SequenceEqual(actual));
        }
        static void Main(string[] args)
        {
            var prog = IntProgram.ParseFrom("input.txt");

            //Print(prog);

            Part2(prog);
        }
        static void Main(string[] args)
        {
            var prog   = IntProgram.ParseFrom("input.txt");
            var comp   = new Computer(prog, input: Enumerable.Empty <long>());
            var output = comp.Run().ToList();

            var tiles = new List <(long x, long y, long t)>(output.Count / 3);

            for (var i = 0; i < output.Count; i += 3)
            {
                tiles.Add((output[i], output[i + 1], output[i + 2]));
            }

            Console.WriteLine();
            Console.WriteLine($"Part 1: {tiles.Count(c => c.t == 2)}");
            Console.WriteLine("Press ENTER to start Part II");
            Console.ReadLine();

            var  screen = Screen.Create();
            long ballX = 0, paddleX = 0;

            prog[0] = 2; // set it to 2 to play for free
            comp    = new Computer(prog, input: Joystick());

            var  dsp = new long[3];
            long cnt = 0;

            foreach (var o in comp.Run())
            {
                dsp[cnt % 3] = o;
                cnt++;
                if (cnt % 3 == 0)
                {
                    screen.PutChar(dsp[0], dsp[1], dsp[2]);

                    if (dsp[2] == 4)
                    {
                        ballX = dsp[0];
                    }

                    if (dsp[2] == 3)
                    {
                        paddleX = dsp[0];
                    }
                }
            }


            IEnumerable <long> Joystick()
            {
                while (true)
                {
                    Thread.Sleep(25);
                    if (ballX > paddleX)
                    {
                        yield return(1);
                    }
                    if (ballX == paddleX)
                    {
                        yield return(0);
                    }
                    if (ballX < paddleX)
                    {
                        yield return(-1);
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            // Console.WriteLine("Ticks;From;To;X;Y");
            var program   = IntProgram.ParseFrom("input.txt");
            var sw        = Stopwatch.StartNew();
            var computers = new Computer[Size];
            var inputs    = new Queue <long> [Size];

            for (int i = 0; i < Size; i++)
            {
                inputs[i] = new Queue <long>();
                inputs[i].Enqueue(i);
                computers[i] = new Computer(program, NetworkInput(i));
            }

            var outputs = Interleave(computers.Select(c => c.Walk()));

            var result = Route(outputs, part1: true);

            Console.WriteLine($"Part I: {result} in {sw.ElapsedMilliseconds}ms");

            sw.Restart();

            computers = new Computer[Size];
            inputs    = new Queue <long> [Size];

            for (int i = 0; i < Size; i++)
            {
                inputs[i] = new Queue <long>();
                inputs[i].Enqueue(i);
                computers[i] = new Computer(program, NetworkInput(i));
            }

            outputs = Interleave(computers.Select(c => c.Walk()));
            result  = Route(outputs, part1: false);

            Console.WriteLine($"Part II: {result} in {sw.ElapsedMilliseconds}ms");

            IEnumerable <long> NetworkInput(int idx)
            {
                var input = inputs[idx];

                while (true)
                {
                    if (input.Count == 0)
                    {
                        yield return(-1);
                    }
                    else
                    {
                        long val = input.Dequeue();
                        yield return(val);
                    }
                }
            }

            long Route(IEnumerable <(int comp, long?output, long ticks)> outputs, bool part1)
            {
                var packets = new Queue <long> [Size];

                (long X, long Y)memory   = (0L, 0L);
                (long X, long Y)lastSend = (-1L, -1L);
                const long idleTimeout = 1000;
                long       timestamp   = 0; // ticks of the last routed pocket

                for (int i = 0; i < Size; i++)
                {
                    packets[i] = new Queue <long>();
                }

                foreach ((int comp, long?output, long ticks) in outputs)
                {
                    if (part1 && !output.HasValue)
                    {
                        continue;
                    }

                    if (!part1 && ticks > timestamp + idleTimeout && packets.All(IsEmpty) && inputs.All(IsEmpty))
                    {
                        if (memory.Y == lastSend.Y)
                        {
                            return(memory.Y);
                        }

                        var input = inputs[0];
                        input.Enqueue(memory.X);
                        input.Enqueue(memory.Y);
                        lastSend  = memory;
                        timestamp = ticks;
                    }

                    if (!output.HasValue)
                    {
                        continue;
                    }

                    var packet = packets[comp];
                    packet.Enqueue(output.Value);

                    if (packet.Count == 3)
                    {
                        int  adr = (int)packet.Dequeue();
                        long x   = packet.Dequeue();
                        long y   = packet.Dequeue();

                        //Console.WriteLine($"{ticks};{comp};{adr};{x};{y}");

                        if (adr == 255)
                        {
                            if (part1)
                            {
                                return(y);
                            }
                            memory = (x, y);
                        }
                        else
                        {
                            var input = inputs[adr];
                            input.Enqueue(x);
                            input.Enqueue(y);
                        }

                        timestamp = ticks;
                    }
                }

                return(-1); // all computers ended without sending anything to 255
            }

            bool IsEmpty(Queue <long> q) => q.Count == 0;
        }