Ejemplo n.º 1
0
        public List <aocinstruction> getInput()
        {
            string filename = @"C:\dev\adventofcode\2020\Day08\input.txt";
            List <aocinstruction> inputList = new List <aocinstruction>();
            int count = 0;

            try
            {
                using (StreamReader reader = new StreamReader(filename))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        aocinstruction ins = new aocinstruction();

                        ins.Id        = count;
                        ins.Operation = line.Split(" ")[0];
                        ins.Argument  = Convert.ToInt32(line.Split(" ")[1]);
                        ins.Executed  = false;

                        inputList.Add(ins);
                        count++;
                    }
                }
                return(inputList);
            }
            catch (System.Exception)
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        public void solve(List <aocinstruction> input)
        {
            int    accumulator = 0;
            int    step        = 0;
            bool   execute     = true;
            string output      = "error";

            while (execute)
            {
                aocinstruction nextInstruction = input[step];

                if (nextInstruction.Executed || step > input.Count)
                {
                    if (Part == 1)
                    {
                        output = "solved";
                    }

                    execute = false;
                    break;
                }

                input[step].Executed = true;

                switch (nextInstruction.Operation)
                {
                case "acc":
                    accumulator += nextInstruction.Argument;
                    step++;
                    break;

                case "jmp":
                    step += nextInstruction.Argument;
                    break;

                case "nop":
                    step++;
                    break;

                default:
                    break;
                }

                if (step >= input.Count)
                {
                    output = "solved";
                    break;
                }
            }

            if (Part == 1 || output == "solved")
            {
                Console.WriteLine(string.Format("accumulator value: {0} with {1}", accumulator.ToString(), output));
            }
        }