-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day09.cs
62 lines (54 loc) · 1.55 KB
/
Day09.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2016.Solvers;
public class Day09 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
input = input.TrimEnd((byte)'\n');
int part1 = SolvePart1(input);
long part2 = SolvePart2(input);
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
private static int SolvePart1(ReadOnlySpan<byte> input)
{
int length = 0;
var reader = new SpanReader(input);
while (!reader.Done)
{
if (reader.Read() == '(')
{
int repLength = reader.ReadPosIntUntil('x');
int repCount = reader.ReadPosIntUntil(')');
length += repLength * repCount;
reader.SkipLength(repLength);
}
else
{
length += 1;
}
}
return length;
}
private static long SolvePart2(ReadOnlySpan<byte> input)
{
long length = 0;
var reader = new SpanReader(input);
while (!reader.Done)
{
if (reader.Read() == '(')
{
int repLength = reader.ReadPosIntUntil('x');
int repCount = reader.ReadPosIntUntil(')');
ReadOnlySpan<byte> rep = reader.ReadBytes(repLength);
length += SolvePart2(rep) * repCount;
}
else
{
length += 1;
}
}
return length;
}
}