-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day12.java
139 lines (116 loc) · 3.33 KB
/
Day12.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package io.github.sirnikolai.days;
import io.github.sirnikolai.AbstractSolution;
public class Day12 extends AbstractSolution {
enum dir {
NORTH,
SOUTH,
EAST,
WEST
}
@Override
public void Part1(String input) {
int right = 0;
int up = 0;
int dir = 0;
String[] arr = input.split("\n");
for(String s : arr) {
char action = s.charAt(0);
int amount = Integer.parseInt(s.substring(1));
if(action == 'F') {
switch(getDir(dir)) {
case EAST:
action = 'E';
break;
case WEST:
action = 'W';
break;
case NORTH:
action = 'N';
break;
case SOUTH:
action = 'S';
break;
}
}
switch (action) {
case 'E':
right += amount;
break;
case 'W':
right -= amount;
break;
case 'N':
up += amount;
break;
case 'S':
up -= amount;
break;
case 'R':
dir -= amount;
break;
case 'L':
dir += amount;
break;
}
}
System.out.println(Math.abs(right) + Math.abs(up));
}
private dir getDir(int i) {
while(i < 0)
i += 360;
if(i % 360 == 0)
return dir.EAST;
if(i % 360 == 180)
return dir.WEST;
if(i % 360 == 90)
return dir.NORTH;
return dir.SOUTH;
}
@Override
public void Part2(String input) {
int shipRight = 0;
int shipUp = 0;
int right = 10;
int up = 1;
String[] arr = input.split("\n");
for(String s : arr) {
char action = s.charAt(0);
int amount = Integer.parseInt(s.substring(1));
if(action == 'F') {
shipRight += right * amount;
shipUp += up * amount;
}
switch (action) {
case 'E':
right += amount;
break;
case 'W':
right -= amount;
break;
case 'N':
up += amount;
break;
case 'S':
up -= amount;
break;
case 'R':
while(amount > 0) {
int rTemp = right;
right = up;
up = -rTemp;
amount -= 90;
}
break;
case 'L':
while(amount > 0) {
int uTemp = up;
up = right;
right = -uTemp;
amount -= 90;
}
break;
}
}
System.out.println(Math.abs(shipRight) + Math.abs(shipUp));
}
}