Added day 2
This commit is contained in:
parent
537305704b
commit
45ccaf5c22
@ -2,16 +2,20 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Models\Day1\**" />
|
||||
<Content Remove="Models\Day1\**" />
|
||||
<EmbeddedResource Remove="Models\Day1\**" />
|
||||
<None Remove="Models\Day1\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<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>
|
||||
<Folder Include="Models\Day1\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -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 };
|
||||
|
32
AOC2021/Days/Day2.cs
Normal file
32
AOC2021/Days/Day2.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
6
AOC2021/Days/Day3.cs
Normal file
6
AOC2021/Days/Day3.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace AOC2021.Days
|
||||
{
|
||||
public class Day3
|
||||
{
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
56
AOC2021/Models/Day2/Submarine.cs
Normal file
56
AOC2021/Models/Day2/Submarine.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
7
AOC2021/Models/Day2/SubmarineAction.cs
Normal file
7
AOC2021/Models/Day2/SubmarineAction.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace AOC2021.Models.Day2
|
||||
{
|
||||
public enum SubmarineAction
|
||||
{
|
||||
FORWARD,UP,DOWN
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user