Day 2 solution in C#
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@ -1 +1,5 @@
|
|||||||
/target
|
/target
|
||||||
|
/.vs/
|
||||||
|
*.user
|
||||||
|
/bin/
|
||||||
|
/obj/
|
||||||
|
29
advent-of-code-2021.csproj
Normal file
29
advent-of-code-2021.csproj
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>aoc2021</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ProduceReferenceAssembly>False</ProduceReferenceAssembly>
|
||||||
|
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
<WarningLevel>5</WarningLevel>
|
||||||
|
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||||
|
<WarningLevel>5</WarningLevel>
|
||||||
|
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="inputs\02.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
25
advent-of-code-2021.sln
Normal file
25
advent-of-code-2021.sln
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.31903.59
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "advent-of-code-2021", "advent-of-code-2021.csproj", "{1B54D933-507B-4F44-9BE3-F1794B593AF7}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{1B54D933-507B-4F44-9BE3-F1794B593AF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{1B54D933-507B-4F44-9BE3-F1794B593AF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{1B54D933-507B-4F44-9BE3-F1794B593AF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{1B54D933-507B-4F44-9BE3-F1794B593AF7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {17D280F0-AD9F-481E-8687-28AAB4A54437}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
1000
inputs/02.txt
Normal file
1000
inputs/02.txt
Normal file
File diff suppressed because it is too large
Load Diff
91
src/02.cs
Normal file
91
src/02.cs
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
namespace aoc2021
|
||||||
|
{
|
||||||
|
internal class Day02
|
||||||
|
{
|
||||||
|
internal static void Go()
|
||||||
|
{
|
||||||
|
var lines = File.ReadAllLines("inputs/02.txt");
|
||||||
|
var instructions = new List<Instruction>();
|
||||||
|
foreach (var instruction in lines)
|
||||||
|
{
|
||||||
|
var fmt = instruction.Split(' ');
|
||||||
|
instructions.Add(new Instruction()
|
||||||
|
{
|
||||||
|
Direction = fmt[0],
|
||||||
|
Amount = Convert.ToInt64(fmt[1]),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Part1(instructions);
|
||||||
|
Part2(instructions);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Instruction
|
||||||
|
{
|
||||||
|
public string Direction;
|
||||||
|
public long Amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Position
|
||||||
|
{
|
||||||
|
public long h;
|
||||||
|
public long d;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Part1(IEnumerable<Instruction> instructions)
|
||||||
|
{
|
||||||
|
Position pos = new();
|
||||||
|
using (var t = new Timer())
|
||||||
|
{
|
||||||
|
foreach (var instruction in instructions)
|
||||||
|
{
|
||||||
|
switch (instruction.Direction)
|
||||||
|
{
|
||||||
|
case "forward":
|
||||||
|
pos.h += instruction.Amount;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "down":
|
||||||
|
pos.d += instruction.Amount;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "up":
|
||||||
|
pos.d -= instruction.Amount;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.Log($"part1: h: {pos.h}, d: {pos.d}, result: {pos.h * pos.d}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Part2(IEnumerable<Instruction> instructions)
|
||||||
|
{
|
||||||
|
Position pos = new();
|
||||||
|
using (var t = new Timer())
|
||||||
|
{
|
||||||
|
long aim = 0;
|
||||||
|
foreach (var instruction in instructions)
|
||||||
|
{
|
||||||
|
switch (instruction.Direction)
|
||||||
|
{
|
||||||
|
case "forward":
|
||||||
|
pos.h += instruction.Amount;
|
||||||
|
pos.d += aim * instruction.Amount;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "down":
|
||||||
|
aim += instruction.Amount;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "up":
|
||||||
|
aim -= instruction.Amount;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.Log($"part2: h: {pos.h}, d: {pos.d}, result: {pos.h * pos.d}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
src/Logger.cs
Normal file
13
src/Logger.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace aoc2021
|
||||||
|
{
|
||||||
|
internal class Logger
|
||||||
|
{
|
||||||
|
public static void Log(string msg)
|
||||||
|
{
|
||||||
|
Console.WriteLine(msg);
|
||||||
|
Debug.WriteLine(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
47
src/Timer.cs
Normal file
47
src/Timer.cs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace aoc2021
|
||||||
|
{
|
||||||
|
internal class Timer : IDisposable
|
||||||
|
{
|
||||||
|
private readonly Stopwatch stopwatch = Stopwatch.StartNew();
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
stopwatch.Stop();
|
||||||
|
var (elapsed, unit) = ConvertElapsedToHumanReadable();
|
||||||
|
Logger.Log($"Took {elapsed:N1}{unit}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public (double elapsed, string unit) ConvertElapsedToHumanReadable()
|
||||||
|
{
|
||||||
|
return ConvertElapsedToHumanReadable(stopwatch.ElapsedTicks, Stopwatch.Frequency);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (double elapsed, string unit) ConvertElapsedToHumanReadable(double ticks, long frequency)
|
||||||
|
{
|
||||||
|
var elapsed = 1.0d * ticks / frequency;
|
||||||
|
var unit = "s";
|
||||||
|
if (elapsed < 0.001)
|
||||||
|
{
|
||||||
|
elapsed *= 1e+6;
|
||||||
|
unit = "us";
|
||||||
|
}
|
||||||
|
else if (elapsed < 1)
|
||||||
|
{
|
||||||
|
elapsed *= 1000;
|
||||||
|
unit = "ms";
|
||||||
|
}
|
||||||
|
else if (elapsed < 60)
|
||||||
|
{
|
||||||
|
unit = "s";
|
||||||
|
}
|
||||||
|
else if (elapsed < 60 * 60)
|
||||||
|
{
|
||||||
|
elapsed /= 60;
|
||||||
|
unit = "m";
|
||||||
|
}
|
||||||
|
|
||||||
|
return (elapsed, unit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
src/main.cs
Normal file
1
src/main.cs
Normal file
@ -0,0 +1 @@
|
|||||||
|
aoc2021.Day02.Go();
|
Reference in New Issue
Block a user