-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3.cs
63 lines (55 loc) · 2.02 KB
/
Day3.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
using System.Drawing;
using System.Text.RegularExpressions;
static class Day3{
internal static void doit(){
Regex dayNoR = new(@"\d*$");
var lines = Helper.getInputAsLines(int.Parse(dayNoR.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Value),
test:false);
var sumA = 0;
var sumB = 0;
Regex num = new(@"\d+");
Regex sign = new(@"[^\d\.]");
Regex gearR = new(@"\*");
List<Point> pl = new();
Dictionary<Point,List<int>> gearCand = new();
var lNo = 0;
foreach (string line in lines)
{
foreach (Match sMat in sign.Matches(line))
{
pl.Add(new(lNo,sMat.Index));
}
foreach (Match sMat in gearR.Matches(line))
{
gearCand.Add(new(lNo,sMat.Index), new());
}
lNo++;
}
lNo = 0;
foreach (string line in lines)
{
foreach (Match zM in num.Matches(line))
{
var p = pl.Where(a => a.X >= lNo -1 && a.X <= lNo+1 && a.Y >= zM.Index - 1 && a.Y <= zM.Index + zM.Length);
if (p.Any()) {
sumA+=int.Parse(zM.Value);
Console.WriteLine($"Drin: {lNo+1},{zM.Index}: {zM.Value}");
if (p.Count()>1){
Console.WriteLine("achtung");
}
if (gearCand.ContainsKey(p.First())){
gearCand[p.First()].Add(int.Parse(zM.Value));
}
}
else{
Console.WriteLine($"Aussortiert: {lNo+1},{zM.Index}: {zM.Value}");
}
}
lNo++;
Console.WriteLine();
}
sumB=gearCand.Where(a => a.Value.Count() == 2).Select(a=> a.Value.First() * a.Value.Last()).Sum();
Console.WriteLine($"Sum: {sumA}");
Console.WriteLine($"SumB: {sumB}");
}
}