-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay9.cs
More file actions
65 lines (60 loc) · 2.27 KB
/
Copy pathDay9.cs
File metadata and controls
65 lines (60 loc) · 2.27 KB
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
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
namespace Advent_of_Code_2022
{
internal class Day9 : ISolver
{
public string Title => "Rope Bridge";
public string PartOne(string input) => Solve(input, 2);
public string PartTwo(string input) => Solve(input, 10);
private static string Solve(string input, int knots)
{
var visited = new List<Point>();
var moves = input.Split("\n");
var rope = new Point[knots];
foreach (var move in moves)
{
var split = move.Split(' ');
var direction = split[0].Trim();
var steps = int.Parse(split[1].Trim());
for (int i = 0; i < steps; i++)
{
// Move the head
switch (direction)
{
case "R":
rope[0].X++;
break;
case "L":
rope[0].X--;
break;
case "U":
rope[0].Y--;
break;
case "D":
rope[0].Y++;
break;
}
// Move all the knots
for (int j = 1; j < rope.Length; j++)
{
// Work out how far away the next knot is
var x = rope[j - 1].X - rope[j].X;
var y = rope[j - 1].Y - rope[j].Y;
// Only move if we are at least 1 square away
if (Math.Abs(x) > 1 || Math.Abs(y) > 1)
{
// We only want to move 1 square at a time
// Math.Sign(...) returns 0 if the value is 0
rope[j].X += Math.Sign(x);
rope[j].Y += Math.Sign(y);
}
visited.Add(rope[^1]); // Add the tail
}
}
}
return visited.Distinct().Count().ToString();
}
}
}