Fixing the intcode compiler thing

master
gnomerd 5 years ago
parent 6a685af00c
commit 5b926491c4
  1. 40
      2019/Day2/Part1.cs
  2. 17
      2019/Day2/Part2.cs
  3. 39
      2019/Day2/Program.cs
  4. BIN
      2019/Day2/bin/Debug/netcoreapp2.2/Day2.dll
  5. BIN
      2019/Day2/bin/Debug/netcoreapp2.2/Day2.pdb
  6. 2
      2019/Day2/input.txt
  7. BIN
      2019/Day2/obj/Debug/netcoreapp2.2/Day2.dll
  8. BIN
      2019/Day2/obj/Debug/netcoreapp2.2/Day2.pdb

@ -24,40 +24,50 @@ namespace Part1 {
} }
public dynamic runopcode( int[] array, int i, vars.intcode opcode ) { public dynamic runopcode( int[] array, int i, vars.intcode opcode ) {
int writepos; int[] output = array; // CREATE NEW INSTANCE
int writepos = output[i+3];
int mod1 = output[ output[i+1] ];
int mod2 = output[ output[i+2] ];
if( opcode == vars.intcode.ADD ) { if( writepos >= output.Length ) {
Console.WriteLine( "[Warning] Tried to write at non-existent address ({0}/{1})", writepos, output.Length );
writepos = output.Length - 1; // just write it at the end of the program
}
int sum = array[array[i+1]] + array[array[i+2]]; // get the two values Console.WriteLine( "Mod1: {0}, Mod2: {1}, Writepos: {2}, mod1loc: {3}, mod2loc: {4}", mod1, mod2, writepos, output[i+1], output[i+2] );
writepos = Math.Clamp( array[i+3], 0, array.Length - 1 ); // get the write pos
Debug( "ADD (" + i.ToString() + "/" + array.Length.ToString() + ") " + "Sum=" + sum.ToString() + " at i=" + writepos.ToString() );
array[writepos] = sum; // write the value if( opcode == vars.intcode.ADD ) {
} else if( opcode == vars.intcode.MULT ) { Debug( "ADD (" + i.ToString() + "/" + output.Length.ToString() + ") " + "Sum=" + (mod1 + mod2).ToString() + " at i=" + writepos.ToString() );
output[writepos] = mod1 + mod2; // write the value
int product = array[array[i+1]] * array[array[i+2]]; // get the product } else if( opcode == vars.intcode.MULT ) {
writepos = Math.Clamp( array[i+3], 0, array.Length - 1 );
Debug( "MULT (" + i.ToString() + "/" + array.Length.ToString() + ") " + "Product=" + product.ToString() + " at i=" + writepos.ToString() );
array[writepos] = product; // write the value Debug( "MULT (" + i.ToString() + "/" + output.Length.ToString() + ") " + "Product=" + (mod1 * mod2).ToString() + " at i=" + writepos.ToString() );
output[writepos] = mod1 * mod2; // write the value
} else if ( opcode == vars.intcode.STOP ) { } else if ( opcode == vars.intcode.STOP ) {
Debug( "STOP (" + i.ToString() + "/" + array.Length.ToString() + ")" ); Debug( "STOP (" + i.ToString() + "/" + output.Length.ToString() + ")" );
} }
return array; return output;
} }
public int[] intcode( int[] input ) { public int[] intcode( int[] input ) {
int[] output = input; // make an instanse of the input where we can change stuff int[] output = input; // make an instanse of the input where we can change stuff
Debug( "\n----Running Intcode----" );
Console.Write( "\nRunning: " );
for( int c = 0; c < input.Length; c++ ){ Console.Write( input[c].ToString() + "," ); }
Debug( "\n----DEBUG----" );
for( int i = 0; i < output.Length; i+= NEXT ) { for( int i = 0; i < output.Length; i+= NEXT ) {
if( i+3 < output.Length ) {
vars.intcode opcode = getOpcode(input[i]); vars.intcode opcode = getOpcode(input[i]);
output = runopcode( output, i, opcode ); output = runopcode( output, i, opcode );
}
if( (int)output[i] == 99 ) { break; } if( (int)output[i] == 99 ) { break; }
} }
Debug( "----Intcode finished----\n" ); Debug( "----END-OF-DEBUG----\n" );
return output; return output;
} }
} }

