-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day12.cs
141 lines (122 loc) · 3.54 KB
/
Day12.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using AdventOfCode.CSharp.Common;
using System;
using System.Linq;
namespace AdventOfCode.CSharp.Y2015.Solvers;
public class Day12 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
solution.SubmitPart1(SolvePart1(input));
solution.SubmitPart2(SolvePart2(input));
}
private static int SolvePart1(ReadOnlySpan<byte> input)
{
int part1 = 0;
int curNum = 0;
int sign = 1;
bool inNumber = false;
byte prev = (byte)'\0';
foreach (byte c in input)
{
int digit = c - '0';
if (inNumber)
{
if (digit is >= 0 and < 10)
{
curNum = curNum * 10 + digit;
}
else
{
part1 += sign * curNum;
inNumber = false;
}
}
else if (digit is >= 0 and < 10)
{
inNumber = true;
curNum = digit;
sign = prev == '-' ? -1 : 1;
}
prev = c;
}
return part1;
}
private static int SolvePart2(ReadOnlySpan<byte> input)
{
int i = 0;
return ParseValue(input, ref i, out _);
}
private static int ParseValue(ReadOnlySpan<byte> input, ref int i, out bool isRedString)
{
isRedString = false;
return input[i] switch
{
(byte)'{' => ParseObject(input, ref i),
(byte)'[' => ParseArray(input, ref i),
(byte)'\"' => ParseString(input, ref i, out isRedString),
>= (byte)'0' and <= (byte)'9' => ParseNumber(input, isNegative: false, ref i),
(byte)'-' => ParseNumber(input, isNegative: true, ref i),
_ => 0,
};
}
private static int ParseObject(ReadOnlySpan<byte> input, ref int i)
{
// check if it is an empty object first
if (input[i + 1] == '}')
{
i += 2;
return 0;
}
bool isRedString = false;
int total = 0;
while (input[i++] != '}')
{
// parse the property name
_ = ParseString(input, ref i, out _);
// skip the colon
i += 1;
// parse the value
total += ParseValue(input, ref i, out bool isValueRedString);
isRedString = isRedString || isValueRedString;
}
return isRedString ? 0 : total;
}
private static int ParseArray(ReadOnlySpan<byte> input, ref int i)
{
// check if it is an empty array first
if (input[i + 1] == ']')
{
i += 2;
return 0;
}
int total = 0;
while (input[i++] != ']')
{
total += ParseValue(input, ref i, out _);
}
return total;
}
private static int ParseNumber(ReadOnlySpan<byte> input, bool isNegative, ref int i)
{
if (isNegative)
{
i++;
}
int number = 0;
byte c;
while ((c = input[i]) is >= (byte)'0' and <= (byte)'9')
{
number = 10 * number + (c - '0');
i++;
}
return (isNegative ? -1 : 1) * number;
}
private static int ParseString(ReadOnlySpan<byte> input, ref int i, out bool isRedString)
{
i++;
int stringEnd = input[i..].IndexOf((byte)'\"');
isRedString = stringEnd == 3 && input.Slice(i, 3).SequenceEqual("red"u8);
i += stringEnd + 1;
return 0;
}
}