From 45ccaf5c221c8dd0d11bdd967e1c19e8713cf560 Mon Sep 17 00:00:00 2001 From: Xander Sigler Date: Sat, 4 Dec 2021 21:59:27 -0800 Subject: [PATCH] Added day 2 --- AOC2021/AOC2021.csproj | 12 ++-- AOC2021/Controllers/AdventOfCodeController.cs | 2 +- AOC2021/Days/Day2.cs | 32 +++++++++++ AOC2021/Days/Day3.cs | 6 ++ AOC2021/Models/AOCDay.cs | 4 ++ AOC2021/Models/Day2/Submarine.cs | 56 +++++++++++++++++++ AOC2021/Models/Day2/SubmarineAction.cs | 7 +++ 7 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 AOC2021/Days/Day2.cs create mode 100644 AOC2021/Days/Day3.cs create mode 100644 AOC2021/Models/Day2/Submarine.cs create mode 100644 AOC2021/Models/Day2/SubmarineAction.cs diff --git a/AOC2021/AOC2021.csproj b/AOC2021/AOC2021.csproj index 4a85dff..b170c40 100644 --- a/AOC2021/AOC2021.csproj +++ b/AOC2021/AOC2021.csproj @@ -2,16 +2,20 @@ net5.0 + AnyCPU;x64 - - - + + + + - + + + diff --git a/AOC2021/Controllers/AdventOfCodeController.cs b/AOC2021/Controllers/AdventOfCodeController.cs index ac9cecb..8dffac0 100644 --- a/AOC2021/Controllers/AdventOfCodeController.cs +++ b/AOC2021/Controllers/AdventOfCodeController.cs @@ -19,7 +19,7 @@ namespace AOC2021.Controllers [HttpPost] [Consumes("text/plain")] - [Route("day1")] + [Route("day1")][Route("day2")] public AOCResponse Day(AOCVersion version, [FromBody] string input, bool IgnoreLogMessages = false) { AOCRequest request = new AOCRequest() { Input = input, Version = version, IgnoreLogMessages = IgnoreLogMessages }; diff --git a/AOC2021/Days/Day2.cs b/AOC2021/Days/Day2.cs new file mode 100644 index 0000000..e88dcc0 --- /dev/null +++ b/AOC2021/Days/Day2.cs @@ -0,0 +1,32 @@ +using AOC2021.Models; +using AOC2021.Models.Day2; + +namespace AOC2021.Days +{ + public class Day2 : AOCDay + { + private Submarine _sub; + public Day2() + { + _sub = new Submarine(); + } + protected override AOCResponse ExecutePartA() + { + var actions = GetSplitInput(); + foreach (var action in actions) + { + _sub.Move(action, this._request.Version); + } + Log("Depth is " + _sub.Depth); + Log("Horiz is " + _sub.Forward); + this._response.Answer = ((_sub.Depth) * _sub.Forward).ToString(); + this._response.Status = true; + return this._response; + } + + protected override AOCResponse ExecutePartB() + { + return ExecutePartA(); + } + } +} diff --git a/AOC2021/Days/Day3.cs b/AOC2021/Days/Day3.cs new file mode 100644 index 0000000..0dde27e --- /dev/null +++ b/AOC2021/Days/Day3.cs @@ -0,0 +1,6 @@ +namespace AOC2021.Days +{ + public class Day3 + { + } +} diff --git a/AOC2021/Models/AOCDay.cs b/AOC2021/Models/AOCDay.cs index 3ebab0c..e597734 100644 --- a/AOC2021/Models/AOCDay.cs +++ b/AOC2021/Models/AOCDay.cs @@ -49,6 +49,10 @@ namespace AOC2021.Models protected abstract AOCResponse ExecutePartA(); protected abstract AOCResponse ExecutePartB(); + protected string[] GetSplitInput() + { + return this._request.Input.Trim().Split("\n"); + } public void SetLogger(ILogger logger) { this._logger = logger; diff --git a/AOC2021/Models/Day2/Submarine.cs b/AOC2021/Models/Day2/Submarine.cs new file mode 100644 index 0000000..c4a074e --- /dev/null +++ b/AOC2021/Models/Day2/Submarine.cs @@ -0,0 +1,56 @@ +namespace AOC2021.Models.Day2 +{ + using System; + using AOC2021.Helper; + public class Submarine + { + public int Depth { get; set; } + public int Forward { get; set; } + + public int Aim { get; set; } + + public void Move(string action, AOCVersion version) + { + var direction = action.Split(" ")[0]; + var value = action.Split(" ")[1]; + var subDir = Enum.Parse(direction.ToUpper()); + if (version == AOCVersion.A) + HandlePartA(value, subDir); + else + HandlePartB(value, subDir); + } + + private void HandlePartA(string value, SubmarineAction subDir) + { + switch (subDir) + { + case SubmarineAction.FORWARD: + Forward += value.ToInt(); + break; + case SubmarineAction.DOWN: + Depth += value.ToInt(); + break; + case SubmarineAction.UP: + Depth -= value.ToInt(); + break; + } + } + + private void HandlePartB(string value, SubmarineAction subDir) + { + switch (subDir) + { + case SubmarineAction.FORWARD: + Forward += value.ToInt(); + Depth += (Aim * value.ToInt()); + break; + case SubmarineAction.DOWN: + Aim += value.ToInt(); + break; + case SubmarineAction.UP: + Aim -= value.ToInt(); + break; + } + } + } +} diff --git a/AOC2021/Models/Day2/SubmarineAction.cs b/AOC2021/Models/Day2/SubmarineAction.cs new file mode 100644 index 0000000..e713745 --- /dev/null +++ b/AOC2021/Models/Day2/SubmarineAction.cs @@ -0,0 +1,7 @@ +namespace AOC2021.Models.Day2 +{ + public enum SubmarineAction + { + FORWARD,UP,DOWN + } +}