Exemplo n.º 1
0
        public static long ExecutePart1()
        {
            //Init
            var result        = new List <long>();
            var computer      = new IntcodeComputer();
            var phasesettings = Permutate(new List <long>()
            {
                0, 1, 2, 3, 4
            }, 5);
            var program = File.ReadAllText(@"..\..\..\Day7Input.txt").Split(",").Select(value => long.Parse(value)).ToList();

            //Test all phase settings permutations
            foreach (var phasesetting in phasesettings)
            {
                long signal = 0;
                foreach (var ps in phasesetting)
                {
                    //Load program
                    computer.Load(program);

                    //Run program
                    var output = computer.Run(new Queue <long>(new long[] { ps, signal }));
                    if (output.HasValue)
                    {
                        signal = output.Value;
                    }
                }
                result.Add(signal);
            }

            return(result.Max());
        }
Exemplo n.º 2
0
        public static void PartTwo()
        {
            List <string> commands = new List <string>
            {
                "A,B,B,A,C,B,C,C,B,A",
                "R,10,R,8,L,10,L,10",
                "R,8,L,6,L,6",
                "L,10,R,10,L,6",
                "n"
            };

            long[]          data = InputHelper.GetIntcodeFromFile("17");
            IntcodeComputer com  = new IntcodeComputer(data, IntcodeMode.Quiet);

            foreach (string command in commands)
            {
                foreach (char c in command.ToCharArray())
                {
                    com.InputQueue.Enqueue((long)c);
                }
                com.InputQueue.Enqueue(10);
            }
            com.Context.Data[0] = 2;

            com.Run();

            Console.WriteLine("Dust collected : " + com.OutputQueue.Dequeue());
        }
Exemplo n.º 3
0
        public static void PartOne()
        {
            long[] data = InputHelper.GetIntcodeFromFile("2");

            IntcodeComputer i = new IntcodeComputer(data);

            Console.Write(i.Run(12, 2));
        }
Exemplo n.º 4
0
        public static void PartOneAndTwo()
        {
            long[] data = InputHelper.GetIntcodeFromFile("5");

            IntcodeComputer i = new IntcodeComputer(data);

            i.Run();
        }
Exemplo n.º 5
0
        public static void PartOne()
        {
            long[] data = InputHelper.GetIntcodeFromFile("9");

            IntcodeComputer i = new IntcodeComputer(data);

            i.InputQueue.Enqueue(1);
            i.Run();
        }
Exemplo n.º 6
0
        public static void Trace()
        {
            long[] data = InputHelper.GetIntcodeFromFile("9");

            IntcodeComputer i = new IntcodeComputer(data, IntcodeMode.Trace);

            i.Run();

            File.WriteAllText(InputHelper.GetOutputPathForFile("9_trace"), String.Join(Environment.NewLine, i.GetTrace()));
        }
Exemplo n.º 7
0
        public static void PartOne()
        {
            long[] data = InputHelper.GetIntcodeFromFile("15");

            IntcodeComputer com = new IntcodeComputer(data, IntcodeMode.Blocking | IntcodeMode.Quiet);
            Thread          bot = new Thread(() => com.Run());

            bot.Start();
            Maze maze = MakeMaze(com);

            Display(maze);
        }
Exemplo n.º 8
0
        public static void PartTwo()
        {
            long[] data = InputHelper.GetIntcodeFromFile("7");

            List <int> phases = new List <int> {
                5, 6, 7, 8, 9
            };

            var allPhases = Permute(phases);
            Dictionary <long, List <int> > outputPerPhase = new Dictionary <long, List <int> >();

            foreach (IEnumerable <int> p in allPhases)
            {
                var             phase = p.ToList();
                IntcodeComputer a     = new IntcodeComputer(data.ToArray(), IntcodeMode.Quiet | IntcodeMode.Blocking, "A");
                IntcodeComputer b     = new IntcodeComputer(data.ToArray(), IntcodeMode.Quiet | IntcodeMode.Blocking, "B");
                IntcodeComputer c     = new IntcodeComputer(data.ToArray(), IntcodeMode.Quiet | IntcodeMode.Blocking, "C");
                IntcodeComputer d     = new IntcodeComputer(data.ToArray(), IntcodeMode.Quiet | IntcodeMode.Blocking, "D");
                IntcodeComputer e     = new IntcodeComputer(data.ToArray(), IntcodeMode.Quiet | IntcodeMode.Blocking, "E");
                a.InputQueue = e.OutputQueue;
                a.InputQueue.Enqueue(phase[0]);
                a.InputQueue.Enqueue(0);
                b.InputQueue = a.OutputQueue;
                b.InputQueue.Enqueue(phase[1]);
                c.InputQueue = b.OutputQueue;
                c.InputQueue.Enqueue(phase[2]);
                d.InputQueue = c.OutputQueue;
                d.InputQueue.Enqueue(phase[3]);
                e.InputQueue = d.OutputQueue;
                e.InputQueue.Enqueue(phase[4]);
                Thread at = new Thread(() => a.Run());
                Thread bt = new Thread(() => b.Run());
                Thread ct = new Thread(() => c.Run());
                Thread dt = new Thread(() => d.Run());
                Thread et = new Thread(() => e.Run());
                at.Start();
                bt.Start();
                ct.Start();
                dt.Start();
                et.Start();
                at.Join();
                bt.Join();
                ct.Join();
                dt.Join();
                et.Join();
                long output = e.OutputQueue.Dequeue();
                outputPerPhase.Add(output, phase);
                Console.WriteLine($"Phase {String.Join(',', phase)} produces output {output}");
            }
            var maxPhase = outputPerPhase.First(o => o.Key == outputPerPhase.Max(k => k.Key));

            Console.WriteLine($"Phase with biggest output {String.Join(',', maxPhase.Value)} produces output {maxPhase.Key}");
        }
