diff --git a/2019/Day2/Part1.cs b/2019/Day2/Part1.cs index bb584cc..48c5198 100644 --- a/2019/Day2/Part1.cs +++ b/2019/Day2/Part1.cs @@ -24,40 +24,50 @@ namespace Part1 { } 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 - 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() ); + 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 ) { - array[writepos] = sum; // write the value + Debug( "ADD (" + i.ToString() + "/" + output.Length.ToString() + ") " + "Sum=" + (mod1 + mod2).ToString() + " at i=" + writepos.ToString() ); + output[writepos] = mod1 + mod2; // write the value } else if( opcode == vars.intcode.MULT ) { - - int product = array[array[i+1]] * array[array[i+2]]; // get the product - 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 ) { - Debug( "STOP (" + i.ToString() + "/" + array.Length.ToString() + ")" ); + Debug( "STOP (" + i.ToString() + "/" + output.Length.ToString() + ")" ); } - return array; + return output; } public int[] intcode( int[] input ) { 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 ) { - vars.intcode opcode = getOpcode(input[i]); - output = runopcode( output, i, opcode ); + if( i+3 < output.Length ) { + vars.intcode opcode = getOpcode(input[i]); + output = runopcode( output, i, opcode ); + } if( (int)output[i] == 99 ) { break; } } - Debug( "----Intcode finished----\n" ); + Debug( "----END-OF-DEBUG----\n" ); return output; } } diff --git a/2019/Day2/Part2.cs b/2019/Day2/Part2.cs index 2c32aa6..fe24673 100644 --- a/2019/Day2/Part2.cs +++ b/2019/Day2/Part2.cs @@ -4,29 +4,28 @@ namespace Part2 { public class calcInputs { public void bruteforce( int[] input, int find ) { int noun, verb; - int[] input_c = input; // keep a version of the original - int[] output = new int[input_c.Length]; + int[] input_c; // keep a version of the original + int[] output = new int[input.Length]; bool success = false; int min = 0; // min and max for the input values - int max = 99; // + int max = 6; // Part1.compile compile = new Part1.compile(); 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 ); verb = Math.Clamp( i, min, max ); - input[1] = noun; - input[2] = verb; + input_c[1] = noun; + input_c[2] = verb; - Console.WriteLine( "Checking: {0}, {1} for {2}", noun, verb, find ); - output = compile.intcode( input ); + output = compile.intcode( input_c ); - Console.WriteLine( input[0] ); + Console.WriteLine( input_c[0] ); if( output[0] == find ) { Console.WriteLine( "({0}) Found: {1}, {2}", find, noun, verb ); success = true; diff --git a/2019/Day2/Program.cs b/2019/Day2/Program.cs index d490361..8e85346 100644 --- a/2019/Day2/Program.cs +++ b/2019/Day2/Program.cs @@ -13,29 +13,34 @@ 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 ); // run the intcode program - 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 //// 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 ); } } } diff --git a/2019/Day2/bin/Debug/netcoreapp2.2/Day2.dll b/2019/Day2/bin/Debug/netcoreapp2.2/Day2.dll index 49a8f62..a1bbfc9 100644 Binary files a/2019/Day2/bin/Debug/netcoreapp2.2/Day2.dll and b/2019/Day2/bin/Debug/netcoreapp2.2/Day2.dll differ diff --git a/2019/Day2/bin/Debug/netcoreapp2.2/Day2.pdb b/2019/Day2/bin/Debug/netcoreapp2.2/Day2.pdb index d3a5ccc..977c939 100644 Binary files a/2019/Day2/bin/Debug/netcoreapp2.2/Day2.pdb and b/2019/Day2/bin/Debug/netcoreapp2.2/Day2.pdb differ diff --git a/2019/Day2/input.txt b/2019/Day2/input.txt index b873674..81b46d8 100644 --- a/2019/Day2/input.txt +++ b/2019/Day2/input.txt @@ -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 \ No newline at end of file +2,7,8,0,99,4,4,2,1 \ No newline at end of file diff --git a/2019/Day2/obj/Debug/netcoreapp2.2/Day2.dll b/2019/Day2/obj/Debug/netcoreapp2.2/Day2.dll index 49a8f62..a1bbfc9 100644 Binary files a/2019/Day2/obj/Debug/netcoreapp2.2/Day2.dll and b/2019/Day2/obj/Debug/netcoreapp2.2/Day2.dll differ diff --git a/2019/Day2/obj/Debug/netcoreapp2.2/Day2.pdb b/2019/Day2/obj/Debug/netcoreapp2.2/Day2.pdb index d3a5ccc..977c939 100644 Binary files a/2019/Day2/obj/Debug/netcoreapp2.2/Day2.pdb and b/2019/Day2/obj/Debug/netcoreapp2.2/Day2.pdb differ