-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.cs
90 lines (81 loc) · 2.58 KB
/
day9.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
class Day9 {
class Knot {
public (int x, int y) pos;
public Knot next;
public Knot previous;
}
private static Knot CreateRope(int nKnots) {
var startPos = (0,0);
var head = new Knot() {
pos = startPos
};
var previous = head;
nKnots--;
while(nKnots > 0){
var newKnot = new Knot() {
pos = startPos,
previous = previous
};
previous.next = newKnot;
previous = newKnot;
nKnots--;
}
return head;
}
private static int MoveRope(string[] moves, Knot head) {
var visited = new HashSet<(int,int)>();
visited.Add(head.pos);
foreach(var move in moves) {
var _move = move.Split(" ");
var direction = _move[0];
var amount = Int32.Parse(_move[1]);
while(amount > 0) {
switch(direction) {
case "R" :
head.pos.y ++;
break;
case "L" :
head.pos.y --;
break;
case "U" :
head.pos.x ++;
break;
case "D" :
head.pos.x --;
break;
default: break;
}
var previous = head;
var next = head.next;
while(next != null) {
var diffy = previous.pos.y - next.pos.y;
var diffx = previous.pos.x - next.pos.x;
if (Math.Abs(diffx) > 1 || Math.Abs(diffy) > 1) {
next.pos.x += Math.Sign(diffx);
next.pos.y += Math.Sign(diffy);
if (next.next == null) {
visited.Add(next.pos);
}
}
previous = next;
next = next.next;
}
amount--;
}
}
return visited.Count();
}
private static void part2(string[] input) {
var head = CreateRope(10);
Console.WriteLine($"Part 2: {MoveRope(input, head)}");
}
private static void part1(string[] input) {
var head = CreateRope(2);
Console.WriteLine($"Part 1: {MoveRope(input, head)}");
}
public static void Run() {
var input = File.ReadAllLines(@"input/9");
part1(input);
part2(input);
}
}