My solutions for Advent of Code.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
adventofcode/2019/Day2/Part1.cs

57 lines
1.6 KiB

5 years ago
using System;
using System.IO;
namespace Part1 {
public class vars{
public enum intcode : int {
ADD = 1,
MULT = 2,
STOP = 99
}
}
public class compile {
5 years ago
public int NEXT = 4; // steps per opcode
5 years ago
public vars.intcode getOpcode( int opcode ) {
return (vars.intcode)opcode; // return the opcode, if 1 then return "ADD" etc
}
5 years ago
public bool isOpcode( int i ) {
return false;
}
public int[] runopcode( int[] array, int i, vars.intcode opcode ) {
int writepos = array[array[i+3]]; // get the write pos
5 years ago
if( opcode == vars.intcode.ADD ) {
5 years ago
int sum = array[array[i+1]] + array[array[i+2]]; // get the two values
Console.WriteLine( "ADD (" + i.ToString() + ") " + "Sum=" + sum.ToString() + " at i=" + array[i+3].ToString() );
array[writepos] = sum; // write the value
} else if( opcode == vars.intcode.MULT ) {
int product = array[array[i+1]] * array[array[i+2]]; // get the two values
Console.WriteLine( "MULT (" + i.ToString() + ") " + "Product=" + product.ToString() + " at i=" + array[i+3].ToString() );
array[writepos] = product; // write the value
} else if ( opcode == vars.intcode.STOP ) {
5 years ago
}
5 years ago
return array;
5 years ago
}
public int[] intcode( int[] input ) {
int[] output = input; // make an instanse of the input where we can change stuff
5 years ago
//int opcode_count = 0;
for( int i = 0; i < output.Length; i+= NEXT ) {
Console.WriteLine(i);
5 years ago
vars.intcode opcode = getOpcode(input[i]);
5 years ago
output = runopcode( output, i, opcode );
//Console.WriteLine( getOpcode(output[i]).ToString() + " | " + i.ToString() );
5 years ago
}
return output;
}
}
}