From 0cda2f940607bb4a9f94c730c798fd695f93f504 Mon Sep 17 00:00:00 2001 From: Parnic Date: Fri, 10 Dec 2021 13:51:44 -0600 Subject: [PATCH] Equivalent day 1 in C# Again, purely for curiosity/performance comparison reasons --- advent-of-code-2021.csproj | 3 ++ src/01.cs | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/01.cs diff --git a/advent-of-code-2021.csproj b/advent-of-code-2021.csproj index a1b53cc..6a8a541 100644 --- a/advent-of-code-2021.csproj +++ b/advent-of-code-2021.csproj @@ -21,6 +21,9 @@ + + PreserveNewest + PreserveNewest diff --git a/src/01.cs b/src/01.cs new file mode 100644 index 0000000..b25ac64 --- /dev/null +++ b/src/01.cs @@ -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 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 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}"); + } + } +}