Skip to content

Commit

Permalink
day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
mazharenko committed Dec 5, 2024
1 parent 89de104 commit 27be90c
Show file tree
Hide file tree
Showing 3 changed files with 1,494 additions and 15 deletions.
132 changes: 117 additions & 15 deletions src/aoc/Year2024/Day05/Day05.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,121 @@ namespace aoc.Year2024.Day05;

internal partial class Day05
{
internal partial class Part1
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}

internal partial class Part2
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}
public record Page(List<int> Numbers)
{
public static Page Create(IEnumerable<int> numbers) => new(numbers.ToList());

public int? FindIndex(int number)
{
var index = Numbers.IndexOf(number);
return index == -1 ? null : index;
}

public int Middle() => Numbers[Numbers.Count / 2];
}

public record Rule(int Smaller, int Larger)
{
public static Rule Create(int smaller, int larger) => new(smaller, larger);

public bool Satisfied(Page page)
{
var smallerIndex = page.FindIndex(Smaller);
var largerIndex = page.FindIndex(Larger);
if (smallerIndex is null || largerIndex is null)
return true;
return smallerIndex < largerIndex;
}
}

public static (Rule[] rules, Page[] pages) ParseInput(string input)
{
var rulesParser =
Numerics.IntegerInt32.ThenIgnore(Span.EqualTo("|"))
.Then(Numerics.IntegerInt32)
.Select(Rule.Create)
.Lines();

var pages =
Numerics.IntegerInt32.ManyDelimitedBy(Span.EqualTo(","))
.Select(Page.Create)
.Lines();

return rulesParser.Block().ThenBlock(pages).Parse(input);
}

internal partial class Part1
{
private readonly Example example = new(ExampleInput, 143);

public int Solve((Rule[] rules, Page[] pages) input)
{
var correctPages
= input.pages.Where(page => input.rules.All(rule => rule.Satisfied(page)));
return correctPages.Select(x => x.Middle()).Sum();
}

public (Rule[] rules, Page[] pages) Parse(string input) => ParseInput(input);
}

internal partial class Part2
{
private readonly Example example = new(ExampleInput, 123);

public int Solve((Rule[] rules, Page[] pages) input)
{
var incorrectPages
= input.pages.Where(page => ! input.rules.All(rule => rule.Satisfied(page)));

return incorrectPages.Select(page =>
Page.Create(page.Numbers.Order(new Comparer(input.rules))).Middle())
.Sum();
}

private class Comparer(Rule[] rules) : IComparer<int>
{
public int Compare(int x, int y)
{
if (rules.Contains(Rule.Create(x, y)))
return 1;
if (rules.Contains(Rule.Create(y, x)))
return -1;
return 0;
}
}

public (Rule[] rules, Page[] pages) Parse(string input) => ParseInput(input);
}

private const string ExampleInput =
"""
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47
""";
}
2 changes: 2 additions & 0 deletions tests/AoC.Tests/InputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ private static IEnumerable<PartInputCaseData> GetCases()
yield return new PartInputCaseData(3, 2, "99532691");
yield return new PartInputCaseData(4, 1, "2646");
yield return new PartInputCaseData(4, 2, "2000");
yield return new PartInputCaseData(5, 1, "5639");
yield return new PartInputCaseData(5, 2, "5273");
}
}
Loading

0 comments on commit 27be90c

Please sign in to comment.