-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.cs
111 lines (87 loc) · 3.08 KB
/
Day1.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2016
{
class Day1 : Day
{
public dynamic Input
{
get
{
return "L5, R1, R4, L5, L4, R3, R1, L1, R4, R5, L1, L3, R4, L2, L4, R2, L4, L1, R3, R1, R1, L1, R1, L5, R5, R2, L5, R2, R1, L2, L4, L4, R191, R2, R5, R1, L1, L2, R5, L2, L3, R4, L1, L1, R1, R50, L1, R1, R76, R5, R4, R2, L5, L3, L5, R2, R1, L1, R2, L3, R4, R2, L1, L1, R4, L1, L1, R185, R1, L5, L4, L5, L3, R2, R3, R1, L5, R1, L3, L2, L2, R5, L1, L1, L3, R1, R4, L2, L1, L1, L3, L4, R5, L2, R3, R5, R1, L4, R5, L3, R3, R3, R1, R1, R5, R2, L2, R5, L5, L4, R4, R3, R5, R1, L3, R1, L2, L2, R3, R4, L1, R4, L1, R4, R3, L1, L4, L1, L5, L2, R2, L1, R1, L5, L3, R4, L1, R5, L5, L5, L1, L3, R1, R5, L2, L4, L5, L1, L1, L2, R5, R5, L4, R3, L2, L1, L3, L4, L5, L5, L2, R4, R3, L5, R4, R2, R1, L5";
}
}
public void Test()
{
Debug.Assert(Part1("R5, L5, R5, R3") == "12");
Debug.Assert(Part2("R8, R4, R4, R8") == "4");
}
public string Part2(dynamic steps)
{
var visited = new List<string>();
var x = 0;
var y = 0;
var dx = 0;
var dy = 1;
foreach (var step in steps.Replace(",", "").Split(' '))
{
var direction = step[0];
var distance = int.Parse(step.Substring(1));
var tmp = dx;
dx = dy;
dy = tmp;
if (direction == 'R')
{
dy = -dy;
}
else
{
dx = -dx;
}
for (int i = 0; i < distance; i++)
{
x += dx;
y += dy;
var coords = x + ":" + y;
if (visited.Contains(coords))
{
return (x + y).ToString();
}
visited.Add(coords);
}
}
return "";
}
public string Part1(dynamic steps)
{
var x = 0;
var y = 0;
var dx = 0;
var dy = 1;
foreach (var step in steps.Replace(",", "").Split(' '))
{
var direction = step[0];
var distance = int.Parse(step.Substring(1));
var tmp = dx;
dx = dy;
dy = tmp;
if (direction == 'R')
{
dy = -dy;
}
else
{
dx = -dx;
}
x += dx * distance;
y += dy * distance;
var coords = x + ":" + y;
}
return (x + y).ToString();
}
}
}