Exemplo n.º 1
0
        private SparseMap <char> GenerateMap(IntCodeMachine icm)
        {
            var map = new SparseMap <char>(true);

            (int x, int y)cur = (0, 0);


            while (icm.Step())
            {
                while (icm.Output.CanRead)
                {
                    var tile = icm.Output.Read();
                    //Console.Write((char)tile);
                    if (tile == 10)
                    {
                        cur.x = 0;
                        cur.y++;
                    }
                    else
                    {
                        map.Set(cur, (char)tile);
                        cur.x++;
                    }
                }
                if (icm.WantInput)
                {
                }
            }
            return(map);
        }
Exemplo n.º 2
0
    string[] Screenshot(string input)
    {
        var icm    = new IntCodeMachine(input);
        var output = icm.Run();

        return(output.ToAscii().Split("\n").Where(x => !string.IsNullOrWhiteSpace(x)).ToArray());
    }
Exemplo n.º 3
0
        public object PartOne(string input)
        {
            var icm    = new IntCodeMachine(input);
            var output = icm.Run();
            var chunks = Chunk(output, 3);

            return(chunks.Count(x => x[2] == 2));
        }
Exemplo n.º 4
0
        public static long Step1()
        {
            var input = Input;

            input[1] = 12;
            input[2] = 2;
            return(IntCodeMachine.RunUntilStopped(input).Memory[0]);
        }
Exemplo n.º 5
0
 long ExecIntCode(IntCodeMachine icm, int noun, int verb)
 {
     icm.Reset();
     icm.memory[1] = noun;
     icm.memory[2] = verb;
     icm.Run();
     return(icm.memory[0]);
 }
Exemplo n.º 6
0
    public object PartOne(string input)
    {
        var securityRoom = "== Security Checkpoint ==";
        var icm          = new IntCodeMachine(input);
        var description  = icm.Run().ToAscii();

        VisitRooms(securityRoom, icm, description, args => {
            foreach (var item in args.items)
            {
                if (item != "infinite loop")
                {
                    var takeCmd = "take " + item;
                    var clone   = icm.Clone();
                    clone.Run(takeCmd);
                    if (!clone.Halted() && Inventory(clone).Contains(item))
                    {
                        icm.Run(takeCmd);
                    }
                }
            }
            return(null);
        });

        var door = VisitRooms(securityRoom, icm, description, args =>
                              args.room == securityRoom ? args.doors.Single(door => door != ReverseDir(args.doorTaken)) : null);

        Random r = new Random();

        void TakeOrDrop(string cmd, List <string> from, List <string> to)
        {
            var i    = r.Next(from.Count);
            var item = from[i];

            from.RemoveAt(i);
            to.Add(item);
            icm.Run(cmd + " " + item);
        }

        var inventory = Inventory(icm).ToList();
        var floor     = new List <string>();

        while (true)
        {
            var output = icm.Run(door).ToAscii();
            if (output.Contains("heavier"))
            {
                TakeOrDrop("take", floor, inventory);
            }
            else if (output.Contains("lighter"))
            {
                TakeOrDrop("drop", inventory, floor);
            }
            else
            {
                return(long.Parse(Regex.Match(output, @"\d+").Value));
            }
        }
    }
Exemplo n.º 7
0
        public void Test5()
        {
            long[]         program = "104,1125899906842624,99".Split(',').Select(long.Parse).ToArray();
            IntCodeMachine icm     = new IntCodeMachine(program);

            icm.Run();

            Assert.AreEqual(1125899906842624, icm.GetNextOutput());
        }
Exemplo n.º 8
0
        public void Test4()
        {
            long[]         program = "1102,34915192,34915192,7,4,7,99,0".Split(',').Select(long.Parse).ToArray();
            IntCodeMachine icm     = new IntCodeMachine(program);

            icm.Run();

            Assert.AreEqual(1219070632396864, icm.GetNextOutput());
        }
Exemplo n.º 9
0
        public void Test1()
        {
            long[]         program = "109,2000,109,19,99".Split(',').Select(long.Parse).ToArray();
            IntCodeMachine icm     = new IntCodeMachine(program);

            icm.Run();

            Assert.AreEqual(2019, icm.CurrentRelativeBase);
        }
Exemplo n.º 10
0
        public void Test3()
        {
            long[]         program = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99".Split(',').Select(long.Parse).ToArray();
            IntCodeMachine icm     = new IntCodeMachine(program);

            icm.Run();

            Assert.AreEqual(109, icm.GetNextOutput());
        }
Exemplo n.º 11
0
    public object PartTwo(string input)
    {
        var program = GeneratePrograms(Path(input)).First();

        var icm = new IntCodeMachine(input);

        icm.memory[0] = 2;
        return(icm.Run(program).Last());
    }
Exemplo n.º 12
0
        public override void First()
        {
            var icm = new IntCodeMachine();

            icm.Init("Day15/repairdroid.ic");
            map = CreateMap(icm);
            int val = ShortestPath(map);

            Echo($"Shortest path from (0,0) to oxygen tank in {val} steps");
            ValidateAnswer(val, 218);
        }
Exemplo n.º 13
0
        public override void First()
        {
            var icm = new IntCodeMachine();

            icm.Init("Day17/cameras.ic");
            var map = GenerateMap(icm);
            int val = FindIntersections(map);

            map.Render();
            Echo($"Alignment parameter sum: {val}");
            ValidateAnswer(val, 2080);
        }
Exemplo n.º 14
0
        public override void Second()
        {
            var icm = new IntCodeMachine();

            icm.Init("Day9/boost.ic");
            icm.Input.Write(2); // Boost code
            icm.Run();
            var boostcode = icm.Output.Read();

            Echo($"output: {boostcode}");
            ValidateAnswer(boostcode, 49115);
        }
