Exemple #1
0
        public Solution(Input.InputMode inputMode, string input)
        {
            var lines = Input.GetInputLines(inputMode, input).ToArray();

            bodies = new Dictionary <string, Body>();
            HashSet <string> allbodies        = new HashSet <string>();
            HashSet <string> allbodiesInOrbit = new HashSet <string>();

            foreach (var line in lines)
            {
                var split     = line.Split(")");
                var baseBody  = split[0];
                var orbitBody = split[1];

                if (!bodies.ContainsKey(baseBody))
                {
                    bodies.Add(baseBody, new Body(baseBody));
                }
                if (!bodies.ContainsKey(orbitBody))
                {
                    bodies.Add(orbitBody, new Body(orbitBody));
                }

                bodies[baseBody].AddOrbit(bodies[split[1]]);

                allbodies.Add(baseBody);
                allbodies.Add(orbitBody);
                allbodiesInOrbit.Add(orbitBody);
            }

            root = bodies[allbodies.Except(allbodiesInOrbit).Single()];
        }
Exemple #2
0
        private static void EvaluateRuntime(Input.InputMode inputMode, int numRuns)
        {
            var runtimes  = new long[25, numRuns];
            var stopwatch = new Stopwatch();

            for (int runCount = 0; runCount < numRuns; runCount++)
            {
                for (int dayNum = 1; dayNum <= 25; dayNum++)
                {
                    var solver = GetSolver(inputMode, dayNum, stopwatch);
                    runtimes[dayNum - 1, runCount] = stopwatch.ElapsedMilliseconds;
                }
            }

            var averageRuntimes = new long[25];

            for (int dayNum = 1; dayNum <= 25; dayNum++)
            {
                long totalTime = 0;
                for (int runNum = 0; runNum < numRuns; runNum++)
                {
                    totalTime += runtimes[dayNum - 1, runNum];
                }
                averageRuntimes[dayNum - 1] = totalTime / numRuns;
            }

            for (int dayCount = 1; dayCount <= 25; dayCount++)
            {
                Console.WriteLine($"Day{dayCount} average runtime: {averageRuntimes[dayCount - 1]}ms");
            }
            Console.WriteLine($"Average total runtime: {runtimes.Cast<long>().Sum() / numRuns}ms");
        }
Exemple #3
0
        public Solution(Input.InputMode inputMode, string input)
        {
            var startProg = Input.GetInputLines(inputMode, input, new char[] { ',' }).ToArray();
            var executor  = new Executor(startProg);

            program = executor.startProgram;
        }
Exemple #4
0
        private static void SolveIndividualDay(Input.InputMode inputMode, int dayNum)
        {
            var solver = GetSolver(inputMode, dayNum);

            Console.WriteLine($"Day{dayNum}:");
            solver.WriteSolutions();
        }
Exemple #5
0
        public Solution(Input.InputMode inputMode, string input)
        {
            var startProg = Input.GetInputLines(inputMode, input, new char[] { ',' }).ToArray();

            executor = new Executor(startProg);

            tiles = new Dictionary <Coordinate, long>();
        }
Exemple #6
0
        protected PuzzleSolver(Input.InputMode mode, string input)
        {
            var inputString = Input.GetInput(mode, input);

            ParseInput(inputString);
            PrepareSolution();
            SolvePartOne();
            SolvePartTwo();
        }
Exemple #7
0
 private static void SolveAllDays(Input.InputMode inputMode)
 {
     for (int dayNum = 1; dayNum <= 25; dayNum++)
     {
         var solver = GetSolver(inputMode, dayNum);
         Console.WriteLine($"Day{dayNum}:");
         solver.WriteSolutions();
     }
 }
Exemple #8
0
        public Solution(Input.InputMode inputMode, string input)
        {
            var program = Input.GetInputLines(inputMode, input, new char[] { ',' }).ToArray();

            amplifiers = new Amplifier[5];
            for (int n = 0; n < 5; n++)
            {
                amplifiers[n] = new Amplifier(program, "Amplifier " + n);
            }
        }
Exemple #9
0
        public Solution(Input.InputMode inputMode, string input)
        {
            var lines = Input.GetInputLines(inputMode, input).ToArray();

            asteroids = Asteroid.Parse(lines);
            foreach (var asteroid in asteroids)
            {
                locations[asteroid.X * 100 + asteroid.Y] = true;
            }

            FindStation();
            Find200th();
        }
