-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day12.cs
84 lines (76 loc) · 1.72 KB
/
Day12.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
using System;
using System.IO;
class Day12 {
public static int Solution1() {
int x = 0;
int y = 0;
int rot = 0;
foreach (string line in File.ReadLines("input12.txt")) {
char action = line[0];
int amount = int.Parse(line.Substring(1));
if (action == 'N') {
y += amount;
} else if (action == 'S') {
y -= amount;
} else if (action == 'E') {
x += amount;
} else if (action == 'W') {
x -= amount;
} else if (action == 'L') {
rot = (rot - amount / 90 + 4) % 4;
} else if (action == 'R') {
rot = (rot + amount / 90) % 4;
} else if (action == 'F') {
if (rot == 0) {
x += amount;
} else if (rot == 1) {
y -= amount;
} else if (rot == 2) {
x -= amount;
} else {
y += amount;
}
}
}
return Math.Abs(x) + Math.Abs(y);
}
public static int Solution2() {
int x = 10;
int y = 1;
int xPos = 0;
int yPos = 0;
foreach (string line in File.ReadLines("input12.txt")) {
char action = line[0];
int amount = int.Parse(line.Substring(1));
if (action == 'N') {
y += amount;
} else if (action == 'S') {
y -= amount;
} else if (action == 'E') {
x += amount;
} else if (action == 'W') {
x -= amount;
} else if (action == 'L') {
action = 'R';
amount = 360 - amount;
} if (action == 'R') {
if (amount == 90) {
int t = x;
x = y;
y = -t;
} else if (amount == 180) {
x = -x;
y = -y;
} else if (amount == 270) {
int t = x;
x = -y;
y = t;
}
} else if (action == 'F') {
xPos += x * amount;
yPos += y * amount;
}
}
return Math.Abs(xPos) + Math.Abs(yPos);
}
}