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, true); Dictionary <(long, long), TileId> tiles = new(); long score = PlayGame(intCode, tiles); int count = tiles.Where(kvp => kvp.Value == TileId.Block).Count(); Console.WriteLine($"Part One: {count}"); intCode = new IntCode(input.Split(",").ToList().Select(o => long.Parse(o)).ToArray(), true, true); intCode.Opcodes[0] = 2; tiles = new(); long currentScore = PlayGame(intCode, tiles); Console.WriteLine($"Part Two: {currentScore}"); }
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}"); }
public static long PlayGame(IntCode intCode, Dictionary <(long, long), TileId> tiles)
public static void Execute(string filename) { string input = File.ReadAllText(filename); IntCode[] intCodes = new IntCode[50]; for (int i = 0; i < intCodes.Length; i++) { intCodes[i] = new IntCode(input.Split(",").ToList().Select(o => long.Parse(o)).ToArray(), true); intCodes[i].Resume(new List <int> { i, -1 }); } long x = 0; long y = 0; long comp = 0; bool first = true; long NATX = 0; long NATY = 0; HashSet <long> NATYs = new HashSet <long>(); while (true) { for (int i = 0; i < intCodes.Length; i++) { intCodes[i].Resume(); comp = intCodes[i].Compute(); if (intCodes[i].WaitForInput) { continue; } intCodes[i].Resume(); x = intCodes[i].Compute(); intCodes[i].Resume(); y = intCodes[i].Compute(); if (comp < 50) { intCodes[comp].Resume(new List <long> { x, y }); } else if (comp == 255) { if (first) { Console.WriteLine($"Part One: {y}"); first = false; } NATX = x; NATY = y; } } if (intCodes.All(i => i.WaitForInput == true)) { if (!NATYs.Add(NATY)) { break; } intCodes[0].Resume(new List <long> { NATX, NATY }); } } Console.WriteLine($"Part Two: {y}"); }