Exemplo n.º 1
0
        void UpdateReadmeForYear(Calendar calendar)
        {
            var file = Path.Combine(SolverExtensions.WorkingDir(calendar.Year), "README.md");

            WriteFile(file, new ReadmeGeneratorForYear().Generate(calendar));

            var svg = Path.Combine(SolverExtensions.WorkingDir(calendar.Year), "calendar.svg");

            WriteFile(svg, calendar.ToSvg());
        }
Exemplo n.º 2
0
        public async Task Update(int year, int day, IEnumerable <Type> solvers, IUsageProvider usageProvider)
        {
            if (!System.Environment.GetEnvironmentVariables().Contains("SESSION"))
            {
                throw new Exception("Specify SESSION environment variable");
            }

            var cookieContainer = new CookieContainer();

            using (var client = new HttpClient(
                       new HttpClientHandler {
                CookieContainer = cookieContainer,
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            })
                   ) {
                var baseAddress = new Uri("https://adventofcode.com/");
                client.BaseAddress = baseAddress;
                cookieContainer.Add(baseAddress, new Cookie("session", System.Environment.GetEnvironmentVariable("SESSION")));

                var calendar = await DownloadCalendar(client, year);

                var problem = await DownloadProblem(client, year, day);

                var dir = Dir(year, day);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                // var years = Assembly.GetEntryAssembly().GetTypes()
                //     .Where(t => t.GetTypeInfo().IsClass && typeof(ISolver).IsAssignableFrom(t))
                //     .Select(tsolver => SolverExtensions.Year(tsolver));
                var years = solvers.Select(tsolver => SolverExtensions.Year(tsolver));

                if (years.Any())
                {
                    UpdateProjectReadme(years.Min(), years.Max(), usageProvider);
                }
                UpdateReadmeForYear(calendar);
                UpdateSplashScreen(calendar);
                UpdateReadmeForDay(problem);
                UpdateInput(problem);
                UpdateRefout(problem);
                UpdateSolutionTemplate(problem);
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var tsolvers = Assembly.GetEntryAssembly() !.GetTypes()
                           .Where(t => t.GetTypeInfo().IsClass&& typeof(Solver).IsAssignableFrom(t))
                           .OrderBy(t => t.FullName)
                           .ToArray();

            var action =
                // Generate solution template for [year]/[day]
                Command(args, Args("update", "([0-9]+)/([0-9]+)"), m => {
                var year = int.Parse(m[1]);
                var day  = int.Parse(m[2]);
                return(() => new Updater().Update(year, day).Wait());
            }) ??
                Command(args, Args("([0-9]+)/([0-9]+)"), m => {
                var year             = int.Parse(m[0]);
                var day              = int.Parse(m[1]);
                var tSolversSelected = tsolvers.First(tSolver =>
                                                      SolverExtensions.Year(tSolver) == year &&
                                                      SolverExtensions.Day(tSolver) == day);
                return(() => Runner.RunAll(tSolversSelected));
            }) ??
                Command(args, Args("[0-9]+"), m => {
                var year             = int.Parse(m[0]);
                var tSolversSelected = tsolvers.Where(tSolver =>
                                                      SolverExtensions.Year(tSolver) == year);
                return(() => Runner.RunAll(tSolversSelected.ToArray()));
            }) ??
                Command(args, Args("last"), m => {
                var tSolversSelected = tsolvers.Last();
                return(() => Runner.RunAll(tSolversSelected));
            }) ??
                Command(args, Args("all"), m => {
                return(() => Runner.RunAll(tsolvers));
            }) ??
                new Action(() => {
                Console.WriteLine(Usage.Get());
            });

            action();
        }
Exemplo n.º 4
0
        public async Task Update(int year, int day)
        {
            if (!Environment.GetEnvironmentVariables().Contains("SESSION"))
            {
                throw new Exception("Specify SESSION environment variable");
            }
            var session     = Environment.GetEnvironmentVariable("SESSION");
            var baseAddress = new Uri("https://adventofcode.com/");

            var context = BrowsingContext.New(Configuration.Default
                                              .WithDefaultLoader()
                                              .WithCss()
                                              .WithDefaultCookies()
                                              );

            context.SetCookie(new Url(baseAddress.ToString()), "session=" + session);

            var calendar = await DownloadCalendar(context, baseAddress, year);

            var problem = await DownloadProblem(context, baseAddress, year, day);

            var dir = Dir(year, day);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            var years = Assembly.GetEntryAssembly().GetTypes()
                        .Where(t => t.GetTypeInfo().IsClass&& typeof(Solver).IsAssignableFrom(t))
                        .Select(tsolver => SolverExtensions.Year(tsolver));

            UpdateProjectReadme(years.Min(), years.Max());
            UpdateReadmeForYear(calendar);
            UpdateSplashScreen(calendar);
            UpdateReadmeForDay(problem);
            UpdateInput(problem);
            UpdateRefout(problem);
            UpdateSolutionTemplate(problem);
        }
Exemplo n.º 5
0
 string Dir(int year, int day) => SolverExtensions.WorkingDir(year, day);
Exemplo n.º 6
0
        void UpdateSplashScreen(Calendar calendar)
        {
            var file = Path.Combine(SolverExtensions.WorkingDir(calendar.Year), "SplashScreen.cs");

            WriteFile(file, new SplashScreenGenerator().Generate(calendar));
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            var usageProvider = new ApplicationUsage();

            var entryAssembly = Assembly.GetEntryAssembly();
            var files         = Directory.EnumerateFiles(Path.GetDirectoryName(entryAssembly.Location), "solutions.dll");
            var assemblies    = files.Select(f => AssemblyLoadContext.Default.LoadFromAssemblyPath(f)).ToList();

            assemblies.Add(typeof(Program).Assembly);

            var tsolvers = assemblies.SelectMany(a => a.GetTypes())
                           .Where(t => t.GetTypeInfo().IsClass&& typeof(ISolver).IsAssignableFrom(t))
                           .OrderBy(t => t.FullName)
                           .ToArray();

            var action =
                Command(args, Args("update", "([0-9]+)/([0-9]+)"), m => {
                var year = int.Parse(m[1]);
                var day  = int.Parse(m[2]);
                return(() => new Updater().Update(year, day, tsolvers, usageProvider).Wait());
            }) ??
                Command(args, Args("update", "last"), m => {
                var dt = DateTime.Now;
                if (dt.Month == 12 && dt.Day >= 1 && dt.Day <= 25)
                {
                    return(() => new Updater().Update(dt.Year, dt.Day, tsolvers, usageProvider).Wait());
                }
                else
                {
                    throw new Exception("Event is not active. This option works in Dec 1-25 only)");
                }
            }) ??
                Command(args, Args("([0-9]+)/([0-9]+)"), m => {
                var year             = int.Parse(m[0]);
                var day              = int.Parse(m[1]);
                var tsolversSelected = tsolvers.First(tsolver =>
                                                      SolverExtensions.Year(tsolver) == year &&
                                                      SolverExtensions.Day(tsolver) == day);
                return(() => Runner.RunAll(tsolversSelected));
            }) ??
                Command(args, Args("[0-9]+"), m => {
                var year             = int.Parse(m[0]);
                var tsolversSelected = tsolvers.Where(tsolver =>
                                                      SolverExtensions.Year(tsolver) == year);
                return(() => Runner.RunAll(tsolversSelected.ToArray()));
            }) ??
                Command(args, Args("([0-9]+)/last"), m => {
                var year             = int.Parse(m[0]);
                var tsolversSelected = tsolvers.Last(tsolver =>
                                                     SolverExtensions.Year(tsolver) == year);
                return(() => Runner.RunAll(tsolversSelected));
            }) ??
                Command(args, Args("([0-9]+)/all"), m => {
                var year             = int.Parse(m[0]);
                var tsolversSelected = tsolvers.Where(tsolver =>
                                                      SolverExtensions.Year(tsolver) == year);
                return(() => Runner.RunAll(tsolversSelected.ToArray()));
            }) ??
                Command(args, Args("all"), m => {
                return(() => Runner.RunAll(tsolvers));
            }) ??
                Command(args, Args("last"), m => {
                var tsolversSelected = tsolvers.Last();
                return(() => Runner.RunAll(tsolversSelected));
            }) ??
                new Action(() => {
                Console.WriteLine(usageProvider.Usage());
            });

            action();
        }