-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay10.cs
82 lines (69 loc) · 2.29 KB
/
Day10.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
using System;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2023.Solvers;
public class Day10 : ISolver
{
public enum Dir { East, West, North, South }
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int rowLen = input.IndexOf((byte)'\n') + 1;
int startPosIndex = input.IndexOf((byte)'S');
int i = startPosIndex;
// assume that S = (0, 0)
int x = 0;
int y = 0;
Dir dir;
if (input[i - 1] is (byte)'L' or (byte)'F' or (byte)'-')
dir = Dir.West;
else if (input[i + 1] is (byte)'J' or (byte)'7' or (byte)'-')
dir = Dir.East;
else
dir = Dir.South;
int steps = 0;
int area = 0;
while (true)
{
byte c;
int count = 1;
switch (dir)
{
case Dir.East:
while ((c = input[++i]) == '-')
count++;
steps += count;
x += count;
area -= count * y;
dir = c == 'J' ? Dir.North : Dir.South;
break;
case Dir.West:
while ((c = input[--i]) == '-')
count++;
steps += count;
x -= count;
area += count * y;
dir = c == 'L' ? Dir.North : Dir.South;
break;
case Dir.North:
while ((c = input[i -= rowLen]) == '|')
count++;
steps += count;
y -= count;
area -= count * x;
dir = c == '7' ? Dir.West : Dir.East;
break;
case Dir.South:
while ((c = input[i += rowLen]) == '|')
count++;
steps += count;
y += count;
area += count * x;
dir = c == 'L' ? Dir.East : Dir.West;
break;
}
if (i == startPosIndex)
break;
}
solution.SubmitPart1(steps / 2);
solution.SubmitPart2((Math.Abs(area) - steps) / 2 + 1);
}
}