Exemplo n.º 9
0
        public static void PartOne()
        {
            long[] data = InputHelper.GetIntcodeFromFile("13");

            IntcodeComputer computer = new IntcodeComputer(data);

            computer.Run();

            List <Tile> tiles = computer.OutputQueue.ToList().Sublists(3).Select(t => new Tile(t[0], t[1], t[2])).ToList();

            int blocks = tiles.Count(t => t.Type == Tile.TileId.Block);

            Console.WriteLine($"There are {blocks} block tiles");
        }
Exemplo n.º 10
0
        public static void PartTwo()
        {
            long[] data = InputHelper.GetIntcodeFromFile("2");

            for (int noun = 0; noun < 100; noun++)
            {
                for (int verb = 0; verb < 100; verb++)
                {
                    IntcodeComputer i = new IntcodeComputer(data);
                    if (i.Run(noun, verb) == 19690720)
                    {
                        Console.WriteLine($"{noun} {verb}");
                        return;
                    }
                }
            }
        }
Exemplo n.º 11
0
        public static void PartOne()
        {
            long[] data = InputHelper.GetIntcodeFromFile("7");

            List <int> phases = new List <int> {
                0, 1, 2, 3, 4
            };

            var allPhases = Permute(phases);
            Dictionary <long, List <int> > outputPerPhase = new Dictionary <long, List <int> >();

            foreach (IEnumerable <int> p in allPhases)
            {
                var             phase = p.ToList();
                IntcodeComputer a     = new IntcodeComputer(data);
                a.InputQueue.Enqueue(phase[0]);
                a.InputQueue.Enqueue(0);
                a.Run();
                IntcodeComputer b = new IntcodeComputer(data);
                b.InputQueue.Enqueue(phase[1]);
                b.InputQueue.Enqueue(a.OutputQueue.Dequeue());
                b.Run();
                IntcodeComputer c = new IntcodeComputer(data);
                c.InputQueue.Enqueue(phase[2]);
                c.InputQueue.Enqueue(b.OutputQueue.Dequeue());
                c.Run();
                IntcodeComputer d = new IntcodeComputer(data);
                d.InputQueue.Enqueue(phase[3]);
                d.InputQueue.Enqueue(c.OutputQueue.Dequeue());
                d.Run();
                IntcodeComputer e = new IntcodeComputer(data);
                e.InputQueue.Enqueue(phase[4]);
                e.InputQueue.Enqueue(d.OutputQueue.Dequeue());
                e.Run();
                long output = e.OutputQueue.Dequeue();
                outputPerPhase.Add(output, phase);
                Console.WriteLine($"Phase {String.Join(',', phase)} produces output {output}");
            }
            var maxPhase = outputPerPhase.First(o => o.Key == outputPerPhase.Max(k => k.Key));

            Console.WriteLine($"Phase with biggest output {String.Join(',', maxPhase.Value)} produces output {maxPhase.Key}");
        }
Exemplo n.º 12
0
        private static string RunBoostProgram(long input)
        {
            //Init
            var computer = new IntcodeComputer();
            var program  = File.ReadAllText(@"..\..\..\Day9Input.txt").Split(",").Select(value => long.Parse(value)).ToList();
            var result   = new List <long>();

            //Load program
            computer.Load(program);

            //Run program
            while (computer.State != ProgramState.Finished)
            {
                var output = computer.Run(new Queue <long>(new long[] { input }));
                if (output.HasValue)
                {
                    result.Add(output.Value);
                }
            }

            return(string.Join(",", result.Select(value => value.ToString())));
        }
