Completed day 5
This commit is contained in:
parent
40cf8dd019
commit
ea203f1b25
@ -10,7 +10,7 @@ IChallange challange = Host.CreateDefaultBuilder()
|
||||
.Build()
|
||||
.Services
|
||||
.GetService<SolutionManager>()
|
||||
.GetChallange(2024, 4);
|
||||
.GetChallange(2024, 5);
|
||||
|
||||
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");
|
||||
|
||||
|
||||
@ -33,7 +33,6 @@ namespace AdventOfCode.Solutions._2024
|
||||
//1888
|
||||
public async Task<string> GetSolutionPart2()
|
||||
{
|
||||
//_inputReader.SetSampleInput(true);
|
||||
Grid<Point> grid = await _inputReader.ReadToGrid<Point>();
|
||||
int total = grid.FindWithValue('M').Where(p => IsValid(grid, p, 'S')).Count();
|
||||
total += grid.FindWithValue('S').Where(p => IsValid(grid, p, 'M')).Count();
|
||||
|
||||
119
AdventOfCode.Solutions/2024/Day 05/Day05.cs
Normal file
119
AdventOfCode.Solutions/2024/Day 05/Day05.cs
Normal file
@ -0,0 +1,119 @@
|
||||
using AdventOfCode.Core.Shared.IO;
|
||||
|
||||
namespace AdventOfCode.Solutions._2024
|
||||
{
|
||||
public class Day05 : IChallange
|
||||
{
|
||||
public int Year => 2024;
|
||||
|
||||
public int Day => 5;
|
||||
|
||||
private readonly IInputReader _inputReader;
|
||||
|
||||
public Day05(IInputReader inputReader)
|
||||
{
|
||||
_inputReader = inputReader;
|
||||
_inputReader.SetInput(this);
|
||||
}
|
||||
|
||||
// 143
|
||||
// 5651
|
||||
public async Task<string> GetSolutionPart1()
|
||||
{
|
||||
int total = 0;
|
||||
await foreach ((Rule[] rules, int[] updates) in GetUpdateWithRules(true))
|
||||
{
|
||||
total += updates[(updates.Length - 1) / 2];
|
||||
}
|
||||
|
||||
return total.ToString();
|
||||
}
|
||||
|
||||
// 123
|
||||
// 4743
|
||||
public async Task<string> GetSolutionPart2()
|
||||
{
|
||||
int total = 0;
|
||||
await foreach ((Rule[] rules, int[] updates) in GetUpdateWithRules(false))
|
||||
{
|
||||
Rule[] requiredRules = rules;
|
||||
int A = 0,
|
||||
runs = (updates.Length + 1) / 2;
|
||||
|
||||
for (int run = 0; run < runs; run++)
|
||||
{
|
||||
int[] B = requiredRules.Select(r => r.B).Distinct().ToArray();
|
||||
A = requiredRules.Where(r => !B.Contains(r.A)).Select(r => r.A).First();
|
||||
requiredRules = requiredRules.Where(r => r.A != A).ToArray();
|
||||
}
|
||||
|
||||
total += A;
|
||||
}
|
||||
|
||||
return total.ToString();
|
||||
}
|
||||
|
||||
private struct Rule
|
||||
{
|
||||
public int A { get; set; }
|
||||
public int B { get; set; }
|
||||
}
|
||||
|
||||
private async IAsyncEnumerable<(Rule[] rules, int[] updates)> GetUpdateWithRules(bool returnValidSet = true)
|
||||
{
|
||||
List<Rule> rules = [];
|
||||
bool inputComplete = false;
|
||||
await foreach (string updateString in _inputReader.ReadAsStringLine())
|
||||
{
|
||||
if (!inputComplete)
|
||||
{
|
||||
if (string.IsNullOrEmpty(updateString))
|
||||
inputComplete = true;
|
||||
else
|
||||
rules.Add(ParseRule(updateString));
|
||||
continue;
|
||||
}
|
||||
|
||||
int[] updatePages = updateString.Split(',').Select(int.Parse).ToArray();
|
||||
Rule[] requiredRules = rules
|
||||
.Where(r => updatePages.Contains(r.A))
|
||||
.Where(r => updatePages.Contains(r.B))
|
||||
.ToArray();
|
||||
|
||||
if (IsValid(requiredRules, updatePages) == returnValidSet)
|
||||
{
|
||||
yield return (requiredRules, updatePages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsValid(Rule[] rules, int[] updates)
|
||||
{
|
||||
Rule[] requiredRules = rules;
|
||||
List<Rule> openRules = [];
|
||||
foreach (int update in updates)
|
||||
{
|
||||
// check if the current update is in the required list as the second value, if so this is an invalid update
|
||||
if (requiredRules.Any(r => r.B == update))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// get new rules
|
||||
openRules.AddRange(requiredRules.Where(r => r.A == update));
|
||||
requiredRules = requiredRules.Where(r => r.A != update).ToArray();
|
||||
|
||||
// close open rules
|
||||
openRules = openRules.Where(r => r.B != update).ToList();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Rule ParseRule(string input)
|
||||
{
|
||||
int[] numbers = input.Split('|').Select(int.Parse).ToArray();
|
||||
return new Rule { A = numbers[0], B = numbers[1] };
|
||||
}
|
||||
}
|
||||
}
|
||||
1373
AdventOfCode.Solutions/2024/Day 05/day-05-input.txt
Normal file
1373
AdventOfCode.Solutions/2024/Day 05/day-05-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -34,6 +34,9 @@
|
||||
<Compile Update="2023\Day 13\Day13.cs">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
<Compile Update="2024\Day 05\Day05.cs">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
<Compile Update="2024\Day 04\Day04.cs">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
|
||||
@ -1,10 +1,28 @@
|
||||
MMMSXXMASM
|
||||
MSAMXMSMSA
|
||||
AMXSXMAAMM
|
||||
MSAMASMSMX
|
||||
XMASAMXAMM
|
||||
XXAMMXXAMA
|
||||
SMSMSASXSS
|
||||
SAXAMASAAA
|
||||
MAMMMXMMMM
|
||||
MXMXAXMASX
|
||||
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
|
||||
Loading…
Reference in New Issue
Block a user