Exemplo n.º 1
0
 /// <summary>
 /// Accepts a single input, applying it to the memory address stored at the single parameter.
 /// </summary>
 public static IntCodeState ApplyInput(this IntCodeState state, long input)
 {
     return(new IntCodeState(state)
     {
         State = state.SetAt(state.WriteParameter(1), input),
         Index = state.Index + 2
     });
 }
Exemplo n.º 2
0
        /// <summary>
        /// Compares the first two values using the given comparator.
        /// Sets the value in the index given by param 3 to either 1 or 0, reflecting the result of the comparison
        /// </summary>
        public static IntCodeState Compare(this IntCodeState state, Func <long, long, bool> comparator)
        {
            var valueToStore = comparator(state.ReadParameter(1), state.ReadParameter(2)) ? 1 : 0;

            return(new IntCodeState(state)
            {
                State = state.SetAt(state.WriteParameter(3), valueToStore),
                Index = state.Index + 4
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// Applies an arbitrary operation to the first *TWO* parameters, outputting based on the third parameter.
        /// Will need extending if it needs to be applied to more than two parameters.
        /// </summary>
        public static IntCodeState ApplyOperation(this IntCodeState state, Func <long, long, long> operation)
        {
            var operationResult = operation(state.ReadParameter(1), state.ReadParameter(2));

            return(new IntCodeState(state)
            {
                State = state.SetAt(state.WriteParameter(3), operationResult),
                Index = state.Index + 4
            });
        }