Exemplo n.º 15
0
        public override void First()
        {
            var icm = new IntCodeMachine();

            icm.Init("Day9/boost.ic");
            //icm.Trace = true;
            icm.Input.Write(1); // Test code
            icm.Run();
            var testOutput = icm.Output.Read();

            Echo($"debug output: {testOutput}");
            ValidateAnswer(testOutput, 2775723069);
        }
Exemplo n.º 16
0
        public override void Second()
        {
            var icm = new IntCodeMachine();

            icm.Init("Day17/cameras.ic");
            icm.mem[0] = 2; // Override movement control
            var map = new SparseMap <char>(true);
            var y0  = Console.CursorTop;

            (int x, int y)cur = (0, 0);
            int cmd = 0, cIdx = 0;
            var dust = 0L;

            while (icm.Step())
            {
                while (icm.Output.CanRead)
                {
                    var tile = icm.Output.Read();
                    if (tile == 10)
                    {
                        cur.x = 0;
                        cur.y++;
                    }
                    else if (tile < 256)
                    {
                        map.Set(cur, (char)tile);
                        cur.x++;
                    }
                    else
                    {
                        dust = tile;
                    }
                }
                if (icm.WantInput)
                {
                    if (cmd < movements.Length && cIdx < movements[cmd].Length)
                    {
                        icm.Input.Write((long)movements[cmd][cIdx]);
                        cIdx++;
                    }
                    else
                    {
                        cmd++;
                        cIdx = 0;
                    }
                }
            }
            map.Render();
            Echo($"Dust collected: {dust}");
            ValidateAnswer(dust, 742673);
        }
Exemplo n.º 17
0
        public void Day5BExample4()
        {
            string memory = "3,9,8,9,10,9,4,9,99,-1,8";

            var input = new long[1] {
                8
            };

            IntCodeMachine machine = new IntCodeMachine(Array.ConvertAll(memory.Split(','), long.Parse), input);

            machine.Run();

            Assert.AreEqual(1, machine.GetNextOutput());
        }
Exemplo n.º 18
0
        public override void First()
        {
            var icm = new IntCodeMachine();

            icm.Init("Day13/breakout.ic");

            icm.Input.Write(0); // Start on a black panel

            Play(icm);
            var tilesLeft = tiles.Count(t => t.Value == 2);

            Echo($"# of tiles left: {tilesLeft}");
            ValidateAnswer(tilesLeft, 306);
        }
Exemplo n.º 19
0
    public object PartOne(string input)
    {
        var icm = new IntCodeMachine(input);

        // J = (¬A ∨ ¬B ∨ ¬C) ∧ D
        // jump if no road ahead, but we can continue from D
        return(new IntCodeMachine(input).Run(
                   "OR A T",
                   "AND B T",
                   "AND C T",
                   "NOT T J",
                   "AND D J",
                   "WALK"
                   ).Last());
    }
Exemplo n.º 20
0
        public void Day5BExample3()
        {
            string memory =
                "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99";

            var input = new long[1] {
                9
            };

            IntCodeMachine machine = new IntCodeMachine(Array.ConvertAll(memory.Split(','), long.Parse), input);

            machine.Run();

            Assert.AreEqual(1001, machine.GetNextOutput());
        }
Exemplo n.º 21
0
    public object PartTwo(string input)
    {
        var icm = new IntCodeMachine(input);

        for (var sum = 0; ; sum++)
        {
            for (var verb = 0; verb <= sum; verb++)
            {
                var noun = sum - verb;
                var res  = ExecIntCode(icm, noun, verb);
                if (res == 19690720)
                {
                    return(100 * noun + verb);
                }
            }
        }
        throw new Exception();
    }
Exemplo n.º 22
0
        public object PartTwo(string input)
        {
            var icm = new IntCodeMachine(input);

            icm.memory[0] = 2;
            var score      = 0;
            var icolBall   = -1;
            var icolPaddle = -1;
            var dir        = 0;

            while (true)
            {
                var output = icm.Run(dir);
                var chunks = Chunk(output, 3);
                foreach (var chunk in chunks)
                {
                    var(icol, irow, block) = (chunk[0], chunk[1], chunk[2]);
                    if ((icol, irow) == (-1, 0))
                    {
                        score = (int)block;
                    }
                    if (block == 3)
                    {
                        icolPaddle = (int)icol;
                    }
                    else if (block == 4)
                    {
                        icolBall = (int)icol;
                    }
                }

                if (icm.Halted())
                {
                    break;
                }

                dir =
                    icolBall <icolPaddle ? -1 :
                              icolBall> icolPaddle ? 1 :
                    0;
            }
            return(score);
        }
Exemplo n.º 23
0
        public static int Step2()
        {
            const int sentinel = 19690720;

            for (var noun = 0; noun < 100; noun++)
            {
                for (var verb = 0; verb < 100; verb++)
                {
                    var input = Input;
                    input[1] = noun;
                    input[2] = verb;
                    var result = IntCodeMachine.RunUntilStopped(input).Memory[0];
                    if (result == sentinel)
                    {
                        return(noun * 100 + verb);
                    }
                }
            }
            throw new ApplicationException();
        }
Exemplo n.º 24
0
 private ImmutableIntCodeMachine(IntCodeMachine icm)
 {
     this.icm = icm;
 }
Exemplo n.º 25
0
 string VisitRooms(
     string securityRoom,
     IntCodeMachine icm,
     string description,
     Func <(IEnumerable <string> items, string room, string doorTaken, IEnumerable <string> doors), string> callback