-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.cs
151 lines (128 loc) · 4.04 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
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
using System.Collections;
using System.Diagnostics;
using System.Numerics;
namespace _2023_cs;
public class Day3
{
private static int find_start(string line, int index)
{
while (index > 0 && index < line.Length && char.IsNumber(line[index - 1]))
{
index--;
}
return index;
}
public static int to_int(string line, int index)
{
int number = 0;
for (int i = index; i < line.Length; i++)
{
if (!char.IsDigit(line[i]))
{
break;
}
number *= 10;
number += line[i] - '0';
}
return number;
}
private static bool in_range(IReadOnlyList<string> lines, int i, int j)
{
return !(0 > i || i > lines.Count || 0 > j || j > lines[i].Length);
}
private static int checkAndConvert(IReadOnlyList<string> lines, int newi, int newj,
HashSet<Tuple<int, int>> counted)
{
if (in_range(lines, newi, newj) && char.IsDigit(lines[newi][newj]))
{
counted.Add(new Tuple<int, int>(newi, newj));
return to_int(lines[newi], find_start(lines[newi], newj));
}
return 0;
}
private static Tuple<int, int> CheckSurroundings(IReadOnlyList<string> lines, int i, int j,
HashSet<Tuple<int, int>> counted)
{
int sum = 0;
int sum2 = 0;
List<int> numbers = new List<int>();
//check left
int newi = i;
int newj = j - 1;
numbers.Add(checkAndConvert(lines, newi, newj, counted));
// check right
newj = j + 1;
numbers.Add(checkAndConvert(lines, newi, newj, counted));
//check up
newi = i - 1;
newj = j;
if (in_range(lines, newi, newj) && char.IsDigit(lines[newi][newj]))
{
numbers.Add(checkAndConvert(lines, newi, newj, counted));
}
else
{
newj = j - 1;
numbers.Add(checkAndConvert(lines, newi, newj, counted));
newj = j + 1;
numbers.Add(checkAndConvert(lines, newi, newj, counted));
}
//check down
newi = i + 1;
newj = j;
if (in_range(lines, newi, newj) && char.IsDigit(lines[newi][newj]))
{
numbers.Add(checkAndConvert(lines, newi, newj, counted));
}
else
{
newj = j - 1;
numbers.Add(checkAndConvert(lines, newi, newj, counted));
newj = j + 1;
numbers.Add(checkAndConvert(lines, newi, newj, counted));
}
List<int> numbers2 = new List<int>();
foreach (var num in numbers)
{
if (num != 0)
{
numbers2.Add(num);
}
}
numbers = numbers2;
sum = numbers.Sum();
if (lines[i][j] == '*' && numbers.Count ==2)
{
sum2 = numbers[0] * numbers[1];
}
return new Tuple<int, int>(sum, sum2);
}
public static Tuple<int,int> Part1(string filename)
{
StreamReader input = new StreamReader(filename);
List<string> lines = new List<string>();
string? line = input.ReadLine();
while (line != null)
{
lines.Add(line);
line = input.ReadLine();
}
HashSet<Tuple<int, int>> counted = new HashSet<Tuple<int, int>>();
int sum = 0;
int sum2 = 0;
for (int i = 0; i < lines.Count; i++)
{
for (int j = 0; j < lines[i].Length; j++)
{
if (lines[i][j] == '.' || Char.IsNumber(lines[i][j]))
{
continue;
}
Tuple<int, int> x = CheckSurroundings(lines, i, j, counted);
sum += x.Item1;
sum2 += x.Item2;
}
}
return new Tuple<int,int>(sum,sum2);
}
}