public string SolveA() { var lines = File.ReadAllLines(Input, Encoding.UTF8); var program = Intcode.ParseInput(lines[0]); var state = new Intcode.State(program); var ascii = new StringBuilder(); while (Intcode.Step(state)) { var output = state.PopOutput(); if (output.HasValue) { var v = OutputToValue(output.Value); if (v.HasValue) { ascii.Append(v.Value); } } } var asciiText = ascii.ToString(); // OutputAscii(asciiText); var environment = asciiText.Split(new[] { newLine }); environment = environment.Where(e => e.Length > 0).ToArray(); var answer = AlignmentValue(environment); return(answer.ToString()); }
private bool ProveA1() { var input = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99"; var program = Intcode.ParseInput(input); var state = new Intcode.State(program); var outputs = new List <IntType>(); while (Intcode.Step(state)) { if (state.output.HasValue) { var output = state.PopOutput(); if (output.HasValue) { outputs.Add(output.Value); } } } var final = string.Join(",", outputs); if (final != input) { Console.WriteLine(); Console.WriteLine($"Input: {input}"); Console.WriteLine($"Final: {final}"); return(false); } return(true); }
public string SolveFor(int input) { var lines = File.ReadAllLines(Input, Encoding.UTF8); var program = Intcode.ParseInput(lines[0]); var state = new Intcode.State(program, input); var outputs = new List <IntType>(); while (Intcode.Step(state)) { if (state.output.HasValue) { var output = state.PopOutput(); if (output.HasValue) { outputs.Add(output.Value); } } } if (outputs.Count == 1) { return(outputs[0].ToString()); } // FAILED Console.WriteLine(string.Join(",", outputs)); throw new Exception("Day 9, Solve A failed"); }
public string execute(string[] instructions) { var lines = File.ReadAllLines(Input, Encoding.UTF8); var program = Intcode.ParseInput(lines[0]); var state = new Intcode.State(program); var input = string.Join('\n', instructions); var ascii = new List <long>(); var index = 0; while (Intcode.Step(state)) { var output = state.PopOutput(); if (output.HasValue) { ascii.Add(output.Value); } if (!state.input.HasValue && index < input.Length) { state.input = input[index++]; } } var lastVal = ascii[ascii.Count - 1]; if (lastVal < char.MaxValue) { var asciiText = ascii.Select(a => (char)a).Take(ascii.Count - 1); Console.WriteLine(string.Join("", asciiText)); return("Sad death"); } return(lastVal.ToString()); }
private string executeCommands(Intcode.State state, string[] commands, bool assumePrompt) { var lastLine = new StringBuilder(); var commandOutput = new StringBuilder(); var commandIndex = 0; var input = ""; if (assumePrompt) { commandIndex++; input = commands[0] + "\n"; } var index = 0; while (Intcode.Step(state)) { var output = state.PopOutput(); if (output.HasValue) { char c = (char)output.Value; fullTrace.Append(c); commandOutput.Append(c); switch (c) { case '\n': if (lastLine.ToString() == "Command?") { if (commandIndex < commands.Length) { input = commands[commandIndex++]; fullTrace.Append($"> {input}\n"); } else { // We are done return(commandOutput.ToString()); } input += '\n'; index = 0; } lastLine.Clear(); break; default: lastLine.Append(c); break; } } if (!state.input.HasValue && index < input.Length) { state.input = input[index++]; } } return(commandOutput.ToString()); }
public string Solve(bool isA) { var lines = File.ReadAllLines(Input, Encoding.UTF8); var program = Intcode.ParseInput(lines[0]); var startingColor = isA ? Color.Black : Color.White; var state = new Intcode.State(program, (long)startingColor); var hull = new Hull(); var robot = new Robot(); var outputs = new List <IntType>(); // 0 = paint // 1 = move var mode = 0; while (Intcode.Step(state)) { var output = state.PopOutput(); if (output.HasValue) { if (mode == 0) { // Paint hull.Paint(robot.location, (Color)output.Value); } else { // Move if (output.Value == 0) { robot.TurnLeft(); } else { robot.TurnRight(); } robot.MoveForward(); state.input = (long)hull.ColorAt(robot.location); } mode = (mode + 1) % 2; } } if (isA) { // Count up the doubled squares. return(hull.Painted().ToString()); } else { // Render it hull.Render(); return("Manual"); } }
private bool ProveA2() { var input = "1102,34915192,34915192,7,4,7,99,0"; var program = Intcode.ParseInput(input); var state = new Intcode.State(program); while (Intcode.Step(state)) { // nothing } return(state.PopOutput().Value.ToString().Length == 16); }
public string SolveB() { var lines = File.ReadAllLines(Input, Encoding.UTF8); var program = Intcode.ParseInput(lines[0]); program[0] = 2; var state = new Intcode.State(program); var main = ConvertToInstructions("A,B,B,C,C,A,B,B,C,A"); var A = ConvertToInstructions("R,4,R,12,R,10,L,12"); var B = ConvertToInstructions("L,12,R,4,R,12"); var C = ConvertToInstructions("L,12,L,8,R,10"); var total = new List <char>(); total.AddRange(main); total.Add(newLine); total.AddRange(A); total.Add(newLine); total.AddRange(B); total.Add(newLine); total.AddRange(C); total.Add(newLine); total.Add('n'); total.Add(newLine); int current = 0; var ascii = new StringBuilder(); long lastOutput = 0; while (Intcode.Step(state)) { if (!state.input.HasValue && current < total.Count) { state.input = total[current++]; } var output = state.PopOutput(); if (output.HasValue) { lastOutput = output.Value; var v = OutputToValue(output.Value); if (v.HasValue) { ascii.Append(v.Value); } } } var asciiText = ascii.ToString(); // OutputAscii(asciiText); return(lastOutput.ToString()); }
private IntType?RunProgram(ref IntType[] program, IntType input) { IntType finalOutput = -1; var state = new Intcode.State(program, input); while (Intcode.Step(state)) { // nothing // consume the output var tempOut = state.PopOutput(); if (tempOut.HasValue) { finalOutput = tempOut.Value; } } program = state.MemoryDump(program.Length); return(finalOutput); }
public string SolveB() { var lines = File.ReadAllLines(Input, Encoding.UTF8); var program = Intcode.ParseInput(lines[0]); program[0] = 2; var state = new Intcode.State(program); var outputs = new List <IntType>(); IntType score = 0; IntType paddlePos = 0; IntType ballPos = 0; while (Intcode.Step(state)) { var output = state.PopOutput(); if (output.HasValue) { outputs.Add(output.Value); if (outputs.Count == 3) { var x = outputs[0]; var y = outputs[1]; var other = outputs[2]; if (x == -1 && y == 0) { score = other; } else if (other == (IntType)Tile.Paddle) { paddlePos = x; } else if (other == (IntType)Tile.Ball) { ballPos = x; } // -1, 0, 1 for movement based on ball, paddle position. state.input = ballPos.CompareTo(paddlePos); outputs.Clear(); } } } return(score.ToString()); }
bool IsTractorOn(IntType[] program, int x, int y) { var state = new Intcode.State(program); state.input = x; while (Intcode.Step(state)) { if (!state.input.HasValue) { state.input = y; } var output = state.PopOutput(); if (output.HasValue) { return(output.Value != 0); } } throw new Exception("Whaaaa?"); }
public string SolveA() { var lines = File.ReadAllLines(Input, Encoding.UTF8); var program = Intcode.ParseInput(lines[0]); var state = new Intcode.State(program); var outputs = new List <IntType>(); while (Intcode.Step(state)) { if (state.output.HasValue) { var output = state.PopOutput(); if (output.HasValue) { outputs.Add(output.Value); } } } return(countBlocks(outputs).ToString()); }