-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.cs
95 lines (78 loc) · 2.41 KB
/
Day01.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
namespace AdventOfCode.Year2016;
public class Day01 : Solution
{
public override Answer One(string input)
{
var instructions = input.Split(',').Select(l => l.Trim()).Select(l => (l[0], l.Int())).ToList();
var f = 0;
var bn = 0;
var be = 0;
foreach (var (t, d) in instructions)
{
if (t == 'L') f--;
if (t == 'R') f++;
if (f == -1) f = 3;
if (f % 4 == 0) bn += d;
if (f % 4 == 1) be += d;
if (f % 4 == 2) bn -= d;
if (f % 4 == 3) be -= d;
}
return Abs(be) + Abs(bn);
}
public override Answer Two(string input)
{
var instructions = input.Split(',').Select(l => l.Trim()).Select(l => (l[0], l.Int())).ToList();
var f = 0;
var bn = 0;
var be = 0;
var seen = new List<string>();
seen.Add($"{bn},{be}");
foreach (var (t, d) in instructions)
{
if (t == 'L') f--;
if (t == 'R') f++;
if (f == -1) f = 3;
if (f % 4 == 0)
{
for (var i = 0; i < d; i++)
{
bn++;
if (seen.Contains($"{bn},{be}"))
return Abs(be) + Abs(bn);
seen.Add($"{bn},{be}");
}
}
if (f % 4 == 1)
{
for (var i = 0; i < d; i++)
{
be++;
if (seen.Contains($"{bn},{be}"))
return Abs(be) + Abs(bn);
seen.Add($"{bn},{be}");
}
}
if (f % 4 == 2)
{
for (var i = 0; i < d; i++)
{
bn--;
if (seen.Contains($"{bn},{be}"))
return Abs(be) + Abs(bn);
seen.Add($"{bn},{be}");
}
}
if (f % 4 == 3)
{
for (var i = 0; i < d; i++)
{
be--;
if (seen.Contains($"{bn},{be}"))
return Abs(be) + Abs(bn);
seen.Add($"{bn},{be}");
}
}
}
return Abs(be) + Abs(bn);
}
}