public int WallFollower(AsciiComputer cpu) { //cpu.EchoInput = true; //cpu.EchoOutput = true; var gameOutput = cpu.Run(); var facing = Directions.north; bool securityVisited = false; while (true) { TakeAllItems(cpu, gameOutput); if (gameOutput.Contains("Security Checkpoint")) { if (!securityVisited) { // first time here, turn around to get all items facing = (Directions)(((int)facing + 2) % 4); securityVisited = true; } else { return(Force(cpu)); } } else { var doors = GetDoors(gameOutput); facing = TakeLeft(facing, doors); } gameOutput = cpu.Run(facing.ToString()); //Console.ReadKey(true); } }
public int GetAnswer1(long[] program) { var cpu = new IntCodeComputer(program); var asciiComputer = new AsciiComputer(cpu); return(WallFollower(asciiComputer)); }
private bool ForceEntry(AsciiComputer cpu, ImmutableArray <string> inventory, ref int code) { // simple brute force try all combinations foreach (var item in inventory) { cpu.Run("drop " + item); var res = TryGetCode(cpu, ref code); if (res == 0) { return(true); } if (res > 0) { if (ForceEntry(cpu, inventory.Remove(item), ref code)) { return(true); } } cpu.Run("take " + item); } code = 0; return(false); }
private int Force(AsciiComputer cpu) { var all = GetInventory(cpu); int code = 0; ForceEntry(cpu, all.ToImmutableArray(), ref code); return(code); }
public void TakeAllItems(AsciiComputer cpu, string gameOutput) { var itemsHere = GetList(gameOutput, "Items here:"); foreach (var item in itemsHere.Except(UnsafeItems)) { cpu.Run("take " + item); } }
private static long GetHullDamage(int[] input, string[] script) { var cpu = new IntCodeComputer(input); var asc = new AsciiComputer(cpu); foreach (var line in script) { var r = asc.Run(line.Trim()); //Console.Write(r); } //Console.WriteLine(cpu.ToString()); return(asc.ResultCode); }
public void Repl(long[] program) { while (true) { Console.WriteLine("****** Start New Game! ******"); var cpu = new IntCodeComputer(program); var asciiComputer = new AsciiComputer(cpu); Console.Write(asciiComputer.Run()); while (!cpu.Finished) { var input = Console.ReadLine(); if (input == "exit") { return; } Console.Write(asciiComputer.Run(input)); } } }
private int TryGetCode(AsciiComputer cpu, ref int code) { var result = cpu.Run("south"); if (!result.Contains("Alert!")) { code = int.Parse(Regex.Match(result, "\\d+").Value); return(0); } if (result.Contains("lighter")) { return(1); } if (result.Contains("heavier")) { return(-1); } throw new InvalidOperationException("unexpected result"); }
private string[] GetInventory(AsciiComputer cpu) { var result = cpu.Run("inv"); return(GetList(result, "Items in your inventory:")); }