solve day 2
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
49a58d28bf
commit
437ad3ecfa
|
@ -5,6 +5,7 @@ public class DayTests
|
|||
{
|
||||
[DataTestMethod]
|
||||
[DataRow(typeof(Day01), "1616", "1645")]
|
||||
[DataRow(typeof(Day02), "2272262", "2134882034")]
|
||||
public void CheckAllDays(Type dayType, string part1, string part2)
|
||||
{
|
||||
var s = Stopwatch.StartNew();
|
||||
|
@ -36,6 +37,7 @@ public class DayTests
|
|||
|
||||
[DataTestMethod]
|
||||
[DataRow(typeof(Day01), "7", "5")]
|
||||
[DataRow(typeof(Day02), "150", "900")]
|
||||
public void CheckTestInputs(Type dayType, string part1, string part2)
|
||||
{
|
||||
Day.UseTestInput = true;
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
namespace aoc2021;
|
||||
|
||||
/// <summary>
|
||||
/// Day 2: <see href="https://adventofcode.com/2021/day/2"/>
|
||||
/// </summary>
|
||||
public sealed class Day02 : Day
|
||||
{
|
||||
public Day02() : base(2, "Dive!")
|
||||
{
|
||||
}
|
||||
|
||||
public override string Part1()
|
||||
{
|
||||
int horiz = 0, depth = 0;
|
||||
foreach (var line in Input)
|
||||
{
|
||||
var s = line.Split(' ');
|
||||
var x = int.Parse(s[1]);
|
||||
switch (s[0])
|
||||
{
|
||||
case "forward":
|
||||
horiz += x;
|
||||
break;
|
||||
case "down":
|
||||
depth += x;
|
||||
break;
|
||||
case "up":
|
||||
depth -= x;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $"{horiz * depth}";
|
||||
}
|
||||
|
||||
public override string Part2()
|
||||
{
|
||||
int aim = 0, depth = 0, horiz = 0;
|
||||
foreach (var line in Input)
|
||||
{
|
||||
var s = line.Split(' ');
|
||||
var x = int.Parse(s[1]);
|
||||
switch (s[0])
|
||||
{
|
||||
case "forward":
|
||||
horiz += x;
|
||||
depth += aim * x;
|
||||
break;
|
||||
case "down":
|
||||
aim += x;
|
||||
break;
|
||||
case "up":
|
||||
aim -= x;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $"{horiz * depth}";
|
||||
}
|
||||
}
|
|
@ -14,11 +14,11 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Collections.Generic"/>
|
||||
<Using Include="System.Diagnostics"/>
|
||||
<Using Include="System.Reflection"/>
|
||||
<Using Include="System.Text"/>
|
||||
<Using Include="System.Text.RegularExpressions"/>
|
||||
<Using Include="System.Collections.Generic" />
|
||||
<Using Include="System.Diagnostics" />
|
||||
<Using Include="System.Reflection" />
|
||||
<Using Include="System.Text" />
|
||||
<Using Include="System.Text.RegularExpressions" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,6 @@
|
|||
forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2
|
Loading…
Reference in New Issue