-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 9.txt
198 lines (185 loc) · 5.56 KB
/
Day 9.txt
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
internal class Program
{
static void Main(string[] args)
{
string[] input = File.ReadAllLines(@"C:/Users/pette/Documents/AoC9.txt");
List<Action> actionList = new List<Action>();
foreach (string s in input)
{
actionList.Add(new Action(s[0], Convert.ToInt32(s[2..])));
}
// Create all 10 knots
RopeEnd H = new RopeEnd(0, 0);
RopeEnd a1 = new RopeEnd(0, 0);
RopeEnd a2 = new RopeEnd(0, 0);
RopeEnd a3 = new RopeEnd(0, 0);
RopeEnd a4 = new RopeEnd(0, 0);
RopeEnd a5 = new RopeEnd(0, 0);
RopeEnd a6 = new RopeEnd(0, 0);
RopeEnd a7 = new RopeEnd(0, 0);
RopeEnd a8 = new RopeEnd(0, 0);
RopeEnd a9 = new RopeEnd(0, 0);
// Loop through actions.
foreach (Action a in actionList)
{
// One step at a time, check positions and adjust according
// to instructions for every knot.
for (int i = 0; i < a.steps; i++)
{
H = RopeEnd.MoveHead(H, a.dir);
a1 = RopeEnd.CheckKnot(H, a1);
a2 = RopeEnd.CheckKnot(a1, a2);
a3 = RopeEnd.CheckKnot(a2, a3);
a4 = RopeEnd.CheckKnot(a3, a4);
a5 = RopeEnd.CheckKnot(a4, a5);
a6 = RopeEnd.CheckKnot(a5, a6);
a7 = RopeEnd.CheckKnot(a6, a7);
a8 = RopeEnd.CheckKnot(a7, a8);
a9 = RopeEnd.CheckKnot(a8, a9);
// Check tails position and update list if it's a new coordinate
if (!RopeEnd.FindFootprint(a9))
{
RopeEnd.AddToList(a9);
}
}
}
// Print out total coordinates visited.
Console.WriteLine(RopeEnd.footPrints.Count);
}
}
public class Action
{
public int steps;
public char dir;
public Action(char dir, int steps)
{
this.steps = steps;
this.dir = dir;
}
}
public class RopeEnd
{
public int x;
public int y;
public RopeEnd(int x, int y)
{
this.x = x;
this.y = y;
}
// Create list of positions and add starting point as first entry
public static List<Tuple<int, int>> footPrints = new List<Tuple<int, int>>() { Tuple.Create<int, int>(0, 0) };
// Moves the head one step in ordered direction
public static RopeEnd MoveHead(RopeEnd A, char dir)
{
if (dir == 'U')
{
A.x++;
}
else if (dir == 'D')
{
A.x--;
}
else if (dir == 'R')
{
A.y++;
}
else if (dir == 'L')
{
A.y--;
}
return A;
}
// Checks if current knot is too far away from previous and moves if it is.
public static RopeEnd CheckKnot(RopeEnd A, RopeEnd B)
{
// If previous knot is more than one step above current.
if (A.x - B.x > 1)
{
if (A.y == B.y)
{
B.x++;
}
else if (A.y > B.y)
{
B.x++;
B.y++;
}
else if (A.y < B.y)
{
B.x++;
B.y--;
}
}
// If head is more than one step below tail
else if (A.x - B.x < -1)
{
if (A.y == B.y)
{
B.x--;
}
else if (A.y > B.y)
{
B.x--;
B.y++;
}
else if (A.y < B.y)
{
B.x--;
B.y--;
}
}
// If head is more than one step to the right of tail
if (A.y - B.y > 1)
{
if (A.x == B.x)
{
B.y++;
}
else if (A.x > B.x)
{
B.y++;
B.x++;
}
else if (A.x < B.x)
{
B.y++;
B.x--;
}
}
// If head is more than one step to the left of tail
if (A.y - B.y < -1)
{
if (A.x == B.x)
{
B.y--;
}
else if (A.x > B.x)
{
B.y--;
B.x++;
}
else if (A.x < B.x)
{
B.y--;
B.x--;
}
}
return B;
}
public static void AddToList(RopeEnd A)
{
footPrints.Add(Tuple.Create(A.x, A.y));
}
// Checks if current position has been registered.
public static bool FindFootprint(RopeEnd A)
{
foreach (Tuple<int, int> t in footPrints)
{
if (t.Item1 == A.x && t.Item2 == A.y)
{
return true;
}
}
return false;
}
}