Exemple #10
0
        private static string GetFileName(Input.InputMode inputMode, int dayNum)
        {
            switch (inputMode)
            {
            case Input.InputMode.Embedded:
                return("input.txt");

            case Input.InputMode.File:
                return($"{dayNum}.txt");

            default:
                throw new Exception("set InputMode to either Embedded or File");
            }
        }
Exemple #11
0
        public void Parse(Input.InputMode inputMode, string input)
        {
            var lines = Input.GetInputLines(inputMode, input).ToArray();

            tiles    = new List <Tile>();
            keyTiles = new List <Tile>();
            var allTiles = new Dictionary <(int x, int y), Tile>();

            var allKeys   = new List <string>();
            var doorTiles = new List <Tile>();

            for (int y = 0; y < lines.Length; y++)
            {
                var line = lines[y];

                for (int x = 0; x < line.Length; x++)
                {
                    var c = line[x];

                    string lockedBy = null;
                    string hasKey   = null;

                    if (c == '#')
                    {
                        continue;
                    }
                    if (c - 'A' < 26 && c - 'A' >= 0)
                    {
                        lockedBy = c.ToString().ToLower();
                    }
                    if (c - 'a' < 26 && c - 'a' >= 0)
                    {
                        hasKey = c.ToString();
                        allKeys.Add(hasKey);
                    }

                    var coord = new Coordinate(x, y);

                    var tile = new Tile(coord);
                    tile.HasKey   = hasKey;
                    tile.LockedBy = lockedBy;
                    doorTiles.Add(tile);

                    if (c == '@')
                    {
                        start = tile;
                    }

                    tiles.Add(tile);
                    allTiles[(x, y)] = tile;
Exemple #12
0
        private static PuzzleSolver GetSolver(Input.InputMode inputMode, int dayNum, Stopwatch stopwatch = null)
        {
            var  fileName          = GetFileName(inputMode, dayNum);
            bool stopwatchProvided = stopwatch != null;

            var solverType = Type.GetType($"AoC.Day{dayNum}.Solver");

            if (stopwatchProvided)
            {
                stopwatch.Restart();
            }
            var solver = (PuzzleSolver)Activator.CreateInstance(solverType, new Object[] { inputMode, fileName });

            if (stopwatchProvided)
            {
                stopwatch.Stop();
            }

            return(solver);
        }
Exemple #13
0
        public Solution(Input.InputMode inputMode, string input)
        {
            var lines = Input.GetInputLines(inputMode, input).ToArray();

            reactions = Reaction.Parse(lines);

            var ore = new Element()
            {
                name = "ORE", amount = 1
            };
            var oreReaction = new Reaction()
            {
                output = ore, requiredInputs = new Element[0]
            };

            ReactionWithOutput = new Dictionary <string, Reaction>();
            ReactionWithOutput.Add("ORE", oreReaction);
            foreach (var reaction in reactions)
            {
                ReactionWithOutput.Add(reaction.output.name, reaction);
            }

            BuildTree();
        }
Exemple #14
0
 public Solver(Input.InputMode mode, string input) : base(mode, input)
 {
 }
Exemple #15
0
        public Solution(Input.InputMode inputMode, string input)
        {
            var lines = Input.GetInputLines(inputMode, input).ToArray();

            moons = Moon.Parse(lines).ToArray();
        }
Exemple #16
0
        public Solution(Input.InputMode inputMode, string input)
        {
            var lines = Input.GetInputLines(inputMode, input).ToArray();

            startBugs = Parse(lines);
        }
Exemple #17
0
        public Solution(Input.InputMode inputMode, string input)
        {
            var lines = Input.GetInputLines(inputMode, input).ToArray();

            digits = lines.Single().ToCharArray().Select(c => c - 48).ToArray();
        }
Exemple #18
0
 public Solution(Input.InputMode inputMode, string input)
 {
     lines = Input.GetInputLines(inputMode, input).ToArray();
 }
Exemple #19
0
        public Solution(Input.InputMode inputMode, string input)
        {
            var lines = Input.GetInputLines(inputMode, input).ToArray();

            modules = ParsedInput.Parse(lines);
        }
Exemple #20
0
 public Solution(Input.InputMode inputMode, string input)
 {
     startProgram = Input.GetInputLines(inputMode, input).ToArray()[0].ToCharArray().Select(c => c - 48).Select(c => (long)c).ToArray();
     Reset();
 }
Exemple #21
0
 public Solution(Input.InputMode inputMode, string input)
 {
     Parse(inputMode, input);
 }