-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,4 +255,6 @@ paket-files/ | |
*.sln.iml | ||
|
||
# VS Code | ||
.vscode/ | ||
.vscode/ | ||
**/dummyInput.txt | ||
**/input*.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |