public long Solve2(IList <string> input)
        {
            var commands = input.Select(x => HandheldGameConsole.ParseCommand(x)).ToList();

            for (var i = 0; i < commands.Count; i++)
            {
                if (commands[i].instruction == "acc")
                {
                    continue;
                }

                var copy = commands.ToList();
                copy[i] = (SwitchOperation(commands[i].instruction), commands[i].number);

                var handheld  = new HandheldGameConsole(copy);
                var completed = handheld.Boot();

                if (completed)
                {
                    return(handheld.Accumulator);
                }
            }

            return(0);
        }
Exemple #2
0
        private static int FindCorrectMutation(Instruction[] code)
        {
            var visited     = new HashSet <InstructionPointer>();
            var gameConsole = new HandheldGameConsole(stopFunction: ip => !visited.Add(ip) ? Termination.InfiniteLoop : Termination.None);

            for (var i = 0; i < code.Length; ++i)
            {
                if (code[i].Operation == Operation.Acc)
                {
                    continue;
                }

                visited.Clear();
                var originalInstruction = code[i];
                code[i] = SwapInstruction(originalInstruction);

                var(result, termination) = gameConsole.Run(code);

                if (termination == Termination.RanToCompletion)
                {
                    return(result);
                }

                code[i] = originalInstruction;
            }

            throw new InvalidOperationException();
        }
Exemple #3
0
        protected override object SolvePartOne()
        {
            var hgc = HandheldGameConsole.Create(Input);

            hgc.Run();
            return(hgc.Acc);
        }
        public long Solve1(IList <string> input)
        {
            var commands = input.Select(x => HandheldGameConsole.ParseCommand(x)).ToList();

            var handheld = new HandheldGameConsole(commands);

            handheld.Boot();

            return(handheld.Accumulator);
        }
Exemple #5
0
        public void Part2(string file, int expected)
        {
            var code    = File.ReadAllLines(file).Select(ParseInstruction).ToArray();
            var visited = new HashSet <InstructionPointer>();

            var gameConsole = new HandheldGameConsole(stopFunction: ip => !visited.Add(ip) ? Termination.InfiniteLoop : Termination.None);
            var result      = FindCorrectMutation(code);

            Assert.Equal(expected, result);
        }
        public void Test1()
        {
            var input = @"nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6";

            HandheldGameConsole.GetAccumulatorAtLoopInfiniteLoopStart(input)
            .Should().Be(5);
        }
        public void Test2()
        {
            var input = @"nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6";

            HandheldGameConsole.RunAndFix(input)
            .Should().Be(8);
        }
Exemple #8
0
        protected override object SolvePartTwo()
        {
            int i = 0;

            while (true)
            {
                var hgc = HandheldGameConsole.Create(Input);
                hgc.FlipInstructin(i);
                if (hgc.Run())
                {
                    i = hgc.Acc;
                    break;
                }
                i++;
            }

            return(i);
        }