-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
59 lines (47 loc) · 1.5 KB
/
Program.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
using AdventOfCode.Common;
using Coor = AdventOfCode.Common.Coor<int>;
var moves = Resources.GetInputFileLines();
static Coor FollowHead(Coor head, Coor tail)
{
var diff = head - tail;
var distanceX = Math.Abs(diff.X);
var distanceY = Math.Abs(diff.Y);
var distance = distanceX + distanceY;
return (distanceY, distanceX) switch
{
(2, 0) => tail + ((head.Y - tail.Y) / 2, 0),
(0, 2) => tail + (0, (head.X - tail.X) / 2),
_ => distance <= 2
? tail
: tail + (diff.Y / Math.Max(distanceY, 1), diff.X / Math.Max(distanceX, 1)),
};
}
static int Simulate(int ropeLength, string[] moves)
{
var directions = new Dictionary<char, Coor>
{
['R'] = new Coor(0, 1),
['L'] = new Coor(0, -1),
['U'] = new Coor(1, 0),
['D'] = new Coor(-1, 0),
};
var positions = new HashSet<Coor> { Coor.Zero };
var rope = new List<Coor>(Enumerable.Range(0, ropeLength).Select(_ => Coor.Zero));
foreach (var move in moves)
{
var count = int.Parse(move.Substring(2));
var dir = directions[move[0]];
for (var i = 0; i < count; i++)
{
rope[0] += dir;
for (int j = 1; j < rope.Count; j++)
{
rope[j] = FollowHead(rope[j - 1], rope[j]);
}
positions.Add(rope.Last());
}
}
return positions.Count;
}
Console.WriteLine($"Part 1: {Simulate(2, moves)}");
Console.WriteLine($"Part 2: {Simulate(10, moves)}");