mirror of
https://github.com/parnic/advent-of-code-2022.git
synced 2025-06-16 13:40:13 -05:00
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:
@ -75,6 +75,8 @@
|
||||
<EmbeddedResource Include="inputs\25.txt" />
|
||||
<None Remove="inputs\02a.txt" />
|
||||
<EmbeddedResource Include="inputs\02a.txt" />
|
||||
<None Remove="inputs\03a.txt" />
|
||||
<EmbeddedResource Include="inputs\03a.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
1300
inputs/03.txt
1300
inputs/03.txt
File diff suppressed because it is too large
Load Diff
6
inputs/03a.txt
Normal file
6
inputs/03a.txt
Normal file
@ -0,0 +1,6 @@
|
||||
vJrwpWtwJgWrhcsFMMfFFhFp
|
||||
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
|
||||
PmmdzqPrVvPwwTWBwg
|
||||
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
|
||||
ttgJtRGJQctTZtZT
|
||||
CrZsJsPPZsGzwwsLwLmpwMDw
|
37
src/03.cs
Normal file
37
src/03.cs
Normal 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}";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user