コード例 #1
0
ファイル: Part2.cs プロジェクト: jorupp/adventofcode2019
        protected void RunComputer(string title, string input)
        {
            RunScenario(title, () =>
            {
                var lines       = input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                var initialData = lines[0].Split(',').Select(long.Parse).ToArray();
                initialData[0]  = 2;
                var data        = initialData.Select((i, ii) => new { i, ii }).ToDictionary(i => i.ii, i => i.i);


                var map = new Dictionary <(int, int), char>();

                var state = IntCodeSimulationState.Start(data);

                var inputData = @"A,B,A,B,A,C,B,C,A,C
L,6,R,12,L,6
R,12,L,10,L,4,L,6
L,10,L,10,L,4,L,6
n
".Replace("\r", "").Select(i => (int)i).ToList();

                foreach (var i in inputData)
                {
                    state = state.Resume(i);
                }

                foreach (var o in state.Output)
                {
                    Console.WriteLine(o);
                }
            });
コード例 #2
0
ファイル: Part1.cs プロジェクト: jorupp/adventofcode2019
        protected void RunScenario(string title, string input)
        {
            RunScenario(title, () =>
            {
                var lines       = input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                var initialData = lines[0].Split(',').Select(long.Parse).ToArray();
                var data        = initialData.Select((i, ii) => new { i, ii }).ToDictionary(i => i.ii, i => i.i);

                var initialState = IntCodeSimulationState.Start(data);
                var points       = 0l;
                for (var x = 0; x < 50; x++)
                {
                    var state1 = initialState.Resume(x);
                    for (var y = 0; y < 50; y++)
                    {
                        var state2 = state1.Resume(y);
                        points    += state2.Output.Single();
                    }
                }

                Console.WriteLine(points);
            });
        }
コード例 #3
0
ファイル: Part1.cs プロジェクト: jorupp/adventofcode2019
        protected void RunScenario(string title, string input)
        {
            RunScenario(title, () =>
            {
                var lines       = input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                var initialData = lines[0].Split(',').Select(long.Parse).ToArray();
                var data        = initialData.Select((i, ii) => new { i, ii }).ToDictionary(i => i.ii, i => i.i);

                var map = new Dictionary <(int, int), char>();

                var state = IntCodeSimulationState.Start(data);

                var x = 0;
                var y = 0;
                foreach (var v in state.Output)
                {
                    if (v == 10)
                    {
                        y++;
                        x = 0;
                    }
                    else
                    {
                        map[(x, y)] = (char)v;
コード例 #4
0
ファイル: Part2.cs プロジェクト: jorupp/adventofcode2019
        protected void RunScenario(string title, string input)
        {
            RunScenario(title, () =>
            {
                var lines       = input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                var initialData = lines[0].Split(',').Select(long.Parse).ToArray();
                var data        = initialData.Select((i, ii) => new { i, ii }).ToDictionary(i => i.ii, i => i.i);
                var start       = IntCodeSimulationState.Start(data);

                var computers         = Enumerable.Range(0, 50).Select(i => start.Resume(i)).ToList();
                var queues            = Enumerable.Range(0, 50).Select(i => new ConcurrentQueue <(long, long)>()).ToList();
                (long, long)? natData = null;
                long?lastNatY         = null;

                List <long> handleOutput(List <long> output)
                {
                    while (output.Any())
                    {
                        var target = output[0];
                        var toSend = (output[1], output[2]);
                        if (target < queues.Count)
                        {
                            queues[(int)target].Enqueue(toSend);
                        }
                        else
                        {
                            //Console.WriteLine($"Sending {toSend.Item1},{toSend.Item2} to {target}");
                            if (target == 255)
                            {
                                natData = toSend;
                            }
                        }

                        output = output.Skip(3).ToList();
                    }

                    return(output);
                }

                while (computers.Any(i => !i.Halted))
                {
                    var idle = true;
                    for (var ix = 0; ix < computers.Count; ix++)
                    {
                        var computer = computers[ix];
                        if (computer.Halted)
                        {
                            continue;
                        }

                        if (queues[ix].TryDequeue(out var value))
                        {
                            idle     = false;
                            computer = computer.Resume(value.Item1).Resume(value.Item2);
                        }
                        else
                        {
                            computer = computer.Resume(-1);
                        }

                        handleOutput(computer.Output);

                        computers[ix] = computer;
                    }

                    if (idle)
                    {
                        if (lastNatY == natData.Value.Item2)
                        {
                            Console.WriteLine($"Resending {lastNatY}");
                            return;
                        }

                        lastNatY     = natData.Value.Item2;
                        computers[0] = computers[0].Resume(natData.Value.Item1).Resume(natData.Value.Item2);

                        handleOutput(computers[0].Output);
                    }
                }

                Console.WriteLine("Done");
            });
コード例 #5
0
        protected void RunScenario(string title, string input)
        {
            RunScenario(title, () =>
            {
                var lines       = input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                var initialData = lines[0].Split(',').Select(long.Parse).ToArray();
                var data        = initialData.Select((i, ii) => new { i, ii }).ToDictionary(i => i.ii, i => i.i);
                var start       = IntCodeSimulationState.Start(data);

                var computers = Enumerable.Range(0, 50).Select(i => start.Resume(i)).ToList();
                var queues    = Enumerable.Range(0, 50).Select(i => new ConcurrentQueue <(long, long)>()).ToList();

                while (computers.Any(i => !i.Halted))
                {
                    for (var ix = 0; ix < computers.Count; ix++)
                    {
                        var computer = computers[ix];
                        if (computer.Halted)
                        {
                            continue;
                        }

                        if (queues[ix].TryDequeue(out var value))
                        {
                            computer = computer.Resume(value.Item1).Resume(value.Item2);
                        }
                        else
                        {
                            computer = computer.Resume(-1);
                        }

                        var output = computer.Output;
                        while (output.Any())
                        {
                            var target = output[0];
                            var toSend = (output[1], output[2]);
                            if (target < queues.Count)
                            {
                                queues[(int)target].Enqueue(toSend);
                            }
                            else
                            {
                                Console.WriteLine($"Sending {toSend.Item1},{toSend.Item2} to {target}");
                            }

                            output = output.Skip(3).ToList();
                        }

                        computers[ix] = computer;
                    }
                }

                Console.WriteLine("Done");

                //var threads = computers.Select((i, ix) => new Thread(() =>
                //{
                //    var computer = i;
                //    var inputQueue = queues[ix];

                //    while (!computer.Halted)
                //    {
                //    }
                //})).ToList();


                //Console.WriteLine(lines.Length);
            });
コード例 #6
0
ファイル: Part2.cs プロジェクト: jorupp/adventofcode2019
        protected void RunScenario(string title, string input)
        {
            RunScenario(title, () =>
            {
                var lines       = input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                var initialData = lines[0].Split(',').Select(long.Parse).ToArray();
                var data        = initialData.Select((i, ii) => new { i, ii }).ToDictionary(i => i.ii, i => i.i);

                var initialState = IntCodeSimulationState.Start(data);

                var workedPoints = new List <(long, long)>();

                //bool works(long num)
                //{
                //    var d = num / 1000000000;
                //    var x = num % 1000000000;
                //    var y = Math.Max(0, d - x);
                //    Console.WriteLine($"Checking {x},{y} ({num})");
                //    var state1 = initialState.Resume(x);
                //    var state2 = state1.Resume(y);
                //    var startWorked = state2.Output.Single() == 1;
                //    if (!startWorked)
                //    {
                //        return false;
                //    }

                //    var p1Worked = initialState.Resume(x).Resume(y + 99).Output.Single() == 1;
                //    var p2Worked = initialState.Resume(x + 99).Resume(y).Output.Single() == 1;
                //    var p3Worked = initialState.Resume(x + 99).Resume(y + 99).Output.Single() == 1;
                //    if (p1Worked && p2Worked && p3Worked)
                //    {
                //        workedPoints.Add((x, y));
                //        //Console.WriteLine($"Worked @ {x},{y}");
                //        return true;
                //    }

                //    return false;
                //}

                ////var workingD = DoSearchDown(long.MaxValue, (d) =>
                ////{
                ////    var x = d / 2;
                ////    var y = d - x;
                ////    return works(x, y);
                ////});
                //var workingD = DoSearchDown(long.MaxValue, works);

                //var point = workedPoints.OrderBy(i => i.Item1 + i.Item2).First();

                var cache = new Dictionary <(long, long), bool>();
                bool works(long x, long y)
                {
                    if (cache.TryGetValue((x, y), out var v))
                    {
                        return(v);
                    }

                    var state1 = initialState.Resume(x);
                    var state2 = state1.Resume(y);
                    var r      = state2.Output.Single() == 1;
                    //Console.WriteLine($"Checking {x},{y} -> {r}");
                    cache[(x, y)] = r;
                    return(r);
                    //var startWorked = state2.Output.Single() == 1;
                    //if (!startWorked)
                    //{
                    //    return false;
                    //}

                    //var p1Worked = initialState.Resume(x).Resume(y + 99).Output.Single() == 1;
                    //var p2Worked = initialState.Resume(x + 99).Resume(y).Output.Single() == 1;
                    //var p3Worked = initialState.Resume(x + 99).Resume(y + 99).Output.Single() == 1;
                    //if (p1Worked && p2Worked && p3Worked)
                    //{
                    //    //workedPoints.Add((x, y));
                    //    //Console.WriteLine($"Worked @ {x},{y}");
                    //    return true;
                    //}

                    //return false;
                }