Skip to content

Commit

Permalink
day 6 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 6, 2023
1 parent bfb9ea6 commit ab41793
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions AdventOfCode/Day06.cs
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);
}

0 comments on commit ab41793

Please sign in to comment.