Equivalent day 1 in C#
Again, purely for curiosity/performance comparison reasons
This commit is contained in:
@ -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
70
src/01.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user