-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay09.cs
121 lines (103 loc) Β· 3.34 KB
/
Day09.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
namespace AdventOfCode2022;
internal class Day09
{
private readonly List<(Direction, int)> _moves;
public Day09(IEnumerable<string> input)
{
_moves = input.Select(line =>
{
var direction = ToDirection(line[0]);
var distance = int.Parse(line[2..]);
return (direction, distance);
}).ToList();
}
public int PartOne()
{
var head = new IntVector(0, 0);
var tail = new IntVector(0, 0);
var tailPositions = new HashSet<IntVector>() { tail };
foreach (var (direction, distance) in _moves)
{
for (var i = 0; i < distance; i++)
{
head = head.Move(direction);
tail = MoveTail(head, tail);
tailPositions.Add(tail);
}
}
return tailPositions.Count;
}
public int PartTwo()
{
var knots = Enumerable.Repeat(new IntVector(0, 0), 10).ToArray();
var tailPositions = new HashSet<IntVector>() { knots.Last() };
foreach (var (direction, distance) in _moves)
{
for (var i = 0; i < distance; i++)
{
knots[0] = knots[0].Move(direction);
for (var j = 1; j < knots.Length; j++)
{
knots[j] = MoveTail(knots[j - 1], knots[j]);
}
tailPositions.Add(knots.Last());
}
}
return tailPositions.Count;
}
private static IntVector MoveTail(IntVector head, IntVector tail)
{
if (Math.Abs(tail.X - head.X) < 2 && Math.Abs(tail.Y - head.Y) < 2)
{
return tail;
}
var (dx, dy) = (0, 0);
if (tail.X == head.X)
{
dx = 0;
dy = (int)((head.Y - tail.Y) / Math.Abs(head.Y - tail.Y));
}
else if (tail.Y == head.Y)
{
dx = (int)((head.X - tail.X) / Math.Abs(head.X - tail.X));
dy = 0;
}
else
{
dx = (int)((head.X - tail.X) / Math.Abs(head.X - tail.X));
dy = (int)((head.Y - tail.Y) / Math.Abs(head.Y - tail.Y));
}
return new IntVector(tail.X + dx, tail.Y + dy);
}
private static Direction ToDirection(char c) => c switch
{
'R' => Direction.East,
'U' => Direction.North,
'L' => Direction.West,
'D' => Direction.South,
_ => throw new ArgumentOutOfRangeException()
};
}
public class Day09Test
{
private const string InputFile = "Day09.Input.txt";
private const string FirstExampleFile = "Day09.Example1.txt";
private const string SecondExampleFile = "Day09.Example2.txt";
[Theory]
[FileData(FirstExampleFile, FileContents.StringPerLine, 13)]
[FileData(InputFile, FileContents.StringPerLine, 6337)]
public void TestPartOne(IEnumerable<string> input, int expected)
{
var solution = new Day09(input);
Assert.Equal(expected, solution.PartOne());
}
[Theory]
[FileData(FirstExampleFile, FileContents.StringPerLine, 1)]
[FileData(SecondExampleFile, FileContents.StringPerLine, 36)]
[FileData(InputFile, FileContents.StringPerLine, 2455)]
public void TestPartTwo(IEnumerable<string> input, int expected)
{
var solution = new Day09(input);
Assert.Equal(expected, solution.PartTwo());
}
}