示例#1
0
    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());
    }
示例#2
0
    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");
    }
示例#3
0
    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());
    }
示例#4
0
    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);
    }
示例#5
0
    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());
    }
示例#6
0
    public string SolveA()
    {
        var lines   = File.ReadAllLines(Input, Encoding.UTF8);
        var program = Intcode.ParseInput(lines[0]);
        var state   = new Intcode.State(program);
        var found   = FindIt(state, new List <Movement>());

        return(found.Count.ToString());
    }
示例#7
0
    private IntType[] RunProgram(IntType[] program)
    {
        var state = new Intcode.State(program);

        while (Intcode.Step(state))
        {
            // nothing
        }
        return(state.MemoryDump(program.Length));
    }
示例#8
0
    private string findTheCode()
    {
        var lines   = File.ReadAllLines(Input, Encoding.UTF8);
        var program = Intcode.ParseInput(lines[0]);
        var state   = new Intcode.State(program);

        var fullTrace = new StringBuilder();
        var lastLine  = new StringBuilder();

        string[] initializeCommands = File.ReadAllLines(CommandFile, Encoding.UTF8);
        string[] items = File.ReadAllLines(ItemFile, Encoding.UTF8);

        var dropAll = items.Select(i => "drop " + i).ToArray();

        // do the pre-canned commands
        var initializeOutput = executeCommands(state, initializeCommands, false);

        // Assume the answer is somewhere between 2 and 6 tries.
        var combinations = new List <IEnumerable <string> >();

        combinations.AddRange(Util.GetKCombs(items, 2));
        combinations.AddRange(Util.GetKCombs(items, 3));
        combinations.AddRange(Util.GetKCombs(items, 4));
        combinations.AddRange(Util.GetKCombs(items, 5));
        combinations.AddRange(Util.GetKCombs(items, 6));

        foreach (var combo in combinations)
        {
            // Drop all first
            var dropAllOutput = executeCommands(state, dropAll, true);

            // Generate commands for the items
            var comboCommands = combo.Select(i => "take " + i).ToArray();
            comboCommands = comboCommands.Append("east").ToArray();
            var commandOutput = executeCommands(state, comboCommands, true);

            if (!commandOutput.Contains("Alert!"))
            {
                // Find the number:
                var preceeding = "You should be able to get in by typing ";
                var index      = commandOutput.IndexOf(preceeding);
                var startIndex = index + preceeding.Length;
                var endIndex   = commandOutput.IndexOf(" ", startIndex);
                var answer     = commandOutput.Substring(startIndex, endIndex - startIndex);
                return(answer.ToString()); // 319815680
            }
        }

        // Interactive:
        while (true)
        {
            var input = Console.ReadLine();
            executeCommands(state, new[] { input }, true);
        }
    }
示例#9
0
    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");
        }
    }
示例#10
0
    private bool ProveA3()
    {
        var input   = "104,1125899906842624,99";
        var program = Intcode.ParseInput(input);
        var state   = new Intcode.State(program);

        while (Intcode.Step(state))
        {
            // nothing
        }
        return(state.output == 1125899906842624);
    }
示例#11
0
    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);
    }
示例#12
0
    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());
    }
