コード例 #1
0
ファイル: Solution.cs プロジェクト: encse/adventofcode
    public object PartOne(string input)
    {
        var securityRoom = "== Security Checkpoint ==";
        var icm          = new IntCodeMachine(input);
        var description  = icm.Run().ToAscii();

        VisitRooms(securityRoom, icm, description, args => {
            foreach (var item in args.items)
            {
                if (item != "infinite loop")
                {
                    var takeCmd = "take " + item;
                    var clone   = icm.Clone();
                    clone.Run(takeCmd);
                    if (!clone.Halted() && Inventory(clone).Contains(item))
                    {
                        icm.Run(takeCmd);
                    }
                }
            }
            return(null);
        });

        var door = VisitRooms(securityRoom, icm, description, args =>
                              args.room == securityRoom ? args.doors.Single(door => door != ReverseDir(args.doorTaken)) : null);

        Random r = new Random();

        void TakeOrDrop(string cmd, List <string> from, List <string> to)
        {
            var i    = r.Next(from.Count);
            var item = from[i];

            from.RemoveAt(i);
            to.Add(item);
            icm.Run(cmd + " " + item);
        }

        var inventory = Inventory(icm).ToList();
        var floor     = new List <string>();

        while (true)
        {
            var output = icm.Run(door).ToAscii();
            if (output.Contains("heavier"))
            {
                TakeOrDrop("take", floor, inventory);
            }
            else if (output.Contains("lighter"))
            {
                TakeOrDrop("drop", inventory, floor);
            }
            else
            {
                return(long.Parse(Regex.Match(output, @"\d+").Value));
            }
        }
    }