solve day 2
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2021-12-02 00:11:44 -05:00
parent 49a58d28bf
commit 437ad3ecfa
5 changed files with 1073 additions and 5 deletions

View File

@ -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;

60
aoc2021/Day02.cs Normal file
View File

@ -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}";
}
}

View File

@ -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>

1000
aoc2021/input/day02.in Normal file

File diff suppressed because it is too large Load Diff

6
aoc2021/input/test02.in Normal file
View File

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2