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

65 lines
1.8 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 {
public const int NEXT = 4; // steps per opcode
public bool writedebug = true;
5 years ago
5 years ago
public vars.intcode getOpcode( int opcode ) {
return (vars.intcode)opcode; // return the opcode, if 1 then return "ADD" etc
}
public void Debug( string txt ) {
if( writedebug == true ) {
Console.WriteLine( txt );
}
5 years ago
}
public dynamic runopcode( int[] array, int i, vars.intcode opcode ) {
int writepos;
5 years ago
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
writepos = array[i+3]; // get the write pos
Debug( "ADD (" + i.ToString() + "/" + array.Length.ToString() + ") " + "Sum=" + sum.ToString() + " at i=" + writepos.ToString() );
5 years ago
array[writepos] = sum; // write the value
} else if( opcode == vars.intcode.MULT ) {
5 years ago
int product = array[array[i+1]] * array[array[i+2]]; // get the two values
writepos = array[i+3];
Debug( "MULT (" + i.ToString() + "/" + array.Length.ToString() + ") " + "Product=" + product.ToString() + " at i=" + writepos.ToString() );
5 years ago
array[writepos] = product; // write the value
5 years ago
} else if ( opcode == vars.intcode.STOP ) {
Debug( "STOP (" + i.ToString() + "/" + array.Length.ToString() + ")" );
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
Console.WriteLine( "\n----Running Intcode----" );
for( int i = 0; i < output.Length; i+= NEXT ) {
5 years ago
vars.intcode opcode = getOpcode(input[i]);
5 years ago
output = runopcode( output, i, opcode );
if( (int)output[i] == 99 ) { break; }
5 years ago
}
Console.WriteLine( "----Intcode finished----\n" );
5 years ago
return output;
}
}
}