-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolver.cs
95 lines (82 loc) · 2.64 KB
/
Solver.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
using AdventOfCode.PartSubmitter;
using AdventOfCode.Solver;
using AdventOfCode.Utils;
namespace AdventOfCode.Solutions.Y2023.D18;
public class Solver : ISolver<Line[], Line[]>
{
public void Parse(string input, IPartSubmitter<Line[], Line[]> partSubmitter)
{
var lines = input
.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)
.Select((line) => line.Split(' '));
var instructions1 = lines
.Select(
(line) =>
{
var direction = line[0][0] switch
{
'U' => Direction.Up,
'D' => Direction.Down,
'L' => Direction.Left,
'R' => Direction.Right,
_ => throw new Exception("Invalid direction"),
};
var length = byte.Parse(line[1]);
return new Instruction { Direction = direction, Length = length };
}
)
.ToArray();
partSubmitter.SubmitPart1(LinkInstructions(instructions1));
var instructions2 = lines
.Select(
(line) =>
{
var hexString = line[2][2..^1];
var length = int.Parse(hexString[0..5], System.Globalization.NumberStyles.HexNumber);
var direction = hexString[5] switch
{
'0' => Direction.Right,
'1' => Direction.Down,
'2' => Direction.Left,
'3' => Direction.Up,
_ => throw new Exception("Invalid direction"),
};
return new Instruction { Direction = direction, Length = length };
}
)
.ToArray();
partSubmitter.SubmitPart2(LinkInstructions(instructions2));
}
private Line[] LinkInstructions(Instruction[] instructions)
{
Line[] lines = new Line[instructions.Length];
Coordinate current = new(0, 0);
for (int i = 0; i < instructions.Length; i++)
{
var instruction = instructions[i];
Coordinate next = current + (instruction.Direction.ToCoordinate() * instruction.Length);
lines[i] = new Line(current, next);
current = next;
}
return lines;
}
public void Solve(Line[] input1, Line[] input2, IPartSubmitter partSubmitter)
{
partSubmitter.SubmitPart1(GetArea(input1));
partSubmitter.SubmitPart2(GetArea(input2));
}
private long GetArea(Line[] lines)
{
// Implementation of the shoelace formula modified with picks theorem
long interior = 0;
ulong boundary = 0;
foreach (var line in lines)
{
interior += checked(line.Start.X * line.End.Y) - checked(line.End.X * line.Start.Y);
boundary += line.ManhattanLength;
}
interior /= 2;
interior = Math.Abs(interior);
return interior + ((long)boundary / 2) + 1;
}
}