Ejemplo n.º 1
0
        static void Main()
        {
            var sw = new Stopwatch();

            sw.Start();

            _input = File.ReadAllText("../../../input.txt").ParseLongs();

            _pointCache = new Dictionary <Point, bool>();

            var computer = LongCodeComputer.FromFile(@"../../../input.txt");
            var sum      = 0;

            for (int y = 0; y < 50; y++)
            {
                for (int x = 0; x < 50; x++)
                {
                    var inBeam = getPoint(x, y);
                    sum += inBeam ? 1 : 0;
                    Console.Write(inBeam ? '#' : '.');
                }
                Console.WriteLine();
            }
            Console.WriteLine($"Part 1: {sum}");

            var probe = new Point(0, 10);

            var diagMove = new Size(1, -1);
            var topLeft  = Point.Empty;

            while (topLeft == Point.Empty)
            {
                while (!getPoint(probe))
                {
                    probe = probe.MoveTo(Direction.Right);
                }

                var diagonal = new BinarySearchInt(dx => getPoint(probe + (diagMove * dx))).FindLast();

                if (diagonal >= 98)
                {
                    var rect = new Rectangle(probe.X, probe.Y - 99, 100, 100);
                    if (allPointsInBeam(rect))
                    {
                        topLeft = rect.Location;
                        break;
                    }
                }
                probe = probe.MoveTo(Direction.Down);
            }

            Console.WriteLine($"Part 2: topleft point is at {topLeft} => answer = {topLeft.X * 10000 + topLeft.Y}");
            //Console.WriteLine("15231022");
            sw.Stop();
            Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms.");
            _ = Console.ReadLine();
        }
