public static void Execute(string filename) { List <string> input = File.ReadAllLines(filename).ToList(); foreach (string intcode in input) { for (int noun = 0; noun < 100; noun++) { for (int verb = 0; verb < 100; verb++) { long[] opcodes = intcode.Split(",").ToList().Select(o => long.Parse(o)).ToArray(); opcodes[1] = noun; opcodes[2] = verb; IntCode intCode = new IntCode(opcodes); intCode.Compute(); if (noun == 12 && verb == 2) { Console.WriteLine($"Part One: {intCode.Opcodes[0]}"); } if (intCode.Opcodes[0] == 19690720) { Console.WriteLine($"Part Two: 100 * {noun} + {verb} = {100*noun + verb}"); return; } } } } }
public static void Execute(string filename) { List <string> input = File.ReadAllLines(filename).ToList(); foreach (string intcode in input) { long[] opcodes = intcode.Split(",").ToList().Select(o => long.Parse(o)).ToArray(); IntCode partOne = new IntCode(opcodes); Console.WriteLine($"Part One: {partOne.Compute(1)}"); IntCode intCode = new IntCode(opcodes); Console.WriteLine($"Part Two: {intCode.Compute(5)}"); } }
public static void Execute(string filename) { List <string> input = File.ReadAllLines(filename).ToList(); string inputString = string.Join("", input); IntCode partOne = new IntCode(inputString.Split(",").ToList().Select(o => long.Parse(o)).ToArray()); long result = partOne.Compute(1); Console.WriteLine($"Part One: {result}"); IntCode intCode = new IntCode(inputString.Split(",").ToList().Select(o => long.Parse(o)).ToArray()); result = intCode.Compute(2); Console.WriteLine($"Part Two: {result}"); }
public static long RunIntCode(List <int> instructions, IntCode intCode) { intCode.Restart(); intCode.Resume(instructions); long val = 0; while (!intCode.Halted) { intCode.Resume(); val = intCode.Compute(); if (!intCode.Halted) { Console.Write((char)val); } } return(val); }
public static void Execute(string filename) { string input = File.ReadAllText(filename); IntCode intCode = new IntCode(input.Split(",").ToList().Select(o => long.Parse(o)).ToArray(), true); List <string> userCommands = new List <string> { "north", "north", "east", "take antenna", "west", "south", "east", "take cake", "west", "south", "west", "west", "west", "take coin", "east", "east", "east", "east", "east", "east", "east", "take boulder", "north", "east" }; List <int> asciiCommand = ConvertInstructions(userCommands); intCode.Resume(asciiCommand); while (!intCode.Halted) { intCode.Resume(); Console.Write((char)intCode.Compute()); } }
public static void Execute(string filename) { string input = File.ReadAllText(filename); IntCode intCode = new IntCode(input.Split(",").ToList().Select(o => long.Parse(o)).ToArray(), true); List <List <char> > map = new(); map.Add(new List <char>()); while (!intCode.Halted) { intCode.Resume(); char c = (char)(intCode.Compute()); if (c == '\n') { if (map.Last().Count > 0) { map.Add(new List <char>()); } } else { map.Last().Add(c); } } map.RemoveAt(map.Count - 1); int count = 0; char[,] floorplan = new char[map.Count(), map.First().Count()]; for (int i = 0; i < floorplan.GetLength(0); i++) { for (int j = 0; j < floorplan.GetLength(1); j++) { floorplan[i, j] = map[i][j]; if (map[i][j] == '#') { count++; } } } DrawMap(floorplan); Console.WriteLine($"Part One: {SumAlignmentParameters(floorplan)}"); Console.WriteLine(count); intCode = new IntCode(input.Split(",").ToList().Select(o => long.Parse(o)).ToArray(), true); intCode.Opcodes[0] = 2; // A, B,A,B,C,C,B,A,C,A\n // L,10,R,8,R,6,R,10\n // L,12,R,8,L,12\n // L,10,R,8,R,8\n // n\n List <int> A = new List <int>() { 76, 44, 49, 48, 44, 82, 44, 56, 44, 82, 44, 54, 44, 82, 44, 49, 48, 10 }; List <int> B = new List <int>() { 76, 44, 49, 50, 44, 82, 44, 56, 44, 76, 44, 49, 50, 10 }; List <int> C = new List <int>() { 76, 44, 49, 48, 44, 82, 44, 56, 44, 82, 44, 56, 10 }; List <int> routine = new List <int>() { 65, 44, 66, 44, 65, 44, 66, 44, 67, 44, 67, 44, 66, 44, 65, 44, 67, 44, 65, 10 }; intCode.Resume(routine); intCode.Resume(A); intCode.Resume(B); intCode.Resume(C); intCode.Resume(new List <int>() { 110, 10 }); int iteration = 0; long output = 0; while (!intCode.Halted) { iteration++; output = intCode.Compute(); if (intCode.Paused) { Console.Write($"{(char)output}"); //break; } intCode.Resume(); } Console.WriteLine(); Console.WriteLine($"Part Two: {output}"); }
public static void Execute(string filename) { string input = File.ReadAllText(filename); IntCode intCode = new IntCode(input.Split(",").ToList().Select(o => long.Parse(o)).ToArray(), true); char[,] tractorBeam = new char[50, 50]; int count = 0; int lastRow = 0; int previousCountPound = 1; int previousPoundStart = 0; for (int i = 0; i < tractorBeam.GetLength(0); i++) { int countPound = 0; bool foundPound = false; int iterations = i <= 5 ? 10 : previousCountPound * 4; previousCountPound = 0; for (int j = previousPoundStart; j < previousPoundStart + iterations && j < tractorBeam.GetLength(1); j++) { if (j > 0 && tractorBeam[i, j - 1] == '.' && foundPound) { break; } intCode.Restart(); intCode.Resume(new List <int>() { j, i }); long x = intCode.Compute(); switch (x) { case 0: tractorBeam[i, j] = '.'; break; case 1: if (!foundPound) { previousPoundStart = j; } previousCountPound++; tractorBeam[i, j] = '#'; countPound++; foundPound = true; count++; break; default: throw new ArgumentException($"Unknown coordinate {i},{j}"); } } if (countPound == 0 && i > 5) { lastRow = i; break; } } DrawMap(tractorBeam, lastRow); Console.WriteLine($"Part One: {count}"); previousPoundStart = 1000; int row = -1; int col = -1; for (int i = 731; i < 10_000; i++) { for (int j = previousPoundStart; j < previousPoundStart + 100; j++) { intCode.Restart(); intCode.Resume(new List <int>() { j, i }); if (intCode.Compute() == 1) { intCode.Restart(); previousPoundStart = j; intCode.Resume(new List <int>() { j + 99, i - 99 }); // 100 rows up, 100 rows to the right if (intCode.Compute() == 1) { row = i - 99; col = j; } break; } } if (row != -1 && col != -1) { break; } } Console.WriteLine($"Part Two: {col}*10000+{row} = {col*10_000+row}"); }