示例#13
0
    void MapIt(Intcode.State initialState, Point currentLoc, ref HashSet <Point> cells, ref Point oxygenCell, int depth)
    {
        foreach (Movement m in Enum.GetValues(typeof(Movement)))
        {
            log($"{currentLoc.ToString()}: moving {m}", depth);

            var tempState = Intcode.State.Clone(initialState);
            tempState.input = (IntType)m;
            var newLoc = move(m, currentLoc);
            if (cells.Contains(newLoc))
            {
                // we've been here before
                continue;
            }

            bool processedCurrent = false;
            while (!processedCurrent && Intcode.Step(tempState))
            {
                var output = tempState.PopOutput();
                if (output.HasValue)
                {
                    switch ((Response)output.Value)
                    {
                    case Response.Moved:
                        //  log($"Moved to {newLoc.ToString()}", depth);
                        break;

                    case Response.Wall:
                        log($"{newLoc.ToString()}: Wall", depth);
                        processedCurrent = true;
                        break;

                    case Response.MovedAndFound:
                        log($"{newLoc.ToString()}: found big O", depth);
                        oxygenCell = newLoc;
                        break;
                    }
                    if (!processedCurrent)
                    {
                        log($"{newLoc.ToString()}: Spreading it out here, boss", depth);
                        cells.Add(newLoc);
                        MapIt(tempState, newLoc, ref cells, ref oxygenCell, depth + 1);
                        processedCurrent = true;
                    }
                }
            }
        }
    }
示例#14
0
    List <Movement> FindIt(Intcode.State initialState, List <Movement> current)
    {
        foreach (Movement m in Enum.GetValues(typeof(Movement)))
        {
            if (current.Count > 0 && backwards(m, current[current.Count - 1]))
            {
                // Don't go backwards
                continue;
            }

            var tempState = Intcode.State.Clone(initialState);
            tempState.input = (IntType)m;
            var tempMovement = new List <Movement>(current);
            tempMovement.Add(m);

            var hitWall = false;
            while (!hitWall && Intcode.Step(tempState))
            {
                var output = tempState.PopOutput();
                if (output.HasValue)
                {
                    switch ((Response)output.Value)
                    {
                    case Response.Moved:
                        // recurse
                        var found = FindIt(tempState, tempMovement);
                        if (found != null)
                        {
                            return(found);
                        }
                        hitWall = true;
                        break;

                    case Response.Wall:
                        hitWall = true;
                        break;

                    case Response.MovedAndFound:
                        return(tempMovement);
                    }
                }
            }
        }
        return(null);
    }
示例#15
0
    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);
    }
示例#16
0
    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());
    }
示例#17
0
    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?");
    }
示例#18
0
    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());
    }
示例#19
0
    public string SolveB()
    {
        var lines      = File.ReadAllLines(Input, Encoding.UTF8);
        var program    = Intcode.ParseInput(lines[0]);
        var state      = new Intcode.State(program);
        var currentLoc = new Point(0, 0);
        var cells      = new HashSet <Point>();

        cells.Add(currentLoc);
        var oxygenCell = new Point(-1000, -1000);

        MapIt(state, currentLoc, ref cells, ref oxygenCell, 0);

        // Now we have it mapped, spread out the oxy.
        var breathable = new HashSet <Point>();

        breathable.Add(oxygenCell);
        int minutes = 0;

        while (breathable.Count != cells.Count)
        {
            var newBreathable = new HashSet <Point>(breathable);
            foreach (var point in breathable)
            {
                foreach (Movement m in Enum.GetValues(typeof(Movement)))
                {
                    var spreadTo = move(m, point);
                    if (cells.Contains(spreadTo))
                    {
                        newBreathable.Add(spreadTo);
                    }
                }
            }
            breathable = newBreathable;

            minutes++;
        }

        return(minutes.ToString());
    }
示例#20
0
    private IntType ComputePower(IntType[] order, string rawProgram)
    {
        IntType power = 0;

        foreach (var phase in order)
        {
            IntType[] inputs = { phase, power };

            var program = Intcode.ParseInput(rawProgram);

            var inputPos = 0;
            var state    = new Intcode.State(program.ToArray(), inputs[inputPos++]);
            while (Intcode.Step(state))
            {
                if (!state.input.HasValue && inputPos < inputs.Length)
                {
                    state.input = inputs[inputPos++];
                }
            }
            power = state.output ?? -1;
        }
        return(power);
    }
示例#21
0
 public Computer(int address, long[] program, Action <PacketToSend> sendPacket)
 {
     packetSender = sendPacket;
     state        = new Intcode.State(program, address);
 }