Day 3 solution

Took me a bit to remember how Aggregate worked, as it always does when I need it.
This commit is contained in:
2022-12-03 08:57:51 -06:00
parent 89484e1a0a
commit 771a21b969
4 changed files with 345 additions and 1000 deletions

View File

@ -75,6 +75,8 @@
<EmbeddedResource Include="inputs\25.txt" /> <EmbeddedResource Include="inputs\25.txt" />
<None Remove="inputs\02a.txt" /> <None Remove="inputs\02a.txt" />
<EmbeddedResource Include="inputs\02a.txt" /> <EmbeddedResource Include="inputs\02a.txt" />
<None Remove="inputs\03a.txt" />
<EmbeddedResource Include="inputs\03a.txt" />
</ItemGroup> </ItemGroup>
</Project> </Project>

File diff suppressed because it is too large Load Diff

6
inputs/03a.txt Normal file
View File

@ -0,0 +1,6 @@
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw

37
src/03.cs Normal file
View File

@ -0,0 +1,37 @@
namespace aoc2022;
internal class Day03 : Day
{
private IEnumerable<string>? sacks;
internal override void Parse()
{
sacks = Util.ReadAllLines("03");
}
static int GetPriority(char x) => x <= 'Z' ? x - 'A' + 27 : x - 'a' + 1;
internal override string Part1()
{
var compartments = sacks!.Select(x => (x[..(x.Length/2)], x[(x.Length/2)..]));
var intersected = compartments.Select(x => x.Item1.Intersect(x.Item2).First());
var total = intersected.Select(GetPriority).Sum();
return $"Sum of duplicates' priorities: <+white>{total}";
}
internal override string Part2()
{
var groups = sacks!.Chunk(3);
var sum = groups.Sum(x =>
GetPriority(
x.Skip(1).Aggregate(
x.First().AsEnumerable(),
(l, e) => l.Intersect(e)
).First()
)
);
return $"Sum of badges' priorities: <+white>{sum}";
}
}