generated from eduherminio/AdventOfCode.Template
-
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.
- Loading branch information
1 parent
bfb9ea6
commit ab41793
Showing
1 changed file
with
43 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace AdventOfCode; | ||
|
||
public class Day06 : BaseDay | ||
{ | ||
private Race[] _races; | ||
|
||
public Day06() | ||
{ | ||
string[] lines = File.ReadAllLines(InputFilePath); | ||
string[] times = lines[0].Replace("Time:", "").Split(" ", StringSplitOptions.RemoveEmptyEntries); | ||
string[] distances = lines[1].Replace("Distance:", "").Split(" ", StringSplitOptions.RemoveEmptyEntries); | ||
|
||
_races = new Race[times.Length]; | ||
for (int i = 0; i < times.Length; ++i) | ||
{ | ||
_races[i] = new Race(int.Parse(times[i]), int.Parse(distances[i])); | ||
} | ||
} | ||
|
||
public override ValueTask<string> Solve_1() | ||
{ | ||
int result = _races.Aggregate(1, (result, race) => result * GetWaysToWin(race)); | ||
return new(result.ToString()); | ||
} | ||
|
||
public override ValueTask<string> Solve_2() | ||
{ | ||
return new("TBD"); | ||
} | ||
|
||
private int GetWaysToWin(Race race) | ||
{ | ||
int waysToWin = 0; | ||
for (int i = 0; i < race.Time; ++i) | ||
{ | ||
int distance = (race.Time - i) * i; | ||
waysToWin += distance > race.Distance ? 1 : 0; | ||
} | ||
return waysToWin; | ||
} | ||
|
||
private record Race(int Time, int Distance); | ||
} |