Solved Day 2 Part 2

master
gnomerd 5 years ago
parent 5b926491c4
commit a068b3e5aa
  1. 31
      2019/Day2/Part1.cs
  2. 39
      2019/Day2/Part2.cs
  3. 40
      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

@ -11,20 +11,21 @@ namespace Part1 {
}
public class compile {
public const int NEXT = 4; // steps per opcode
public bool writedebug = !false;
public vars.intcode getOpcode( int opcode ) {
return (vars.intcode)opcode; // return the opcode, if 1 then return "ADD" etc
}
public void Debug( string txt ) {
public void Debug( string txt, bool writedebug ) {
if( writedebug == true ) {
Console.WriteLine( txt );
}
}
public dynamic runopcode( int[] array, int i, vars.intcode opcode ) {
int[] output = array; // CREATE NEW INSTANCE
public dynamic runopcode( int[] array, int i, vars.intcode opcode, bool debug ) {
int[] output = new int[array.Length];
Array.Copy( array, output, array.Length );
int writepos = output[i+3];
int mod1 = output[ output[i+1] ];
int mod2 = output[ output[i+2] ];
@ -34,40 +35,42 @@ namespace Part1 {
writepos = output.Length - 1; // just write it at the end of the program
}
Console.WriteLine( "Mod1: {0}, Mod2: {1}, Writepos: {2}, mod1loc: {3}, mod2loc: {4}", mod1, mod2, writepos, output[i+1], output[i+2] );
//Console.WriteLine( "Mod1: {0}, Mod2: {1}, Writepos: {2}, mod1loc: {3}, mod2loc: {4}", mod1, mod2, writepos, output[i+1], output[i+2] );
if( opcode == vars.intcode.ADD ) {
Debug( "ADD (" + i.ToString() + "/" + output.Length.ToString() + ") " + "Sum=" + (mod1 + mod2).ToString() + " at i=" + writepos.ToString() );
Debug( "ADD (" + i.ToString() + "/" + output.Length.ToString() + ") " + "Sum=" + (mod1 + mod2).ToString() + " at i=" + writepos.ToString(), debug );
output[writepos] = mod1 + mod2; // write the value
} else if( opcode == vars.intcode.MULT ) {
Debug( "MULT (" + i.ToString() + "/" + output.Length.ToString() + ") " + "Product=" + (mod1 * mod2).ToString() + " at i=" + writepos.ToString() );
Debug( "MULT (" + i.ToString() + "/" + output.Length.ToString() + ") " + "Product=" + (mod1 * mod2).ToString() + " at i=" + writepos.ToString(), debug );
output[writepos] = mod1 * mod2; // write the value
} else if ( opcode == vars.intcode.STOP ) {
Debug( "STOP (" + i.ToString() + "/" + output.Length.ToString() + ")" );
Debug( "STOP (" + i.ToString() + "/" + output.Length.ToString() + ")", debug );
}
return output;
}
public int[] intcode( int[] input ) {
public int[] intcode( int[] input, bool debug ) {
int[] output = input; // make an instanse of the input where we can change stuff
Console.Write( "\nRunning: " );
for( int c = 0; c < input.Length; c++ ){ Console.Write( input[c].ToString() + "," ); }
if( debug == true ) {
Console.Write( "\nRunning: " );
for( int c = 0; c < input.Length; c++ ){ Console.Write( input[c].ToString() + "," ); }
}
Debug( "\n----DEBUG----" );
Debug( "\n----DEBUG----", debug );
for( int i = 0; i < output.Length; i+= NEXT ) {
if( i+3 < output.Length ) {
vars.intcode opcode = getOpcode(input[i]);
output = runopcode( output, i, opcode );
output = runopcode( output, i, opcode, debug );
}
if( (int)output[i] == 99 ) { break; }
}
Debug( "----END-OF-DEBUG----\n" );
Debug( "----END-OF-DEBUG----\n", debug );
return output;
}
}

@ -2,7 +2,7 @@ using System;
namespace Part2 {
public class calcInputs {
public void bruteforce( int[] input, int find ) {
public int bruteforce( int[] input, int find, bool debug ) {
int noun, verb;
int[] input_c; // keep a version of the original
int[] output = new int[input.Length];
@ -10,36 +10,41 @@ namespace Part2 {
bool success = false;
int min = 0; // min and max for the input values
int max = 6; //
int max = 99; //
int maxcombos = Convert.ToInt16( Math.Pow( (double)(max + 1), 2.0 ) );
Part1.compile compile = new Part1.compile();
for( int i = 0; i < max + 1; i++ ) {
input_c = input; // reset the intcode
Console.WriteLine("Bruteforcing...");
for( int c = 0; c < max + 1; c++ ) {
noun = Math.Clamp( c, min, max );
// check every verb with c (the noun)
for( int i = 0; i < max + 1; i++ ) {
input_c = input; // reset the intcode
verb = Math.Clamp( i, min, max );
noun = Math.Clamp( i, min, max );
verb = Math.Clamp( i, min, max );
input_c[1] = noun;
input_c[2] = verb;
input_c[1] = noun;
input_c[2] = verb;
output = compile.intcode( input_c, debug );
output = compile.intcode( input_c );
Console.WriteLine( input_c[0] );
if( output[0] == find ) {
Console.WriteLine( "({0}) Found: {1}, {2}", find, noun, verb );
success = true;
break;
if( output[0] == find ) {
Console.WriteLine( "\n({0}) Found: {1}, {2}", find, noun, verb );
success = true;
return (100 * noun) + verb;
}
}
if( success == true ) { break; }
}
Console.WriteLine("");
if( success == false ) {
Console.WriteLine( "Nothing found :(" );
return;
return -1;
}
return -1;
}
}
}

@ -13,34 +13,32 @@ namespace Day2 {
Part1.compile compile = new Part1.compile();
// //// Part 1 stuff ////
// intcodes[1] = 12;
// intcodes[2] = 2;
// 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" );
//// Part 1 stuff ////
intcodes[1] = 12;
intcodes[2] = 2;
output = compile.intcode( intcodes, false ); // 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( "Part 1 result: " + output[0].ToString() ); // get the pos 0
//// Part 2 stuff ////
Part2.calcInputs calcInputs = new Part2.calcInputs();
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 );
int res;
res = calcInputs.bruteforce( intcodes2, 19690720, false );
Console.WriteLine( "Part 2 Result: {0}", res );
}
}
}

@ -1 +1 @@
2,7,8,0,99,4,4,2,1
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,19,6,23,2,23,13,27,1,27,5,31,2,31,10,35,1,9,35,39,1,39,9,43,2,9,43,47,1,5,47,51,2,13,51,55,1,55,9,59,2,6,59,63,1,63,5,67,1,10,67,71,1,71,10,75,2,75,13,79,2,79,13,83,1,5,83,87,1,87,6,91,2,91,13,95,1,5,95,99,1,99,2,103,1,103,6,0,99,2,14,0,0
Loading…
Cancel
Save