-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cs
147 lines (126 loc) · 3.92 KB
/
main.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
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
class Program {
public static bool IsNextToSymbol(int lineIndex, int start, int end, bool[,] symbols) {
int fromY = Math.Max(lineIndex - 1, 0);
int toY = Math.Min(lineIndex + 2, symbols.GetLength(0));
for (int y = fromY; y < toY; y++) {
int fromX = Math.Max(0, start - 1);
int toX = Math.Min(end + 1, symbols.GetLength(1));
for (int x = fromX; x < toX; x++) {
if (symbols[y,x]) {
return true;
}
}
}
return false;
}
public static int GetSumOfAllPartNumbers(string[] lines, bool[,] symbols) {
int sum = 0;
for (int y = 0; y < lines.Length; y++) {
string pattern = @"(\d+)";
MatchCollection matches = Regex.Matches(lines[y], pattern);
foreach (Match match in matches) {
if (int.TryParse(match.Value, out int number)) {
int start = match.Index;
int end = match.Index + match.Length;
if (IsNextToSymbol(y, start, end, symbols)) {
sum = sum + number;
}
};
}
}
return sum;
}
public static bool[,] ParseSymbols(string[] lines) {
int lineLength = lines[0].Length;
bool[,] symbols = new bool[lines.Length, lineLength];
for (int y = 0; y < lines.Length; y++) {
for (int x = 0; x < lineLength; x++) {
char c = lines[y][x];
if ((! char.IsDigit(c)) && c != '.') {
symbols[y,x] = true;
} else {
symbols[y,x] = false;
}
}
}
return symbols;
}
public static void AddToGear(int lineIndex, int start, int end, int number, Gear[,] gears) {
int fromY = Math.Max(lineIndex - 1, 0);
int toY = Math.Min(lineIndex + 2, gears.GetLength(0));
for (int y = fromY; y < toY; y++) {
int fromX = Math.Max(0, start - 1);
int toX = Math.Min(end + 1, gears.GetLength(1));
for (int x = fromX; x < toX; x++) {
Gear gear = gears[y,x];
if (gear != null) {
gear.numbers.Add(number);
}
}
}
}
public static int GetSumOfAllGearRatios(string[] lines, Gear[,] gears) {
for (int y = 0; y < lines.Length; y++) {
string pattern = @"(\d+)";
MatchCollection matches = Regex.Matches(lines[y], pattern);
foreach (Match match in matches) {
if (int.TryParse(match.Value, out int number)) {
int start = match.Index;
int end = match.Index + match.Length;
AddToGear(y, start, end, number, gears);
};
}
}
int sum = 0;
for (int y = 0; y < gears.GetLength(0); y++) {
for (int x = 0; x < gears.GetLength(1); x++) {
Gear gear = gears[y,x];
if (gear != null) {
if (gear.numbers.Count == 2) {
sum += (gear.numbers[0] * gear.numbers[1]);
}
}
}
}
return sum;
}
public class Gear {
public List<int> numbers = new List<int>();
}
public static Gear[,] ParseGears(string[] lines) {
int lineLength = lines[0].Length;
Gear[,] gears = new Gear[lines.Length, lineLength];
for (int y = 0; y < lines.Length; y++) {
for (int x = 0; x < lineLength; x++) {
char c = lines[y][x];
if (c == '*') {
Gear gear = new Gear();
gears[y,x] = gear;
} else {
gears[y,x] = null;
}
}
}
return gears;
}
public static void Solve (string filename) {
string[] lines = File.ReadAllLines(filename);
bool[,] symbols = ParseSymbols(lines);
int sum = GetSumOfAllPartNumbers(lines, symbols);
Console.WriteLine(sum);
}
public static void Solve2 (string filename) {
string[] lines = File.ReadAllLines(filename);
Gear[,] gears = ParseGears(lines);
int sum = GetSumOfAllGearRatios(lines, gears);
Console.WriteLine(sum);
}
public static void Main (string[] args) {
Solve("input");
Solve2("input");
}
}