-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day3.cs
142 lines (123 loc) · 2.95 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
using System.Numerics;
using System.Text.RegularExpressions;
namespace AdventOfCode2023
{
internal class Day3
{
public class EnginePartNumber
{
public List<Vector2> positions = new();
public int partNr;
public EnginePartNumber(int nr, Vector2 firstPosition)
{
partNr = nr;
positions.Add(firstPosition);
if (partNr.ToString().Length > 1)
{
for (int i = 1; i < partNr.ToString().Length; i++)
{
positions.Add(new Vector2(firstPosition.X + i, firstPosition.Y));
}
}
}
public bool IsAdjacentToSymbol(Vector2 symbolPosition)
{
foreach (Vector2 position in positions)
{
float distance = Vector2.DistanceSquared(symbolPosition, position);
if (distance <= 2)
{
return true;
}
}
return false;
}
}
public static void RunPuzzle1()
{
string[] lines = File.ReadAllLines(@"Day3-Input.txt");
int numberOfCharacters = lines.Sum(s => s.Length);
int sumEngineParts = 0;
int x = 0, y = 0;
List<EnginePartNumber> parts = new();
List<Vector2> symbolLocations = new();
foreach (string line in lines)
{
Regex regex = new Regex(@"\d+");
foreach (Match match in regex.Matches(line))
{
parts.Add(new(int.Parse(match.Value), new Vector2(match.Index, y)));
}
foreach (char c in line.ToCharArray())
{
if (!char.IsNumber(c) && c != '.')
{
symbolLocations.Add(new Vector2(x, y));
}
x++;
}
x = 0;
y++;
}
foreach (EnginePartNumber part in parts)
{
foreach (Vector2 symbolPosition in symbolLocations)
{
if (part.IsAdjacentToSymbol(symbolPosition))
{
sumEngineParts += part.partNr;
}
}
}
Console.WriteLine("Answer Puzzle 1: " + sumEngineParts);
}
public static void RunPuzzle2()
{
string[] lines = File.ReadAllLines(@"Day3-Input.txt");
int numberOfCharacters = lines.Sum(s => s.Length);
int sumEngineParts = 0;
int x = 0, y = 0;
List<EnginePartNumber> parts = new();
List<Vector2> gearLocations = new();
foreach (string line in lines)
{
Regex regex = new Regex(@"\d+");
foreach (Match match in regex.Matches(line))
{
parts.Add(new(int.Parse(match.Value), new Vector2(match.Index, y)));
}
foreach (char c in line.ToCharArray())
{
if (c == '*')
{
gearLocations.Add(new Vector2(x, y));
}
x++;
}
x = 0;
y++;
}
foreach (Vector2 gearLocation in gearLocations)
{
List<EnginePartNumber> foundGearParts = new();
foreach (EnginePartNumber part in parts)
{
if (part.IsAdjacentToSymbol(gearLocation))
{
foundGearParts.Add(part);
}
}
if (foundGearParts.Count == 2)
{
int multipliedGear = 1;
foreach (EnginePartNumber foundGearPart in foundGearParts)
{
multipliedGear *= foundGearPart.partNr;
}
sumEngineParts += multipliedGear;
}
}
Console.WriteLine("Answer Puzzle 2: " + sumEngineParts);
}
}
}