Skip to content

Commit

Permalink
day 5 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 5, 2024
1 parent 0875ef9 commit a9e535b
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions AdventOfCode/Day05.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
namespace AdventOfCode;

internal class Day05 : BaseDay
{
private readonly Dictionary<int, List<int>> rules = new Dictionary<int, List<int>>();
private readonly Dictionary<int, List<int>> rulesReversed = new Dictionary<int, List<int>>();
private readonly List<List<int>> updates = new List<List<int>>();

public Day05()
{
string[] lines = File.ReadAllLines(InputFilePath);

bool isRule = true;
foreach (string line in lines)
{
if (line == string.Empty)
{
isRule = false;
continue;
}
if (isRule)
{
string[] ruleParts = line.Split('|');
int left = int.Parse(ruleParts[0]);
int right = int.Parse(ruleParts[1]);

if (!rules.TryAdd(left, new List<int> { right }))
{
rules[left].Add(right);
}

if (!rulesReversed.TryAdd(right, new List<int> { left }))
{
rulesReversed[right].Add(left);
}
}
else
{
updates.Add(line.Split(',').Select(int.Parse).ToList());
}
}
}

public override ValueTask<string> Solve_1()
{
int result = 0;
foreach (var update in updates)
{
if (IsCorrectOrder(update))
{
int middle = update.Skip(update.Count / 2).First();
result += middle;
}
}

return new(result.ToString());
}

public override ValueTask<string> Solve_2()
{
return new("");
}

private bool IsCorrectOrder(List<int> update)
{
for(int i = 0; i < update.Count; ++i)
{
if (rules.TryGetValue(update[i], out List<int> haveToBeToTheRight))
{
var toTheLeft = update.Take(i).ToList();
if (toTheLeft.Intersect(haveToBeToTheRight).Any())
{
return false;
}
}

if (rulesReversed.TryGetValue(update[i], out List<int> haveToBeToTheLeft))
{
var toTheRight = update.Skip(i+1).ToList();
if (toTheRight.Intersect(haveToBeToTheLeft).Any())
{
return false;
}
}
}

return true;
}
}

0 comments on commit a9e535b

Please sign in to comment.