From 537305704bc1fa1efaa0d0e7eb37146fe5929913 Mon Sep 17 00:00:00 2001 From: Alexander Sigler Date: Fri, 3 Dec 2021 15:39:38 -0800 Subject: [PATCH] Finished Day 1 and set up Swagger to handle log messages --- AOC2021/Controllers/AdventOfCodeController.cs | 10 ++-- AOC2021/Days/Day1.cs | 39 ++++++++++++-- AOC2021/Helper/AOCExtensions.cs | 12 +++++ AOC2021/Models/AOCDay.cs | 52 ++++++++++++++++--- AOC2021/Models/AOCRequest.cs | 2 + AOC2021/Models/AOCResponse.cs | 6 ++- AOC2021/Program.cs | 5 ++ 7 files changed, 109 insertions(+), 17 deletions(-) create mode 100644 AOC2021/Helper/AOCExtensions.cs diff --git a/AOC2021/Controllers/AdventOfCodeController.cs b/AOC2021/Controllers/AdventOfCodeController.cs index 8d23fca..ac9cecb 100644 --- a/AOC2021/Controllers/AdventOfCodeController.cs +++ b/AOC2021/Controllers/AdventOfCodeController.cs @@ -20,9 +20,9 @@ namespace AOC2021.Controllers [HttpPost] [Consumes("text/plain")] [Route("day1")] - public AOCResponse Day(AOCVersion version, [FromBody] string input) + public AOCResponse Day(AOCVersion version, [FromBody] string input, bool IgnoreLogMessages = false) { - AOCRequest request = new AOCRequest() { Input = input, Version = version }; + AOCRequest request = new AOCRequest() { Input = input, Version = version, IgnoreLogMessages = IgnoreLogMessages }; var splitRoute = this.ControllerContext.HttpContext.Request.Path.ToString().Split("/"); string requestedRoute = splitRoute[splitRoute.Length - 1]; //Get the last endpoint value. return GetAOCDay(requestedRoute).ExecuteDay(request); @@ -30,17 +30,17 @@ namespace AOC2021.Controllers private AOCDay GetAOCDay(string route) { - IAOCService day = null; + AOCDay day = null; var type = typeof(AOCDay); var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(p => type.IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract); foreach (var x in types) { - Console.WriteLine(x.Name.ToLower()); if (x.Name.ToLower() == route.ToLower()) { - day = (IAOCService)Activator.CreateInstance(x); + day = (AOCDay) (IAOCService)Activator.CreateInstance(x); + day.SetLogger(this._logger); } diff --git a/AOC2021/Days/Day1.cs b/AOC2021/Days/Day1.cs index 9163e73..2036f8f 100644 --- a/AOC2021/Days/Day1.cs +++ b/AOC2021/Days/Day1.cs @@ -1,4 +1,5 @@ using AOC2021.Models; +using AOC2021.Helper; using System; using System.Collections.Generic; using System.Linq; @@ -8,16 +9,46 @@ namespace AOC2021.Days { public class Day1 : AOCDay { - protected override AOCResponse ExecutePartA() { - Console.WriteLine(this._request.Input); - return null; + var input = this._request.Input.Split("\n"); + int increase = 0; + for (int i = 0; i < input.Length-1; i++) + { + var curret = input[i].ToInt(); + var next = input[i + 1].ToInt(); + if (next > curret) + { + increase++; + Log("Increase"); + } + + } + this._response.Answer = increase.ToString(); + return this._response; } protected override AOCResponse ExecutePartB() { - throw new NotImplementedException(); + var input = this._request.Input.Split("\n"); + var list = new List(); + for (int i = 0; i < input.Length-2; i++) + { + var a = input[i].ToInt(); + var b = input[i + 1].ToInt(); + var c = input[i + 2].ToInt(); + list.Add(a + b + c); + } + list.ForEach(x => Log(x.ToString())); + + int increase = 0; + for (int i = 0; i < list.Count-1; i++) + { + if (list[i + 1] > list[i]) + increase++; + } + this._response.Answer = increase.ToString(); + return this._response; } } } diff --git a/AOC2021/Helper/AOCExtensions.cs b/AOC2021/Helper/AOCExtensions.cs new file mode 100644 index 0000000..489d462 --- /dev/null +++ b/AOC2021/Helper/AOCExtensions.cs @@ -0,0 +1,12 @@ +using System; + +namespace AOC2021.Helper +{ + public static class AOCExtensions + { + public static int ToInt(this string str) + { + return Convert.ToInt32(str); + } + } +} diff --git a/AOC2021/Models/AOCDay.cs b/AOC2021/Models/AOCDay.cs index 54da9ff..3ebab0c 100644 --- a/AOC2021/Models/AOCDay.cs +++ b/AOC2021/Models/AOCDay.cs @@ -1,26 +1,64 @@  using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; namespace AOC2021.Models { public abstract class AOCDay : IAOCService { protected AOCRequest _request; - protected ILogger _logger; + protected AOCResponse _response; + protected ILogger _logger; + private List _debugMessages; + + public AOCDay() + { + this._response = new AOCResponse() + { + Status = false + }; + this._debugMessages = new List(); + } public AOCResponse ExecuteDay(AOCRequest request) { _request = request; - switch (request.Version) + try + { + switch (request.Version) + { + case AOCVersion.A: + this._response = ExecutePartA(); + break; + case AOCVersion.B: + this._response = ExecutePartB(); + break; + } + this._response.Status = true; + } + catch (Exception e) { - case AOCVersion.A: - return ExecutePartA(); - case AOCVersion.B: - return ExecutePartB(); + this._response.Answer = e.Message; + this._response.Status = false; + this._response.StackTrace = e.StackTrace; } - return new AOCResponse() { Answer = "AOCDay failed to get a response", Status = false }; + this._response.Debug = this._debugMessages; + return this._response; } protected abstract AOCResponse ExecutePartA(); protected abstract AOCResponse ExecutePartB(); + + public void SetLogger(ILogger logger) + { + this._logger = logger; + } + + protected void Log(string message, params object[] args) + { + if (!this._request.IgnoreLogMessages) + _logger.LogInformation(message, args); + _debugMessages.Add(string.Format(message, args)); + } } } diff --git a/AOC2021/Models/AOCRequest.cs b/AOC2021/Models/AOCRequest.cs index 551b574..0793b96 100644 --- a/AOC2021/Models/AOCRequest.cs +++ b/AOC2021/Models/AOCRequest.cs @@ -11,5 +11,7 @@ namespace AOC2021.Models public AOCVersion Version { get; set; } [DataMember] public string Input { get; set; } + [DataMember] + public bool IgnoreLogMessages { get; set; } } } diff --git a/AOC2021/Models/AOCResponse.cs b/AOC2021/Models/AOCResponse.cs index f9e9b7e..b0439df 100644 --- a/AOC2021/Models/AOCResponse.cs +++ b/AOC2021/Models/AOCResponse.cs @@ -9,11 +9,15 @@ namespace AOC2021.Models [Serializable] public class AOCResponse { + [DataMember] + public DateTime Date { get { return DateTime.Now; } } + [DataMember] + public string Answer { get; set; } [DataMember] public bool Status { get; set; } [DataMember] public IEnumerable Debug { get; set; } [DataMember] - public string Answer { get; set; } + public string StackTrace { get; set; } } } diff --git a/AOC2021/Program.cs b/AOC2021/Program.cs index a3c944f..30312b0 100644 --- a/AOC2021/Program.cs +++ b/AOC2021/Program.cs @@ -21,6 +21,11 @@ namespace AOC2021 .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); + }).ConfigureLogging(logging => + { + logging.ClearProviders(); + logging.AddConsole(); + logging.AddDebug(); }); } }