Ejemplo n.º 2
0
        static void Main()
        {
            var sw = new Stopwatch();

            sw.Start();

            //var input = File.ReadAllText("../../../input.txt").ParseLongs();
            var c = LongCodeComputer.FromFile(@"../../../input.txt");

            c.Store(0, 2);

            var path = "R8L12R8R8L12R8L10L10R8L12L12L10R10L10L10R8L12L12L10R10L10L10R8R8L12R8L12L12L10R10R8L12R8";

            var segments = new Stack <(char name, string value)>();

            _ = FindPathFunctions(path, segments);

            foreach (var func in segments)
            {
                Console.WriteLine($"Function {func.name} should be {func.value}");
            }
            Console.ReadLine();

            Console.WindowHeight = Math.Max(Console.WindowHeight, 40);

            Queue(c, "A,A,B,C,B,C,B,A,C,A\n");
            Queue(c, "R,8,L,12,R,8\n");
            Queue(c, "L,10,L,10,R,8\n");
            Queue(c, "L,12,L,12,L,10,R,10\n");
            Queue(c, "Y\n");
            c.RunForOutputs(2167);
            Console.Clear();
            c.Outputs.Clear();

            while (c.RunForOutputs(2101))
            {
                Paint(c.Outputs);
                c.Outputs.Clear();
            }
            var collectedDust = c.Outputs.Dequeue();

            Console.WriteLine($"Part 2: {collectedDust} dust collected.");

            sw.Stop();
            Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms.");
            _ = Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main()
        {
            var sw = new Stopwatch();

            sw.Start();

            var c = LongCodeComputer.FromFile(@"../../../input.txt");

            var allCommands = new List <string>();

            if (File.Exists(@"../../../commands.txt"))
            {
                allCommands = File.ReadAllLines(@"../../../commands.txt").ToList();
                foreach (var item in allCommands)
                {
                    c.QueueInput(item);
                }
            }
            _ = c.RunUntilInputRequired();
            PrintOutputs(c);

            var items = new string[] { "asterisk", "ornament", "cake", "space heater", "festive hat", "semiconductor", "food ration", "sand" };

            TryAllCombinations(c, items);

            while (c.RunUntilInputRequired())
            {
                PrintOutputs(c);
                var cmd = Console.ReadLine();
                allCommands.Add(cmd);
                c.QueueInput(cmd);
            }

            PrintOutputs(c);
            Console.WriteLine($"Part 1: ");
            sw.Stop();

            Console.WriteLine("Save commands?");
            if (Console.ReadLine() == "y")
            {
                File.WriteAllLines(@"../../../commands.txt", allCommands);
            }

            Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms.");
            _ = Console.ReadLine();
        }
Ejemplo n.º 4
0
        static void Main()
        {
            var sw = new Stopwatch();

            sw.Start();

            var computers = new LongCodeComputer[50];

            for (int i = 0; i < 50; i++)
            {
                computers[i] = LongCodeComputer.FromFile(@"../../../input.txt");
                computers[i].Inputs.Enqueue(i);
                computers[i].DefaultInput = -1;
            }

            Packet?natPacket      = null;
            Packet?lastNatPacket  = null;
            Packet?firstNatPacket = null;


            var idlePeriod = 0;
            var runNetwork = true;

            while (runNetwork)
            {
                foreach (var c in computers)
                {
                    c.Run(2);
                }
                var packets = computers.SelectMany(c => CollectPackets(c)).ToList();

                if (packets.Any())
                {
                    idlePeriod = 0;
                    foreach (var p in packets)
                    {
                        if (p.addr == 255)
                        {
                            natPacket = p;
                        }
                        else
                        {
                            QueuePacket(computers[p.addr], p);
                        }
                    }
                }
                else
                {
                    idlePeriod++;
                }


                if (idlePeriod > 1000 && natPacket.HasValue)
                {
                    // Send packet to fight network idling
                    QueuePacket(computers[0], natPacket.Value);
                    idlePeriod = 0;
                    firstNatPacket ??= natPacket;
                    Console.WriteLine($"Sent packet {natPacket.Value.y}");

                    if (lastNatPacket.HasValue && lastNatPacket.Value.y == natPacket.Value.y)
                    {
                        Console.WriteLine($"Duplicate y! {natPacket.Value.y}");
                        runNetwork = false;
                    }

                    lastNatPacket = natPacket;
                    natPacket     = null;
                }
            }
            // 19420 is too high

            Console.WriteLine($"Part 1: santa packet has y value {firstNatPacket.Value.y}");

            sw.Stop();
            Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms.");
            _ = Console.ReadLine();
        }
Ejemplo n.º 5
0
        static void Main()
        {
            var sw = new Stopwatch();

            sw.Start();

            var c = LongCodeComputer.FromFile(@"../../../input.txt");

            Console.WindowHeight = Math.Max(Console.WindowHeight, 40);

            c.QueueInput("NOT A J");
            c.QueueInput("NOT B T");
            c.QueueInput("OR T J");
            c.QueueInput("NOT C T");
            c.QueueInput("OR T J");
            c.QueueInput("AND D J");
            c.QueueInput("WALK");

            Console.Clear();
            c.Outputs.Clear();

            c.Run();

            if (c.Outputs.Last() < 256)
            {
                Console.WriteLine($"Bad code!");
                Paint(c.Outputs);
                _ = Console.ReadLine();
                return;
            }

            var shipDamage = c.Outputs.Last();

            Console.WriteLine($"Part 1: damage = {shipDamage}");

            c = LongCodeComputer.FromFile(@"../../../input.txt");

            c.QueueInput("NOT A J");
            c.QueueInput("NOT B T");
            c.QueueInput("OR T J");
            c.QueueInput("NOT C T");
            c.QueueInput("OR T J");
            c.QueueInput("AND D J");
            c.QueueInput("NOT E T");
            c.QueueInput("NOT T T");
            c.QueueInput("OR H T");
            c.QueueInput("AND T J");
            c.QueueInput("RUN");

            c.Outputs.Clear();

            c.Run();

            if (c.Outputs.Last() < 256)
            {
                Console.WriteLine($"Bad code!");
                Paint(c.Outputs);
                _ = Console.ReadLine();
                return;
            }

            shipDamage = c.Outputs.Last();
            Console.WriteLine($"Part 2: damage = {shipDamage}");

            sw.Stop();
            Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms.");
            _ = Console.ReadLine();
        }