Equivalent day 1 in C#

Again, purely for curiosity/performance comparison reasons
This commit is contained in:
2021-12-10 13:51:44 -06:00
parent db06f5b5c6
commit 0cda2f9406
2 changed files with 73 additions and 0 deletions

View File

@ -21,6 +21,9 @@
</PropertyGroup>
<ItemGroup>
<None Update="inputs\01.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="inputs\02.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

70
src/01.cs Normal file
View File

@ -0,0 +1,70 @@
namespace aoc2021
{
internal class Day01
{
internal static void Go()
{
Logger.Log("Day 1");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/01.txt");
Part1(lines);
Part2(lines);
Logger.Log("");
}
private static void Part1(IEnumerable<string> lines)
{
using var t = new Timer();
int lastDepth = 0;
int numIncreased = -1;
foreach (var line in lines)
{
var depth = Convert.ToInt32(line);
if (depth > lastDepth)
{
numIncreased++;
}
lastDepth = depth;
}
Logger.Log($"part1: {numIncreased}");
}
private static void Part2(IEnumerable<string> lines)
{
using var t = new Timer();
int lastTotal = 0;
int numIncreased = -1;
int num1 = -1;
int num2 = -1;
int num3 = -1;
foreach (var line in lines)
{
var depth = Convert.ToInt32(line);
num1 = num2;
num2 = num3;
num3 = depth;
if (num1 < 0 || num2 < 0 || num3 < 0)
{
continue;
}
var total = num1 + num2 + num3;
if (total > lastTotal)
{
numIncreased++;
}
lastTotal = total;
}
Logger.Log($"part2: {numIncreased}");
}
}
}