コード例 #1
0
        public void GetFixAttemptsOf_only_changes_one_instruction()
        {
            var corrupted   = new BootCode(Example);
            var fixAttempts = GetFixAttemptsOf(Example);

            fixAttempts.Should().OnlyContain(x => x.Instructions.Except(corrupted.Instructions).Count() == 1);
        }
コード例 #2
0
        public void Executing_Jump_moves_instruction_pointer_by_its_offset()
        {
            var bootCode = new BootCode(Example);

            bootCode.Execute(3).NextInstruction
            .Should().Be(bootCode.Instructions.Skip(6).First());
        }
コード例 #3
0
        public void Executing_Noop_moves_next_instruction_forward()
        {
            var bootCode = new BootCode(Example);

            bootCode.Execute(1).NextInstruction
            .Should().Be(bootCode.Instructions.Skip(1).First());
        }
コード例 #4
0
        public void NextInstruction_is_the_first_one_by_default()
        {
            var bootCode = new BootCode(Example);

            bootCode.NextInstruction
            .Should().Be(bootCode.Instructions.First());
        }
コード例 #5
0
        public void Executing_Accumulate_changes_accumulator_by_its_value()
        {
            var bootCode = new BootCode(Example);

            bootCode.Execute(2).Accumulator
            .Should().Be(bootCode.Accumulator + 1);
        }
コード例 #6
0
        public void Executing_Accumulate_moves_instruction_pointer_forward()
        {
            var bootCode = new BootCode(Example);

            bootCode.Execute(2).NextInstruction
            .Should().Be(bootCode.Instructions.Skip(2).First());
        }
コード例 #7
0
        public void DoNothing_WhenNopIsExecuted()
        {
            var bootCode = new BootCode("nop +0");

            bootCode.Run();

            Assert.Equal(0, bootCode.Accumulator);
        }
コード例 #8
0
        public void AddSevenToAccumulator_WhenAcc7InstructionIsExecuted()
        {
            var bootCode = new BootCode("acc +7");

            bootCode.Run();

            Assert.Equal(7, bootCode.Accumulator);
        }
コード例 #9
0
        public void SolveSecondPuzzle()
        {
            var bootCode = new BootCode(PUZZLE_DATA);

            bootCode.FixBug();
            bootCode.Run();

            Assert.Equal(1089, bootCode.Accumulator);
        }
コード例 #10
0
        public void Executing_Accumulate_adds_it_to_executed()
        {
            var bootCode = new BootCode(Example);

            bootCode.Execute(2).ExecutedInstructions
            .Should().ContainEquivalentOf(
                bootCode.Instructions.Skip(1).First(),
                opt => opt.RespectingRuntimeTypes());
        }
コード例 #11
0
        public void SolveFirstPuzzle()
        {
            const string instructions = PUZZLE_DATA;

            var bootCode = new BootCode(instructions);

            bootCode.Run();

            Assert.Equal(1179, bootCode.Accumulator);
        }
コード例 #12
0
        public void Setup()
        {
            var testData = new[]
            {
                "nop +0", "acc +1", "jmp +4", "acc +3", "jmp -3", "acc -99", "acc +1", "jmp -4", "acc +6"
            };

            var mockPuzzleInput = new Mock <IPuzzleInput>();

            mockPuzzleInput.Setup(p => p.GetPuzzleInputAsArray(It.IsAny <string>())).Returns(testData);

            _bootCode = new BootCode(mockPuzzleInput.Object);
        }
コード例 #13
0
        public void ExecuteJumpInstructionCorrectly()
        {
            const string instructions = @"jmp +4
acc +1
acc +1
acc +1
acc +1";
            var          bootCode     = new BootCode(instructions);

            bootCode.Run();

            Assert.Equal(1, bootCode.Accumulator);
        }
コード例 #14
0
        public void ReturnAccumulatorValueOf5_JustBeforeExecutingAnInstructionASecondTime()
        {
            const string instructions = @"nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6";

            var bootCode = new BootCode(instructions);

            bootCode.Run();

            Assert.Equal(5, bootCode.Accumulator);
        }