@ -4,29 +4,28 @@ namespace Part2 {
public class calcInputs { public class calcInputs {
public void bruteforce( int[] input, int find ) { public void bruteforce( int[] input, int find ) {
int noun, verb; int noun, verb;
int[] input_c = input; // keep a version of the original int[] input_c; // keep a version of the original
int[] output = new int[input_c.Length]; int[] output = new int[input.Length];
bool success = false; bool success = false;
int min = 0; // min and max for the input values int min = 0; // min and max for the input values
int max = 99; // int max = 6; //
Part1.compile compile = new Part1.compile(); Part1.compile compile = new Part1.compile();
for( int i = 0; i < max + 1; i++ ) { for( int i = 0; i < max + 1; i++ ) {
input = input_c; // reset the intcode input_c = input; // reset the intcode
noun = Math.Clamp( i, min, max ); noun = Math.Clamp( i, min, max );
verb = Math.Clamp( i, min, max ); verb = Math.Clamp( i, min, max );
input[1] = noun; input_c[1] = noun;
input[2] = verb; input_c[2] = verb;
Console.WriteLine( "Checking: {0}, {1} for {2}", noun, verb, find ); output = compile.intcode( input_c );
output = compile.intcode( input );
Console.WriteLine( input[0] ); Console.WriteLine( input_c[0] );
if( output[0] == find ) { if( output[0] == find ) {
Console.WriteLine( "({0}) Found: {1}, {2}", find, noun, verb ); Console.WriteLine( "({0}) Found: {1}, {2}", find, noun, verb );
success = true; success = true;

@ -13,29 +13,34 @@ namespace Day2 {
Part1.compile compile = new Part1.compile(); Part1.compile compile = new Part1.compile();
//// Part 1 stuff //// // //// Part 1 stuff ////
intcodes[1] = 12; // intcodes[1] = 12;
intcodes[2] = 2; // intcodes[2] = 2;
output = compile.intcode( intcodes ); // run the intcode program // output = compile.intcode( intcodes ); // run the intcode program
Console.WriteLine("\n--Intcode result--");
for( int i = 0; i < output.Length; i++ ) {
if( i != output.Length - 1 ) {
Console.Write( output[i].ToString() + "," );
} else {
Console.Write( output[i].ToString() + "\n" );
}
}
Console.WriteLine( "--End of Intcode result--\n" );
Console.WriteLine( "Part 1 result: " + output[0].ToString() ); // get the pos 0 // Console.WriteLine("\n--Intcode result--");
// for( int i = 0; i < output.Length; i++ ) {
// if( i != output.Length - 1 ) {
// Console.Write( output[i].ToString() + "," );
// } else {
// Console.Write( output[i].ToString() + "\n" );
// }
// }
// Console.WriteLine( "--End of Intcode result--\n" );
// Console.WriteLine( "Part 1 result: " + output[0].ToString() ); // get the pos 0
//// Part 2 stuff //// //// Part 2 stuff ////
Part2.calcInputs calcInputs = new Part2.calcInputs(); Part2.calcInputs calcInputs = new Part2.calcInputs();
calcInputs.bruteforce( intcodes, 9801 ); int[] intcodes2 = intcodes; // make a new instance of it
for( int c = 0; c < intcodes2.Length; c++ ){
Console.Write( intcodes2[c].ToString() + "," );
}
Console.WriteLine("");
calcInputs.bruteforce( intcodes2, 16 );
} }
} }
} }

@ -1 +1 @@
2,0,0,0,99,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 2,7,8,0,99,4,4,2,1
Loading…
Cancel
Save