Improved code, changes structure
This commit is contained in:
parent
e6f1c4f483
commit
0a756ab472
@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.3.32922.545
|
VisualStudioVersion = 17.3.32922.545
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day 01", "Day 01\Day 01.csproj", "{0547286C-CC14-44D9-8D7B-26BAA3377721}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "2022", "Day 01\2022.csproj", "{0547286C-CC14-44D9-8D7B-26BAA3377721}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@ -9,7 +9,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Update="day-01-input.txt">
|
<None Update="Day 01\day-01-input.txt">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
29
Day 01/Day 01/Day01.cs
Normal file
29
Day 01/Day 01/Day01.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
namespace Day_01.Day_01
|
||||||
|
{
|
||||||
|
internal class Day01
|
||||||
|
{
|
||||||
|
private static string inputPath = "Day 01/day-01-input.txt";
|
||||||
|
|
||||||
|
internal static int GetPart1()
|
||||||
|
{
|
||||||
|
return File.ReadAllText(inputPath)
|
||||||
|
.TrimEnd()
|
||||||
|
.Split(Environment.NewLine + Environment.NewLine)
|
||||||
|
.Select(s => s.Split(Environment.NewLine)
|
||||||
|
.Sum(Convert.ToInt32))
|
||||||
|
.MaxBy(v => v);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static int GetPart2()
|
||||||
|
{
|
||||||
|
return File.ReadAllText(inputPath)
|
||||||
|
.TrimEnd()
|
||||||
|
.Split(Environment.NewLine + Environment.NewLine)
|
||||||
|
.Select(s => s.Split(Environment.NewLine)
|
||||||
|
.Sum(Convert.ToInt32))
|
||||||
|
.OrderByDescending(v => v)
|
||||||
|
.Take(3)
|
||||||
|
.Sum();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,44 +1,4 @@
|
|||||||
string[] data = await File.ReadAllLinesAsync("day-01-input.txt");
|
using Day_01.Day_01;
|
||||||
|
|
||||||
|
Console.WriteLine($"Max value: {Day01.GetPart2()}");
|
||||||
Console.WriteLine($"Max value: {GetPart2(data)}");
|
|
||||||
Console.ReadKey(true);
|
Console.ReadKey(true);
|
||||||
|
|
||||||
int GetPart1(string[] data)
|
|
||||||
{
|
|
||||||
int maxValue = 0, buffer = 0;
|
|
||||||
|
|
||||||
foreach (string value in data)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
if (buffer > maxValue) maxValue = buffer;
|
|
||||||
buffer = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer += Convert.ToInt32(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return maxValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int GetPart2(string[] data)
|
|
||||||
{
|
|
||||||
List<int> values = new List<int>();
|
|
||||||
int buffer = 0;
|
|
||||||
|
|
||||||
foreach (string value in data)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
values.Add(buffer);
|
|
||||||
buffer = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer += Convert.ToInt32(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return values.OrderByDescending(v => v).Take(3).Sum();
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user