Skip to content

Commit

Permalink
Day one solution complete
Browse files Browse the repository at this point in the history
- Needs to be cleaned up,  but later
  • Loading branch information
FlippievanDyk committed Dec 1, 2022
1 parent ee3224a commit 4702d30
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,6 @@ paket-files/
*.sln.iml

# VS Code
.vscode/
.vscode/
**/dummyInput.txt
**/input*.txt
7 changes: 7 additions & 0 deletions Advent2022/Advent/Advent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<None Update="input1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
39 changes: 38 additions & 1 deletion Advent2022/Advent/Day1ElfCalories.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
// See https://aka.ms/new-console-template for more information
namespace Advent2022.Advent.Day1;
using System.Linq;

Console.WriteLine("Hello, World!");
public record Elf
{
public List<int> Caloires { get; set; }
}

public static class ElfHelpers
{
public static List<Elf> GetElves(string path)
{
var listElvesWithFood = File.ReadAllLines(path);
var elves = new List<Elf> { new Elf {Caloires = new List<int>()} };
foreach (var caloriesString in listElvesWithFood)
{
var success = int.TryParse(caloriesString, out var calories);
if (!success)
{
if (elves[^1].Caloires.Count != 0)
{
elves.Add(new Elf {Caloires = new List<int>()});
}
}
else
{
elves[^1].Caloires.Add(calories) ;
}
}
return elves;
}

public static int SumElfTotalCalories(Elf elf)
{
return elf.Caloires.Sum();
}


}
25 changes: 25 additions & 0 deletions Advent2022/Advent/ProgramRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Advent2022.Advent.Day1;

public class ProgramRunner
{
public static int Main(string[] args)
{
// Part 1
// var ans = ElfHelpers.GetElves("./input1.txt")
// .Select(ElfHelpers.SumElfTotalCalories)
// .Max();

// Console.WriteLine(ans);
// part 2
var ans = ElfHelpers.GetElves("./input1.txt")
.Select(ElfHelpers.SumElfTotalCalories)
.OrderByDescending(x => x)
.Take(3)
.Sum()
;

Console.WriteLine(ans);

return 0;
}
}
10 changes: 10 additions & 0 deletions Advent2022/AdventTests/AdventTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<None Update="dummyInput.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Advent\Advent.csproj" />
</ItemGroup>

</Project>
23 changes: 22 additions & 1 deletion Advent2022/AdventTests/Day1ElfCalories.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
using FluentAssertions;

namespace AdventTests;

using Advent2022.Advent.Day1;
public class Day1ElfCaloriesTests
{
[Fact]
public void Test1()
public void GetElves_ShouldReturn_5Elves()
{
const int expected = 5;
var actual = ElfHelpers.GetElves("./dummyInput.txt").Count;
actual.Should().Be(expected);
}

[Fact]
public void GetElfTotalCalories_ShouldSumElfCalories()
{
var elf = new Elf
{
Caloires = new List<int>
{
1, 2, 3, 4
}
};
const int expected = 10;
var actual = ElfHelpers.SumElfTotalCalories(elf);
actual.Should().Be(expected);
}
}

0 comments on commit 4702d30

Please sign in to comment.