コード例 #1
0
ファイル: Program.cs プロジェクト: keydon/adventOfCode
        private static State RunOpcodeProgram(State state)
        {
            var register             = state.Register;
            Func <Point, Point> step = null;
            string direction         = " ";

            do
            {
                var opcode = register[state.Pos].ToString().PadLeft(6, '0');
                var val    = new ValueRetriever(state, opcode);

                switch (opcode)
                {
                case string o when o.EndsWith("01"):     //addition
                    var a = val.Get(1);

                    var b      = val.Get(2);
                    var addRes = a + b;

                    if (isDebug)
                    {
                        Console.WriteLine($"Adding {a} + {b}: " + addRes);
                    }
                    val.Set(3, addRes);
                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("02"):     //multiplication
                    var x = val.Get(1);

                    var y        = val.Get(2);
                    var multiRes = x * y;
                    if (isDebug)
                    {
                        Console.WriteLine($"Multi {x} + {y}: " + multiRes);
                    }
                    val.Set(3, multiRes);
                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("03"):     //input
                    long input = 0;

                    if (withVisuals || !isAutopilot)
                    {
                        DrawGame(state);
                    }
                    if (isAutopilot)
                    {
                        switch (direction)
                        {
                        case "l": input = -1; break;

                        case "r": input = 1; break;

                        case " ": input = 0; break;
                        }
                        if (isDebug)
                        {
                            Console.WriteLine("goin " + direction);
                        }
                    }
                    else
                    {
                        key = Console.ReadKey(true);
                        if (key.Key == ConsoleKey.UpArrow)
                        {
                            input = 1;
                            step  = BuildStep('U');
                        }
                        else if (key.Key == ConsoleKey.DownArrow)
                        {
                            step  = BuildStep('D');
                            input = 2;
                        }
                        else if (key.Key == ConsoleKey.LeftArrow)
                        {
                            input = 3;
                            step  = BuildStep('L');
                        }
                        else if (key.Key == ConsoleKey.RightArrow)
                        {
                            input = 4;
                            step  = BuildStep('R');
                        }
                    }
                    val.Set(1, input);
                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("04"):     //output
                    var output = val.Get(1);

                    if (isDebug)
                    {
                        Console.WriteLine("\r\nOut: " + output);
                    }
                    switch (output)
                    {
                    case 0L:
                        var wall = step(state.CurrentRobotPos);

                        if (!state.Panels.TryGetValue(wall, out var _))
                        {
                            state.Panels.Add(wall, new Panel(state.Panels)
                            {
                                Color = "#",
                                Pos   = wall
                            });
                        }
                        ;
                        break;

                    case 1L:
                        var freeSpace = step(state.CurrentRobotPos);
                        if (!state.Panels.TryGetValue(freeSpace, out var _))
                        {
                            state.Panels.Add(freeSpace, new Panel(state.Panels)
                            {
                                Color = ".",
                                Pos   = freeSpace
                            });
                        }
                        ;
                        state.CurrentRobotPos = freeSpace;
                        break;

                    case 2L:
                        var oxy = step(state.CurrentRobotPos);
                        if (!state.Panels.TryGetValue(oxy, out var _))
                        {
                            state.Panels.Add(oxy, new Panel(state.Panels)
                            {
                                Color = "O",
                                Pos   = oxy
                            });
                        }
                        Console.WriteLine("Goal reached! {0}", oxy);
                        Console.ReadLine();
                        state.CurrentRobotPos = oxy;
                        break;

                    default:
                        throw new Exception("Unknown droid response: " + output);
                    }

                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("05"):     //jump if true
                    var jumpIfTrue = val.Get(1);

                    if (jumpIfTrue != 0)
                    {
                        state.Pos = val.Get(2);
                    }
                    else
                    {
                        state.Pos += 3;
                    }
                    break;

                case string o when o.EndsWith("06"):     //jump if false
                    var jumpIfFalse = val.Get(1);

                    if (jumpIfFalse == 0)
                    {
                        state.Pos = val.Get(2);
                    }
                    else
                    {
                        state.Pos += 3;
                    }
                    break;

                case string o when o.EndsWith("07"):     //less then
                    val.Set(3,
                            val.Get(1) < val.Get(2)
                            ? 1
                            : 0
                            );

                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("08"):     //equals
                    val.Set(3,
                            val.Get(1) == val.Get(2)
                            ? 1
                            : 0
                            );

                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("09"):     //setting rel base
                    var oldRelativeBase = state.RelativeBase;

                    var relativeBaseChange = val.Get(1);
                    state.RelativeBase = oldRelativeBase + relativeBaseChange;
                    state.Pos         += 2;
                    break;

                case string o when o.EndsWith("99"):     //halt
                    Console.WriteLine("\r\nGAME OVER!!!!!!!!!!!");

                    state.Running = false;
                    break;

                default:
                    state.Running = false;
                    Console.WriteLine($"ERROR: pos {state.Pos} opcode {opcode}");
                    return(state);
                }
            } while (state.Running);

            return(state);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: keydon/adventOfCode
        private static State RunOpcodeProgram(State state)
        {
            var    register  = state.Register;
            var    latestOut = 0L;
            string toPaint   = null;
            string toTurn    = null;

            do
            {
                var opcode = register[state.Pos].ToString().PadLeft(6, '0');
                var val    = new ValueRetriever(state, opcode);
                //Console.WriteLine($"Current Pos: {state.Pos}");

                switch (opcode)
                {
                case string o when o.EndsWith("01"):     //addition
                    var a = val.Get(1);

                    var b      = val.Get(2);
                    var addRes = a + b;

                    //if (isDebug) Console.WriteLine($"Adding {a} + {b}: " + addRes);
                    val.Set(3, addRes);
                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("02"):     //multiplication
                    var x = val.Get(1);

                    var y        = val.Get(2);
                    var multiRes = x * y;
                    //if (isDebug) Console.WriteLine($"Multi {x} + {y}: " + multiRes);
                    val.Set(3, multiRes);
                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("03"):     //input
                    Panel panelToCheck;

                    if (!state.Panels.TryGetValue(state.CurrentRobotPos, out panelToCheck))
                    {
                        panelToCheck = new Panel();
                        state.Panels.Add(state.CurrentRobotPos, panelToCheck);
                    }
                    var input = panelToCheck.Color == "." ? 0 : 1;

                    if (isDebug)
                    {
                        Console.WriteLine("Input: " + input);
                    }
                    val.Set(1, input);
                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("04"):     //output
                    var output = val.Get(1);

                    if (isDebug)
                    {
                        Console.WriteLine("\r\nOut: " + output);
                    }

                    if (toPaint == null)
                    {
                        toPaint = output == 0 ? "." : "#";
                        Panel panelToPaint;
                        if (!state.Panels.TryGetValue(state.CurrentRobotPos, out panelToPaint))
                        {
                            panelToPaint = new Panel();
                            state.Panels.Add(state.CurrentRobotPos, panelToPaint);
                        }
                        panelToPaint.Color = toPaint;
                        panelToPaint.TimesPainted++;
                    }
                    else
                    {
                        toPaint = null;
                        toTurn  = output == 0 ? "left" : "right";
                        switch (state.Direction)
                        {
                        case "up":
                            state.Direction = toTurn == "left" ? "left" : "right";
                            break;

                        case "left":
                            state.Direction = toTurn == "left" ? "down" : "up";
                            break;

                        case "right":
                            state.Direction = toTurn == "left" ? "up" : "down";
                            break;

                        case "down":
                            state.Direction = toTurn == "left" ? "right" : "left";
                            break;

                        default:
                            throw new Exception("Facing unknown direction1!");
                        }

                        Func <Point, Point> step = null;
                        switch (state.Direction)
                        {
                        case "up":
                            step = BuildStep('U');
                            break;

                        case "left":
                            step = BuildStep('L');
                            break;

                        case "right":
                            step = BuildStep('R');
                            break;

                        case "down":
                            step = BuildStep('D');
                            break;

                        default:
                            throw new Exception("Facing unknown direction2!");
                        }

                        state.CurrentRobotPos = step(state.CurrentRobotPos);
                    }

                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("05"):     //jump if true
                    var jumpIfTrue = val.Get(1);

                    if (jumpIfTrue != 0)
                    {
                        state.Pos = val.Get(2);
                    }
                    else
                    {
                        state.Pos += 3;
                    }
                    break;

                case string o when o.EndsWith("06"):     //jump if false
                    var jumpIfFalse = val.Get(1);

                    if (jumpIfFalse == 0)
                    {
                        state.Pos = val.Get(2);
                    }
                    else
                    {
                        state.Pos += 3;
                    }
                    break;

                case string o when o.EndsWith("07"):     //less then
                    val.Set(3,
                            val.Get(1) < val.Get(2)
                            ? 1
                            : 0
                            );

                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("08"):     //equals
                    val.Set(3,
                            val.Get(1) == val.Get(2)
                            ? 1
                            : 0
                            );

                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("09"):     //setting rel base
                    var oldRelativeBase = state.RelativeBase;

                    var relativeBaseChange = val.Get(1);
                    state.RelativeBase = oldRelativeBase + relativeBaseChange;
                    //if (isDebug) Console.WriteLine($"\r\n{oldRelativeBase} + {relativeBaseChange} = {state.RelativeBase}");
                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("99"):     //halt
                    if (isDebug)
                    {
                        Console.WriteLine("\r\nHALT!");
                    }
                    //Console.WriteLine(string.Join(", ", state.Register));
                    state.Running = false;

                    break;

                default:
                    state.Running = false;
                    Console.WriteLine($"ERROR: pos {state.Pos} opcode {opcode}");
                    return(state);
                }
            } while (state.Running);

            return(state);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: keydon/adventOfCode
        private static State RunOpcodeProgram(State state)
        {
            var    inputP    = new Point(-1, 0);
            var    register  = state.Register;
            int?   toX       = null;
            int?   toY       = null;
            string direction = " ";

            do
            {
                var opcode = register[state.Pos].ToString().PadLeft(6, '0');
                var val    = new ValueRetriever(state, opcode);

                switch (opcode)
                {
                case string o when o.EndsWith("01"):     //addition
                    var a = val.Get(1);

                    var b      = val.Get(2);
                    var addRes = a + b;

                    if (isDebug)
                    {
                        Console.WriteLine($"Adding {a} + {b}: " + addRes);
                    }
                    val.Set(3, addRes);
                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("02"):     //multiplication
                    var x = val.Get(1);

                    var y        = val.Get(2);
                    var multiRes = x * y;
                    if (isDebug)
                    {
                        Console.WriteLine($"Multi {x} + {y}: " + multiRes);
                    }
                    val.Set(3, multiRes);
                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("03"):     //input
                    long input = 0;

                    if (withVisuals || !isAutopilot)
                    {
                        DrawGame(state);
                    }
                    if (isAutopilot)
                    {
                        switch (direction)
                        {
                        case "l": input = -1; break;

                        case "r": input = 1; break;

                        case " ": input = 0; break;
                        }
                        if (isDebug)
                        {
                            Console.WriteLine("goin " + direction);
                        }
                    }
                    else
                    {
                        key = Console.ReadKey(true);
                        if (key.Key == ConsoleKey.LeftArrow)
                        {
                            input = -1;
                        }
                        else if (key.Key == ConsoleKey.RightArrow)
                        {
                            input = 1;
                        }
                    }
                    val.Set(1, input);
                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("04"):     //output
                    var output = val.Get(1);

                    if (isDebug)
                    {
                        Console.WriteLine("\r\nOut: " + output);
                    }

                    if (toX == null)
                    {
                        toX = (int)output;
                    }
                    else if (toY == null)
                    {
                        toY = (int)output;
                    }
                    else
                    {
                        var p = new Point(toX.Value, toY.Value);
                        toX = null;
                        toY = null;

                        if (inputP == p)
                        {
                            state.Score = output;
                        }
                        else
                        {
                            Panel panelToPaint;
                            if (!state.Panels.TryGetValue(p, out panelToPaint))
                            {
                                panelToPaint = new Panel();
                                state.Panels.Add(p, panelToPaint);
                            }
                            panelToPaint.Color = output.ToString();
                            panelToPaint.TimesPainted++;
                            var pp = panelToPaint.Pos;
                            panelToPaint.Pos = p;

                            if (output == 3L)
                            {
                                state.Paddle = panelToPaint;
                            }
                            else if (output == 4L)
                            {
                                direction = p.X > state.Paddle?.Pos.X
                                        ? "r"
                                        : p.X < state.Paddle?.Pos.X
                                        ? "l"
                                        : pp.X <p.X
                                                ? "r"
                                                : pp.X> p.X
                                        ? "l" : " ";
                            }
                        }
                    }
                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("05"):     //jump if true
                    var jumpIfTrue = val.Get(1);

                    if (jumpIfTrue != 0)
                    {
                        state.Pos = val.Get(2);
                    }
                    else
                    {
                        state.Pos += 3;
                    }
                    break;

                case string o when o.EndsWith("06"):     //jump if false
                    var jumpIfFalse = val.Get(1);

                    if (jumpIfFalse == 0)
                    {
                        state.Pos = val.Get(2);
                    }
                    else
                    {
                        state.Pos += 3;
                    }
                    break;

                case string o when o.EndsWith("07"):     //less then
                    val.Set(3,
                            val.Get(1) < val.Get(2)
                            ? 1
                            : 0
                            );

                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("08"):     //equals
                    val.Set(3,
                            val.Get(1) == val.Get(2)
                            ? 1
                            : 0
                            );

                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("09"):     //setting rel base
                    var oldRelativeBase = state.RelativeBase;

                    var relativeBaseChange = val.Get(1);
                    state.RelativeBase = oldRelativeBase + relativeBaseChange;
                    state.Pos         += 2;
                    break;

                case string o when o.EndsWith("99"):     //halt
                    Console.WriteLine("\r\nGAME OVER!!!!!!!!!!!");

                    state.Running = false;
                    break;

                default:
                    state.Running = false;
                    Console.WriteLine($"ERROR: pos {state.Pos} opcode {opcode}");
                    return(state);
                }
            } while (state.Running);

            return(state);
        }