コード例 #15
0
        public void ReturnAccumulatorValueOf8_WhenExecutingCodeToEnd()
        {
            const string instructions = @"nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
nop -4
acc +6";

            var bootCode = new BootCode(instructions);

            bootCode.Run();

            Assert.Equal(8, bootCode.Accumulator);
        }
コード例 #16
0
        private IEnumerable <BootCode> GetFixAttemptsOf(string raw)
        {
            var corrupted = new BootCode(raw);

            foreach (var(instruction, i) in corrupted.Instructions.Select((instruction, i) => (instruction, i)))
            {
                var mutant =
                    instruction.Convert(
                        noop => new BootCode(corrupted.Instructions.ReplaceAt(i, new Jump(noop.N))),
                        accumulate => corrupted,
                        jump => new BootCode(corrupted.Instructions.ReplaceAt(i, new Noop(jump.Offset))),
                        terminate => corrupted);

                if (mutant != corrupted)
                {
                    yield return(mutant);
                }
            }
        }
コード例 #17
0
        public void FixInstructionsToRun()
        {
            const string instructions = @"nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6";

            var bootCode = new BootCode(instructions);

            bootCode.FixBug();
            bootCode.Run();

            Assert.Equal(8, bootCode.Accumulator);
        }
コード例 #18
0
        public void InitializeAccumulatorAsZero()
        {
            var sut = new BootCode("nop +1");

            Assert.Equal(0, sut.Accumulator);
        }
コード例 #19
0
ファイル: Rom.cs プロジェクト: kskjer/MipsSharp
        public static ImmutableArray <UInt32> RecalculateCrc(IReadOnlyList <byte> romContents)
        {
            UInt32 ROL(UInt32 x, int b) =>
            (((x) << (b)) | ((x) >> (32 - (b))));

            int    i, bootcode;
            UInt32 seed;

            UInt32 t1, t2, t3;
            UInt32 t4, t5, t6;
            UInt32 r, d;

            UInt32[] crc = new UInt32[2];

            switch ((bootcode = BootCode.IdentifyFromRom(romContents)))
            {
            case 6101:
            case 6102:
                seed = BootCode.CIC6102;
                break;

            case 6103:
                seed = BootCode.CIC6103;
                break;

            case 6105:
                seed = BootCode.CIC6105;
                break;

            case 6106:
                seed = BootCode.CIC6106;
                break;

            default:
                throw new Exception(String.Format("Unknown bootcode {0}", bootcode));
            }

            t1 = t2 = t3 = t4 = t5 = t6 = seed;

            var csum1 = romContents.GetSegment(0x1000, 0x100000);

            for (i = 0; i < csum1.Count; i += 4)
            {
                UInt32 word = Utilities.ReadU32(csum1, i);

                d = word;

                if ((t6 + d) < t6)
                {
                    t4++;
                }

                t6 += d;
                t3 ^= d;
                r   = ROL(d, (int)(d & 0x1F));

                t5 += r;

                if (t2 > d)
                {
                    t2 ^= r;
                }
                else
                {
                    t2 ^= t6 ^ d;
                }

                if (bootcode == 6105)
                {
                    t1 += Utilities.ReadU32(romContents, 0x40 + 0x0710 + (i & 0xFF)) ^ d;
                }
                else
                {
                    t1 += t5 ^ d;
                }
            }

            switch (bootcode)
            {
            case 6103:
                crc[0] = (t6 ^ t4) + t3;
                crc[1] = (t5 ^ t2) + t1;
                break;

            case 6106:
                crc[0] = (t6 * t4) + t3;
                crc[1] = (t5 * t2) + t1;
                break;

            default:
                crc[0] = t6 ^ t4 ^ t3;
                crc[1] = t5 ^ t2 ^ t1;
                break;
            }

            return(crc.ToImmutableArray());
        }
コード例 #20
0
 public static void Day8()
 {
     Console.WriteLine(BootCode.Run(File.ReadAllText(@"..\..\..\..\Gamepad\Boot.code")));
 }