ben
/
aoc
1
0
Fork 0
aoc/AOC2015/Day10.cs

51 lines
1.1 KiB
C#
Raw Permalink Normal View History

2022-12-03 05:55:49 +00:00
namespace AOC2015;
/// <summary>
2022-12-03 05:41:38 +00:00
/// Day 10: <a href="https://adventofcode.com/2015/day/10"/>
/// </summary>
2023-09-20 18:38:58 +00:00
public sealed class Day10() : Day(2015, 10, "Elves Look, Elves Say")
{
2022-12-11 04:56:33 +00:00
private string? _seed;
2022-11-13 19:51:53 +00:00
2023-12-01 07:30:47 +00:00
public override void ProcessInput() => _seed = Input.First();
2022-11-13 19:51:53 +00:00
public override object Part1()
{
for (var i = 0; i < 40; i++)
2022-12-11 04:56:33 +00:00
_seed = string.Concat(LookAndSay(_seed!));
2022-11-13 19:51:53 +00:00
2022-12-11 04:56:33 +00:00
return _seed!.Length;
2022-11-13 19:51:53 +00:00
}
2022-11-13 19:51:53 +00:00
public override object Part2()
{
for (var i = 0; i < 10; i++)
2022-12-11 04:56:33 +00:00
_seed = string.Concat(LookAndSay(_seed!));
2022-11-13 19:51:53 +00:00
2022-12-11 04:56:33 +00:00
return _seed!.Length;
2022-11-13 19:51:53 +00:00
}
2022-12-11 04:56:33 +00:00
private static IEnumerable<int> LookAndSay(string data)
2022-11-13 19:51:53 +00:00
{
var currentDigit = data[0];
int count = 1, place = 1;
while (place < data.Length)
{
if (data[place] == currentDigit) count++;
else
{
yield return count;
yield return currentDigit - '0';
currentDigit = data[place];
count = 1;
}
place++;
}
yield return count;
yield return currentDigit - '0';
}
}