示例#1
0
        public static void Solve()
        {
            int sum = 0;

            string             inputPath  = FileUtils.GetProjectFilePath("Days/Day4/ProblemB/input.txt");
            List <SecurityLog> logRecords = new List <SecurityLog>();

            using (var reader = new System.IO.StreamReader(inputPath))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string input = line.Trim();
                    if (!String.IsNullOrEmpty(input))
                    {
                        logRecords.Add(new SecurityLog(input));
                    }
                }
            }
            logRecords.Sort(CompareLogTimestamps);

            Dictionary <string, Guard> guards = new Dictionary <string, Guard>();

            Guard activeGuard = null;
            int   curYear     = Int32.MinValue;
            int   curMonth    = Int32.MinValue;
            int   currDay     = Int32.MinValue;

            foreach (var log in logRecords)
            {
                Log.WriteLine("Processing " + log.RawLog);
                if (log.Year != curYear || log.Month != curMonth || log.Day != currDay)
                {
                    if (activeGuard != null)
                    {
                        activeGuard.FinalizeShift();
                    }

                    curYear  = log.Year;
                    curMonth = log.Month;
                    currDay  = log.Day;
                }

                switch (log.LogType)
                {
                case SecurityLogType.GuardOnDuty:
                    if (activeGuard != null)
                    {
                        activeGuard.GoOffDuty(log.Minute);
                    }

                    activeGuard = null;
                    if (!guards.TryGetValue(log.GuardId, out activeGuard))
                    {
                        activeGuard         = new Guard(log.GuardId);
                        guards[log.GuardId] = activeGuard;
                    }

                    if (log.Hour != 00)
                    {
                        activeGuard.ComeOnDuty(0);
                    }
                    else
                    {
                        activeGuard.ComeOnDuty(log.Minute);
                    }

                    break;

                case SecurityLogType.GuardWokeUp:
                    if (activeGuard != null)
                    {
                        activeGuard.WakeUp(log.Minute);
                    }
                    break;

                case SecurityLogType.GuardFellAsleep:
                    if (activeGuard != null)
                    {
                        activeGuard.GoToSleep(log.Minute);
                    }
                    else
                    {
                        Log.WriteLine("Null guard");
                    }
                    break;
                }
            }

            Guard topGuard = null;

            foreach (var kvp in guards)
            {
                if (topGuard == null || topGuard.PeakSleepMinuteValue < kvp.Value.PeakSleepMinuteValue)
                {
                    topGuard = kvp.Value;
                }
            }

            Log.WriteLine("Sleepiest guard is " + topGuard.GuardId + " result is " + (topGuard.PeakSleepMinute * Int32.Parse(topGuard.GuardId)));
        }
示例#2
0
        public static void Solve()
        {
            int sum = 0;

            string inputPath = FileUtils.GetProjectFilePath("Days/Day19/ProblemA/input.txt");

            string[] allLines = File.ReadAllLines(inputPath);

            List <Instruction> instructions = new List <Instruction>();
            int ip = -1;

            for (int i = 0; i < allLines.Length; i++)
            {
                string line = allLines[i].Trim();
                if (line.StartsWith("#ip"))
                {
                    ip = Int32.Parse(line.Split(' ')[1]);
                }
                else
                {
                    instructions.Add(Instruction.Parse(line));
                }
            }


            BuildOpCodes();

            int[] registers = new int[6];

            Log.WriteLine("Beginning a program of " + instructions.Count + " length. Instruction pointer is at " + ip);


            int instructionCount = 0;

            while (true)
            {
                int ipVal = registers[ip];
                if (ipVal < 0 || ipVal > instructions.Count)
                {
                    break;
                }
                Instruction next = instructions[ipVal];
                var         op   = _allOpCodes[next.OpCode];
                if (!op.Execute(next.Arguments, registers))
                {
                    Log.WriteLine("Failed to execute line " + ipVal);
                }
                instructionCount++;
                registers[ip]++;
            }

            /*
             * int programStartPoint = sets * 4;
             * for (int i = programStartPoint; i < allLines.Length; i++)
             * {
             *      string line = allLines[i].Trim();
             *      if (!string.IsNullOrEmpty(line))
             *      {
             *              int[] commandArgs = InstructionTestSet.ParseCommand(line);
             *              string opCode = _opCodeIds[commandArgs[0]][0];
             *              var op = _allOpCodes[opCode];
             *              if (!op.Execute(commandArgs, registers))
             *              {
             *                      Log.WriteLine("Failed to execute line " + i);
             *              }
             *      }
             * }*/
            Log.WriteLine("Program finished, register state is " + DumpRegisters(registers) + " executed " + instructionCount);
        }