Exemple #1
0
        //######################################################################
        // Part 1 + 2 Solutions
        //######################################################################

        public static int Part1()
        {
            const string path          = Helpers.inputPath + @"\day08\input.txt";
            IProgram     input         = ProcessInputFile(path);
            int          programLength = input.Accept(new TotalLength());
            Run          v             = new Run(programLength);

            input.Accept(v);
            return(v.Accumulator);
        }
Exemple #2
0
        public static int Part2()
        {
            // TODO: make this more visitor-ey
            const string path          = Helpers.inputPath + @"\day08\input.txt";
            IProgram     input         = ProcessInputFile(path);
            int          programLength = input.Accept(new TotalLength());

            IList <(string, int)> instructions = Helpers.ProcessInputFile(path, StringToInstruction);

            for (int i = 0; i < instructions.Count; i++)
            {
                IList <IProgram> newInstructions = instructions.Select((inst, j) =>
                {
                    switch (inst.Item1)
                    {
                    case "acc": return(IProgram.MakeACC(inst.Item2));

                    case "nop":
                        {
                            if (i == j)
                            {
                                return(IProgram.MakeJMP(inst.Item2));
                            }
                            else
                            {
                                return(IProgram.MakeNOP(inst.Item2));
                            }
                        }

                    case "jmp":
                        {
                            if (i == j)
                            {
                                return(IProgram.MakeNOP(inst.Item2));
                            }
                            else
                            {
                                return(IProgram.MakeJMP(inst.Item2));
                            }
                        }

                    default: throw new ArgumentException("Invalid opcode.");
                    }
                }).ToList();

                IProgram sequence = IProgram.MakeSequence(newInstructions);
                Run      rv       = new Run(programLength);
                if (sequence.Accept(rv))
                {
                    Console.WriteLine("Index changed: " + i);
                    return(rv.Accumulator);
                }
            }
            throw new Exception("Value not found");
        }
Exemple #3
0
        IList <Run> IProgram.IVisitor <IList <Run> > .Visit(JMP jmp)
        {
            IList <Run> terminated = new List <Run>();
            Run         jmpRun     = new Run(1);

            if (jmp.Accept(jmpRun))
            {
                terminated.Add(jmpRun);
            }

            IProgram nop    = IProgram.MakeNOP(jmp.Argument);
            Run      nopRun = new Run(1);

            if (nop.Accept(nopRun))
            {
                terminated.Add(nopRun);
            }

            return(terminated);
        }
Exemple #4
0
 public ReplaceAndRun(IProgram program)
 {
     sourceProgram = program.Accept(new Flatten());
     sourceLength  = program.Accept(new TotalLength());
 }