Added day 2

master
Xander Sigler 3 years ago
parent 537305704b
commit 45ccaf5c22

@ -2,16 +2,20 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <Compile Remove="Models\Day1\**" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <Content Remove="Models\Day1\**" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.2.3" /> <EmbeddedResource Remove="Models\Day1\**" />
<None Remove="Models\Day1\**" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Models\Day1\" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.2.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -19,7 +19,7 @@ namespace AOC2021.Controllers
[HttpPost] [HttpPost]
[Consumes("text/plain")] [Consumes("text/plain")]
[Route("day1")] [Route("day1")][Route("day2")]
public AOCResponse Day(AOCVersion version, [FromBody] string input, bool IgnoreLogMessages = false) public AOCResponse Day(AOCVersion version, [FromBody] string input, bool IgnoreLogMessages = false)
{ {
AOCRequest request = new AOCRequest() { Input = input, Version = version, IgnoreLogMessages = IgnoreLogMessages }; AOCRequest request = new AOCRequest() { Input = input, Version = version, IgnoreLogMessages = IgnoreLogMessages };

@ -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();
}
}
}

@ -0,0 +1,6 @@
namespace AOC2021.Days
{
public class Day3
{
}
}

@ -49,6 +49,10 @@ namespace AOC2021.Models
protected abstract AOCResponse ExecutePartA(); protected abstract AOCResponse ExecutePartA();
protected abstract AOCResponse ExecutePartB(); protected abstract AOCResponse ExecutePartB();
protected string[] GetSplitInput()
{
return this._request.Input.Trim().Split("\n");
}
public void SetLogger(ILogger logger) public void SetLogger(ILogger logger)
{ {
this._logger = logger; this._logger = logger;

@ -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<SubmarineAction>(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;
}
}
}
}

@ -0,0 +1,7 @@
namespace AOC2021.Models.Day2
{
public enum SubmarineAction
{
FORWARD,UP,DOWN
}
}
Loading…
Cancel
Save