Ejemplo n.º 1
0
        public string PartTwoSolve(string input)
        {
            var computer = new IntcodeComputer();
            var state    = new IntcodeState
            {
                Output             = new List <string>(),
                Input              = new Queue <int>(),
                Memory             = _parser.ParseProgram(input),
                InstructionPointer = 0
            };

            computer.Compute(state);
            return(state.Memory.Select(d => d.Value).Select(i => i.ToString()).Aggregate((output, next) => $"{output},{next}"));
        }
Ejemplo n.º 2
0
        private static IntcodeState GetFreshState(IReadOnlyDictionary <long, long> rom)
        {
            var state = new IntcodeState
            {
                Output             = new List <string>(),
                Input              = new Queue <int>(),
                Memory             = rom.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
                InstructionPointer = 0,
                BreakOnOutput      = false,
                Halted             = false
            };

            return(state);
        }
Ejemplo n.º 3
0
        public string PartTwoSolve(string input)
        {
            var ram      = _parser.ParseProgram(input);
            var computer = new IntcodeComputer();
            var state    = new IntcodeState
            {
                Output             = new List <string>(),
                Input              = new Queue <int>(),
                Memory             = ram,
                InstructionPointer = 0
            };

            state.Input.Enqueue(5);
            computer.Compute(state);

            return(string.Join("\n", state.Output));
        }
Ejemplo n.º 4
0
        public string PartOneSolve(string input, int?programInput)
        {
            var ram   = _parser.ParseProgram(input);
            var state = new IntcodeState
            {
                Memory = ram
            };

            if (programInput != null)
            {
                state.Input.Enqueue(programInput.Value);
            }

            var computer = new IntcodeComputer();

            computer.Compute(state);

            return(state.Output.Aggregate((current, next) => $"{current},{next}"));
        }
Ejemplo n.º 5
0
        public void use_new_instructions_correctly(string input, string expected)
        {
            var programString =
                "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99";
            var volatileInstructions = programString.Split(",")
                                       .Select(long.Parse)
                                       .Select((value, index) => new KeyValuePair <long, long>(index, value));
            var ram      = new Dictionary <long, long>(volatileInstructions);
            var computer = new IntcodeComputer();

            var state = new IntcodeState
            {
                Output             = new List <string>(),
                Input              = new Queue <int>(),
                Memory             = ram,
                InstructionPointer = 0
            };

            state.Input.Enqueue(int.Parse(input));
            computer.Compute(state);

            state.Output.First().Should().Be(expected);
        }