Exemplo n.º 13
0
        public static void PartOne()
        {
            long[]          data     = InputHelper.GetIntcodeFromFile("21");
            IntcodeComputer com      = new IntcodeComputer(data, IntcodeMode.Blocking | IntcodeMode.Quiet);
            List <string>   commands = new List <string>();

            Console.WriteLine("Data ?");
            string line = "";

            while (line != "WALK")
            {
                line = Console.ReadLine();
                commands.Add(line + '\n');
            }
            foreach (string command in commands)
            {
                foreach (char c in command)
                {
                    com.InputQueue.Enqueue(c);
                }
            }
            com.Run();
            if (com.OutputQueue.Count > 1)
            {
                StringBuilder sb = new StringBuilder();
                while (com.OutputQueue.Count > 0)
                {
                    long output = com.OutputQueue.Dequeue();
                    sb.Append((char)output);
                }
                Console.WriteLine(sb.ToString());
            }
            else
            {
                Console.WriteLine("Result : " + com.OutputQueue.Dequeue());
            }
        }
Exemplo n.º 14
0
        public static void PartOne()
        {
            long[] data = InputHelper.GetIntcodeFromFile("19");

            StringBuilder sb = new StringBuilder();

            for (int y = 810; y < 1047; y++)
            {
                for (int x = 840; x < 1047; x++)
                {
                    IntcodeComputer com = new IntcodeComputer(data, IntcodeMode.Quiet);
                    com.InputQueue.Enqueue(x);
                    com.InputQueue.Enqueue(y);
                    com.Run();
                    if (com.OutputQueue.Dequeue() == 0)
                    {
                        sb.Append('.');
                    }
                    else
                    {
                        if (y >= 812 && x >= (845 + 84) &&
                            y < 912 && x < (945 + 84))
                        {
                            sb.Append('X');
                        }
                        else
                        {
                            sb.Append('#');
                        }
                    }
                }
                sb.AppendLine();
            }

            File.WriteAllText(InputHelper.GetOutputPathForFile("19_big_offset"), sb.ToString());
        }
Exemplo n.º 15
0
        public static void PartOne()
        {
            long[]       data          = InputHelper.GetIntcodeFromFile("11");
            List <Panel> panelsPainted = new List <Panel>();

            IntcodeComputer robot   = new IntcodeComputer(data, IntcodeMode.Blocking | IntcodeMode.Quiet);
            Thread          running = new Thread(() => robot.Run());

            running.Start();
            int       x      = 0;
            int       y      = 0;
            Direction facing = Direction.Up;

            while (!robot.IsFinished)
            {
                Panel panelAtXY = panelsPainted.FirstOrDefault(p => p.X == x && p.Y == y);
                if (panelAtXY == null)
                {
                    robot.InputQueue.Enqueue(0);
                }
                else
                {
                    robot.InputQueue.Enqueue(panelAtXY.Color == Color.White ? 1 : 0);
                }
                while (!robot.IsFinished && robot.OutputQueue.Count == 0)
                {
                    ;
                }
                if (robot.IsFinished)
                {
                    break;
                }
                long paint = robot.OutputQueue.Dequeue();
                if (paint == 1)
                {
                    if (panelAtXY != null)
                    {
                        panelAtXY.Color = Color.White;
                    }
                    else
                    {
                        panelsPainted.Add(new Panel(x, y, Color.White));
                    }
                }
                else
                {
                    if (panelAtXY != null)
                    {
                        panelAtXY.Color = Color.Black;
                    }
                    else
                    {
                        panelsPainted.Add(new Panel(x, y, Color.Black));
                    }
                }
                while (robot.OutputQueue.Count == 0)
                {
                    ;
                }
                long action = robot.OutputQueue.Dequeue();
                if (action == 1)
                {
                    facing = TurnRight(facing);
                }
                else
                {
                    facing = TurnLeft(facing);
                }
                switch (facing)
                {
                case Direction.Up:
                    y--;
                    break;

                case Direction.Left:
                    x--;
                    break;

                case Direction.Down:
                    y++;
                    break;

                case Direction.Right:
                    x++;
                    break;
                }
            }
            Console.WriteLine("Robot finished painting !");
            for (y = panelsPainted.Min(p => p.Y); y < panelsPainted.Max(p => p.Y); y++)
            {
                for (x = panelsPainted.Min(p => p.X); x < panelsPainted.Max(p => p.X); x++)
                {
                    Panel panelAtXY = panelsPainted.FirstOrDefault(p => p.X == x && p.Y == y);
                    if (panelAtXY == null || panelAtXY.Color == Color.Black)
                    {
                        Console.Write('.');
                    }
                    else
                    {
                        Console.Write('#');
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine($"{panelsPainted.Count} panels were painted !");
        }