ben
/
aoc
1
0
Fork 0
aoc/AOC2022/Day03.cs

16 lines
607 B
C#
Raw Normal View History

2022-12-03 05:55:49 +00:00
namespace AOC2022;
2022-12-03 05:19:20 +00:00
/// <summary>
2022-12-03 05:41:38 +00:00
/// Day 3: <a href="https://adventofcode.com/2022/day/3"/>
2022-12-03 05:19:20 +00:00
/// </summary>
2023-09-20 18:38:58 +00:00
public sealed class Day03() : Day(2022, 3, "Rucksack Reorganization")
2022-12-03 05:19:20 +00:00
{
2022-12-03 21:28:41 +00:00
public override object Part1() =>
2022-12-03 21:48:19 +00:00
Input.Sum(rucksack => RankItem(rucksack.Chunk(rucksack.Length / 2).Aggregate<IEnumerable<char>>((a, b) => a.Intersect(b)).Single()));
2022-12-03 05:19:20 +00:00
2022-12-03 21:28:41 +00:00
public override object Part2() =>
Input.Chunk(3).Sum(group => RankItem(group.Aggregate<IEnumerable<char>>((a, b) => a.Intersect(b)).Single()));
2022-12-03 05:19:20 +00:00
2022-12-03 06:18:29 +00:00
private static int RankItem(char item) => item - (char.IsUpper(item) ? '&' : '`');
}