-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day3TaskSolver.cs
156 lines (132 loc) · 5.18 KB
/
Day3TaskSolver.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System.Text.RegularExpressions;
namespace AdventOfCode;
public static class Day3TaskSolver
{
private static readonly Regex LineParser = new Regex(@"(?<number>\d+)|(?<symbol>[^0-9.])", RegexOptions.Compiled);
public static async Task RunPart1Async()
{
var inputProvider = new InputProvider();
var input = await inputProvider.GetStringAsync(3);
// input =
// "467..114.." + "\n" +
// "...*......" + "\n" +
// "..35..633." + "\n" +
// "......#..." + "\n" +
// "617*......" + "\n" +
// ".....+.58." + "\n" +
// "..592....." + "\n" +
// "......755." + "\n" +
// "...$.*...." + "\n" +
// ".664.598..";
var lines = input.Split('\n', StringSplitOptions.RemoveEmptyEntries);
var schemaObjects = ParseLines(lines);
var numberParts = schemaObjects
.Where(x => x.Type == SchemaObjectType.Number)
.Where(x => IsAdjacentToSymbol(x, schemaObjects));
var sum = numberParts.Sum(x => int.Parse(x.Value));
Console.WriteLine($"Sum of part numbers {sum}");
}
public static async Task RunPart2Async()
{
var inputProvider = new InputProvider();
var input = await inputProvider.GetStringAsync(3);
// input =
// "467..114.." + "\n" +
// "...*......" + "\n" +
// "..35..633." + "\n" +
// "......#..." + "\n" +
// "617*......" + "\n" +
// ".....+.58." + "\n" +
// "..592....." + "\n" +
// "......755." + "\n" +
// "...$.*...." + "\n" +
// ".664.598..";
var lines = input.Split('\n', StringSplitOptions.RemoveEmptyEntries);
var schemaObjects = ParseLines(lines);
var gears = schemaObjects
.Where(x => x is { Type: SchemaObjectType.Symbol, Value: "*" })
.Select(x => (symbol: x, partNumbers: GetAdjacentNumbers(x, schemaObjects)))
.Where(x => x.partNumbers.Count == 2)
.Select(x => (gear: x.symbol, part1: x.partNumbers[0], part2: x.partNumbers[1]));
var gearRatios = gears.Select(x => int.Parse(x.part1.Value) * int.Parse(x.part2.Value));
var sum = gearRatios.Sum();
Console.WriteLine($"Sum of part numbers {sum}");
}
private static IReadOnlyList<SchemaObject> GetAdjacentNumbers(SchemaObject symbol, IReadOnlyCollection<SchemaObject> schemaObjects)
{
return schemaObjects
.Where(x => x.Type == SchemaObjectType.Number)
.Where(x =>
x.LineIndex == symbol.LineIndex + 1 ||
x.LineIndex == symbol.LineIndex - 1 ||
x.LineIndex == symbol.LineIndex)
.Where(x =>
x.Index + x.Value.Length - 1 >= symbol.Index - 1 &&
x.Index <= symbol.Index + 1)
.ToArray();
}
private static bool IsAdjacentToSymbol(SchemaObject number, IReadOnlyCollection<SchemaObject> schemaObjects)
{
return schemaObjects
.Where(x => x.Type == SchemaObjectType.Symbol)
.Where(x =>
x.LineIndex == number.LineIndex + 1 ||
x.LineIndex == number.LineIndex - 1 ||
x.LineIndex == number.LineIndex)
.Where(x =>
x.Index >= number.Index - 1 &&
x.Index <= number.Index + number.Value.Length)
.Any();
}
private static IReadOnlyCollection<SchemaObject> ParseLines(string[] lines)
{
var result = new List<SchemaObject>();
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i];
var regExResult = LineParser.Matches(line);
foreach (Match match in regExResult)
{
if (match.Groups.TryGetValue("number", out var numberMatch) && numberMatch.Success)
{
var schemaObject = new SchemaObject()
{
Type = SchemaObjectType.Number,
Index = numberMatch.Index,
Value = numberMatch.Value,
LineIndex = i
};
result.Add(schemaObject);
}
else if (match.Groups.TryGetValue("symbol", out var symbolMatch) && symbolMatch.Success)
{
var schemaObject = new SchemaObject()
{
Type = SchemaObjectType.Symbol,
Index = symbolMatch.Index,
Value = symbolMatch.Value,
LineIndex = i
};
result.Add(schemaObject);
}
else
{
throw new ApplicationException($"Unrecognized RegEx result {match}");
}
}
}
return result;
}
}
public sealed record SchemaObject
{
public SchemaObjectType Type { get; init; }
public required string Value { get; init; }
public int Index { get; init; }
public int LineIndex { get; init; }
}
public enum SchemaObjectType
{
Symbol,
Number
}